Graphics

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

public void paint(Graphics g) { . . . }

Graphics

Grid

An applet does not have a main() method. It has a collection of methods that the Web browser (or appletViewer) calls at appropriate times.

The paint() method is called by the Web browser to draw the rectangular part of the screen that the applet is responsible for. The Graphics object that is passed to the method represents the rectangular part of the screen given to the applet.

The dimensions of this rectangle are specified in the <applet> tag of the Web page. (More on this, later) Dimensions are measured in pixels. The rectangle may be width pixels across and height pixels in height. A location in the rectangle is given as (X,Y) where both are integers. The (0, 0) location is at the top left.

Think of Graphics as representing a rectangle of graph paper where each small square is a pixel. The columns of pixels are numbered 0 to width-1 starting at the left. The rows of pixels are numbered 0 to height-1 starting at the top.

QUESTION 2:

What is the name of the method (of class Graphics) that draws a line from (x0, y0) to (x1, y1) ?

( int x0, int y0, int x1, int y1)