Skip to content

Painting graphics on a JPanel

September 15, 2011

Creating graphics or painting graphics is a common task in Java.  I have created a simple-ish application that uses a loop to paint randomly sized, colored  and positioned objects in a panel. This application demonstrates the following topics:

  • multi classed application
  • use of the Graphics class
  • paintComponent() method
  • for loop
  • conditional check for even numbers
  • random number generation
  • dynamic assignment of random numbers to graphics properties
  • BorderLayout manager
  • constructor utilization

If you copy and past this example, it should generate a GUI like this:

To demonstrate the implicit redraw, simply resize the frame.  When you resize the frame, Java will sense the action and implicitly (automatically) call the redraw() method – which generates newly sized and positioned objects.

To understand more about the implicate calling of the paintComponent() and repaint() methods, read this post on stackoverflow.

Here are the source classes, starting with the Painter class, which is the *main class (includes the main method).

import javax.swing.*;
import java.awt.*;

public class Painter extends JPanel{


    public void buildGUI(){
        // create main frame
        JFrame frame = new JFrame();
        //set a layout for the main frame
        frame.setLayout(new BorderLayout());
        //add a title to the frame
        frame.setTitle("Paint drawing demonstration");

        //create Center_frame object - pass in the frame to center
        new Center_frame(frame);

        //create header panel
        JPanel headerPanel = new JPanel();

        //add a label to the top of the header panel
        headerPanel.add(new JLabel("The drawing panel is below"));

        //create the DrawingPanel object
        Drawing_panel dp = new Drawing_panel();

        //add the panels to the frame using the BorderLayout

        //show the frame

    public static void main(String args[]){
        //create a Painter object
        new Painter();

}//end class Painter

Next is the Drawing_panel class, which does the generating/drawing of the objects.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

class Drawing_panel extends JPanel {

    public void paintComponent(Graphics g) {

        int x, posx, posy, width, height;

            //even number differentiation
            if(x % 2 == 0){

            Random rand = new Random();
            posx = rand.nextInt(300);
            posy = rand.nextInt(300);
            width = rand.nextInt(40);
            height = rand.nextInt(40);

            //System.out.println("the ran x pos is: " + posx);
            g.fillRect(posx,posy, width, height);
        }//end for    
    }//end paintComponent  

    public Dimension getPreferredSize() {
        return new Dimension(400,400);

The last class is the Center_frame class, which centers the main JPanel frame.

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Center_frame {

    Center_frame(JFrame arg){

    public void centerFrame(JFrame arg){
        //create a toolkit object
        Toolkit tk = Toolkit.getDefaultToolkit();
        //call method on Toolkit object
        Dimension screenSize = tk.getScreenSize();

        //set screen dimensions
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;

        //apply dimensions to frame object passed into method
        arg.setSize(screenWidth / 2, screenHeight / 2);
        arg.setLocation(screenWidth / 4, screenHeight / 4);      
    }//end centerFrame
}//end class

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: