Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java graphics programming

You can display various graphics including lines, rectangles, ovals, and images in Java programs.

This tutorial focuses on:

The Canvas class

The first thing you will need is the Canvas class. This class is used to create an area in a frame to be used for displaying graphics.

NOTE: All the classes you will need to display graphics (as well as frames) are located in the java.awt package.

Canvas class methods:

Add a canvas to a frame just like you would any other component:
Canvas C1 = new Canvas(); C1.setSize(120,120); C1.setBackground(Color.white); Frame F1 = new Frame(); F1.add(C1); F1.setLayout(new FlowLayout()); F1.setSize(250,250); F1.setVisible(true);

Displaying graphics on a component

Now that you have a Canvas (an area to display graphics on) how do you actually display those graphics? With the paint() method of the Frame class.

The paint() method takes one attribute - an instance of the Graphics class. The Graphics class contain methods which are used for displaying graphics. The Graphics class lets a component draw on itself.

Syntax:
public void paint(Graphics g){ //methods for drawing graphics here; }

Drawing lines

To draw lines, the drawLine() method of the Graphics class is used. This method takes four numeric attributes - the first two indicating the x/y starting point of the line, the last two indicating the x/y ending point of the line.

Example:
public void paint(Graphics g){ //draw a line starting at point 10,10 and ending at point 50,50. g.drawLine(10, 10, 50, 50); }

Drawing rectangles

To draw rectangles, the drawRect() method is used. This method takes four numeric attributes - the first two indicating the x/y starting point of the rectangle, the last two indicating the width and height of the rectangle.

Example:
public void paint(Graphics g){ //draw a rectangle starting at 100,100 width a width and height of 80 g.drawRect(100, 100, 80, 80); }

Filling a rectangle

By default a rectangle will have no color on the inside (it will just look like a box). You can use the fillRect() method to fill a rectangle. The fillRect() method has four numeric attributes indicating the x/y starting position to begin filling and the height and width. Set these values the same as you did for the drawRect() method to properly fill the rectangle.

Example:
public void paint(Graphics g){ //draw a rectangle starting at 100,100 width a width and height of 80 g.drawRect(100, 100, 80, 80); g.fillRect(100, 100, 80, 80); }

Something's missing...

The rectangle is filled, but we didn't set a color for it! To do this, we will use the setColor() method.

g.setColor(Color.orange);

Drawing ovals

To draw ovals, the drawOval() method is used. This method takes four numeric attributes - the first two indicating the x/y starting point of the oval, the last two indicating the width and height of the oval. Fill an oval with the fillOval() method which also takes four numeric attributes indicating the starting position to begin filling and the height and width. Set these values the same as you did for the drawOval() method to properly fill the oval.

Example:
public void paint(Graphics g){ g.setColor(Color.gray); //draw an oval starting at 20,20 with a width and height of 100 and fill it g.drawOval(20,20, 100, 100); g.fillOval(20,20, 100, 100); }

Displaying images

To display images, the Image class is used together with the Toolkit class. Use these classes to get the image to display. Use the drawImage() method to display the image.

Example:
public void paint(Graphics g){ Image img1 = Toolkit.getDefaultToolkit().getImage("sky.jpg"); //four attributes: the image, x/y position, an image observer g.drawImage(img1, 10, 10, this); }
An entire Java graphics program:
import java.awt.*; class GraphicsProgram extends Canvas{ public GraphicsProgram(){ setSize(200, 200); setBackground(Color.white); } public static void main(String[] argS){ //GraphicsProgram class is now a type of canvas //since it extends the Canvas class //lets instantiate it GraphicsProgram GP = new GraphicsProgram(); //create a new frame to which we will add a canvas Frame aFrame = new Frame(); aFrame.setSize(300, 300); //add the canvas aFrame.add(GP); aFrame.setVisible(true); } public void paint(Graphics g){ g.setColor(Color.blue); g.drawLine(30, 30, 80, 80); g.drawRect(20, 150, 100, 100); g.fillRect(20, 150, 100, 100); g.fillOval(150, 20, 100, 100); Image img1 = Toolkit.getDefaultToolkit().getImage("sky.jpg"); g.drawImage(img1, 140, 140, this); } }

What it will look like:

Graphical Java program
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information