Search code examples
javaswingdrawingpaintcomponentpacman

Convert drawings in instructions for Java Swing paintComponent


Would you know a software able to convert simple drawings (done with simple instructions that can be found in paintComponent like fillRectangle, drawLine... etc) into Java instructions?

I ask this question because I'm programming a Pacman game, and i wanted to be able to randomly generate ghosts, with random color, and shapes.

Thank you.


Solution

  • Maybe you can use the Polygon class. You can create Shapes by specifying Points:

    Polygon triangle = new Polygon();
    triangle.addPoint(0, 0);
    triangle.addPoint(15, 30);
    triangle.addPoint(30, 0);
    g2d.setColor( Color.RED );
    g2d.fill( triangle );
    

    The fill(...) method then just draws lines from one point to another to create the Shape and fill in the color. You can also use the draw(...) method to get the outline of the Shape.

    You can also check out Playing With Shapes. It allows you to use the Shape as an Icon or even a component.