Step 4

Drawing lines


Our first bit of code will show you how to draw your own lines within the Java program.

Shown here is an example of how to draw lines within our program. In this example, we draw a simple “X” pattern.

Before drawing a line, if we want to change it’s color we use the “g2d.setColor” before drawing it. (To see a list of colors, wait for a second after typing the “.” after “Color,” a list of colors should appear.)

We then create a new Line2D object by giving it a pair of coordinates, the beginning and end of the line.

Lastly, we tell g2d to draw the line, making it appear on the window. We then repeat to get the second line.

Try experimenting with the lines and the colors to really get a hang of it, it definitely takes some getting used to!