Java graphics programming
With Java, you can create various graphics including lines, rectangles, and more.
This lesson focuses on:
- Using the Graphics class
- Drawing lines
- Drawing rectangles
- The canvas class
Using the Graphics class
The Graphics class is the class used to allow a component to draw onto itself. The Graphics class is located in the java.awt package. To use the Graphics class, you have to pass a Graphics object in a components paint() method.
Example:
public void paint(Graphics g){
}
Drawing lines
To draw lines, the drawLine(int x1, int y1, int x2, int y2) method is used. This method draws a line that goes from the point x1, y1 to the point x2, y2.
Example:
public void paint(Graphics g){
g.drawLine(10, 10, 50, 50);
}
In the above example, a line will be drawn starting at point 10,10 and ending at point 50,50.
Drawing rectangles
To draw rectangles, the drawRect(int x, int y, int width, int height) method is used. This method draws the outline of a rectangle using the top left corner at point x,y and having the specified width and height.
Example:
public void paint(Graphics g){
g.drawRect(100, 100, 100, 100);
}
In the above example, a rectangle will be drawn starting at point 100,100 with a width of 100 and a height of 100.
To fill a rectangle, the fillRect(int x, int y, int widht, int height) method is used. This method fills a rectangle at the given position with the given width and height.
Example:
public void paint(Graphics g){
g.fillRect(100, 100, 100, 100);
}
In the above example, a rectangle will be filled starting at point 100,100 with a width of 100 and a height of 100.
The Canvas class
The Canvas class is the class used to include canvases in a program that you can draw graphics on.
Canvas class constructors:
-
Canvas()
Creates a new canvas.
Canvas class methods:
-
void paint(Graphics g)
Paints the canvas.
-
void setSize(width, height)
Sets the size of the canvas.
-
void setBackground(Color c)
Sets the background color of the canvas.
-
boolean isEnabled()
Returns true if the canvas is enabled and false if it is not.
A graphics program example:
import java.awt.*;
class GraphicsProgram extends Canvas{
public GraphicsProgram(){
setSize(200, 200);
setBackground(Color.white);
}
public static void main(String[] argS){
GraphicsProgram GP = new GraphicsProgram();
Frame aFrame = new Frame();
aFrame.setSize(300, 300);
aFrame.add(GP);
aFrame.setVisible(true);
}
public void paint(Graphics g){
g.drawLine(10, 10, 50, 50);
g.drawRect(100, 100, 100, 100);
g.fillRect(100, 100, 100, 100);
}
}
Output:
In the above example, a class named GraphicsProgram that extends the Canvas class is created. The constructor for this class specifies that it should be 200 by 200 and its background color should be white. Within the main() method, a new instance of the GraphicsProgram class is created as well as a new instance of the Frame class. The frame that is created within the main() method is set to be 300 by 300, contain the instance of the GraphicsProgram class that is also created within the main() method, and that it should also be visible. The paint() method draws a line starting at point 10,10 and ending at point 50,50 using the drawLine() method. The paint() method than draws a rectangle starting at point 100,100 that has a width of 100 and a height of 100 using the drawRect() method. The paint method than fills the rectangle starting at point 100,100 with a width of 100 and a height of 100 using the fillRect() method.




