You can draw (paint) on any Java Container (JFrame, JPanel, JApplet). To do so:
public void paint(Graphics g) .
The variable g is the "Graphics" that you are drawing on, and is a part of every
drawing command. This method (or methods called from this method) should draw everything that is to appear in the component. Begin by erasing everything left over from the previous drawing, with a pair of commands such as g.setColor(Color.WHITE);
|
paint method! Instead, to repaint the Container, call the inherited method repaint(), which will call paint with the needed Graphics parameter. Java uses an X-Y coordinate system, where the units are pixels (picture elements, usually about 72 pixels per inch). X=0, Y=0 is the top left corner of the applet window. If the applet window is 400 pixels wide and 300 pixels high, then the bottom right corner is X=399, Y=299. It's okay to draw things larger than the window, but only the part visible "through the window" will be visible on the screen.
Most of the drawing commands take place within a rectangle defined by (x, y, width, height), where x and y are the coordinates of the top left corner of the rectangle. In algebra, increasing values of y are "higher" in a graph; but in Java, increasing y moves you down on the screen.
Here is a brief explanation of some of the commands you can use in the paint method. If
you want a more detailed explanation, take your browser to https://docs.oracle.com/javase/7/docs/api/,
click on java.awt in the upper left pane, then click on
Graphics (under Classes) in the lower left pane. The large pane on the right will
show detailed explanations of these and other commands.
|