×

Graphics Program in Java

Graphics Program in Java

The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some advanced examples.

Creating a Bouncing Ball

The following Java program shows how to create an animated bouncing ball using swing.

FileName: BouncingBallExample.java

 // For using the containers and components present in the swing package.
 import javax.swing.*;
 // For using color and graphics of AWT
 import java.awt.*;
 // The BouncingBallExample class creates
 // a running animation of a bouncing ball using a customized thread
 public class BouncingBallExample extends JFrame
 {
    // Defining the width and height of the canvas, where the ball bounces
    private static final int CNVS_HEIGHT = 490;
    private static final int CNVS_WIDTH = 650;
    // Defining the interval in milliseconds
    // Threads sleep for the given interval of time
    private static final int UPDTE_INTRVL = 51;
    // reference variable of the DrawCanvas class
    // used for drawing the canvas
    private DrawCnvs cnvs; 
    // Characteristics of the bouncing ball
    private int i = 90; // abscissa
    private int j = 90; // ordinate
    private int radius = 240;  // radius of the bouncing ball
    private int iSpeed = 3;  // defining speed in the x direction
    private int jSpeed = 6;  // defining speed in the x direction
    // Constructor of the class
    // used for event handling and set-up purpose
    public Server()
    {
       cnvs = new DrawCnvs(); // object of the DrawCanvas class
       cnvs.setPreferredSize(new Dimension(CNVS_WIDTH, CNVS_HEIGHT));
       this.setContentPane(cnvs);
       // terminating the program on pressing the X button
       this.setDefaultCloseOperation(EXIT_ON_CLOSE);
       // for maximizing the canvas to the specified dimensions
       this.pack();
       this.setTitle("A Yellow Bouncing Ball");
       this.setVisible(true);
       // Create a new thread to run update at regular interval
       Thread th = new Thread()
       {
          @Override
          public void run()
          {
             while (true)
             {
                updt();   // update the (x, y) position
                repaint();  // doing the repainting work
                try
                {
                   // Making the thread to sleep for 51 milliseconds
                   Thread.sleep(UPDTE_INTRVL);  
                } catch (InterruptedException ignore) {}
             }
          }
       };
       // starting the thread hence the run() method is called
       th.start();
    }
    // Update the coordinates of the bouncing ball
    public void updt()
    {
       i = i + iSpeed;
       j = j + jSpeed;
       if (i > CNVS_WIDTH - radius || i < 0)
       {
          iSpeed = -iSpeed;
       }
       if (j > CNVS_HEIGHT - radius || j < 0)
       {
          jSpeed = -jSpeed;
       }
    }
    // Defining the inner class DrawCnvs
    // This class is used for custom drawing
    class DrawCnvs extends JPanel
    {
       @Override
       public void paintComponent(Graphics grph)
       {
          // painting the parent background
          super.paintComponent(grph); 
          // Setting the background color to BLUE
          setBackground(Color.BLUE);
          // the bouncing ball is of the YELLOW colour
          grph.setColor(Color.YELLOW);
          // drawing a circle at position (i, j)
          grph.fillOval(i, j, radius, radius);
       }
    }
    // main method
    public static void main(String argvs[])
    {
       // Fror performing task using a event dispatching thread
       SwingUtilities.invokeLater(new Runnable()
       {
          @Override
          public void run()
          {
             // constructor is invoked for doing the set-up and event handling job
             new BouncingBallExample();
          }
       });
    }
 }              

Output:

Explanation: The output is an animation where the yellow bouncing ball changes its direction on hitting the boundary of canvas. In the output, 4 different positions of the bouncing ball are shown.

The motion of the ball is handled by a thread that calls the updt() method. The updt() method changes the coordinates of the yellow ball. However, only changing the coordinates does not change the position of the ball. The actual change in the position is done by the repaint() method. The repaint() method cleans the window then calls the paint() method. The paint() method draws the yellow ball again with the updated coordinates. Since the thread sleeps for 51 milliseconds the update() as well as the repaint() method is called after 51 milliseconds. Therefore, the yellow ball changes its position after 51 milliseconds. Update the value of interval to increase/ decrease the speed of the yellow ball.

The SwingUtilities.invokeLater() method is a static method that allows to perform tasks asynchronously. The invokeLater() method sends the job to swing to execute on the event dispatch thread. However, the method does not guarantee the execution immediately, as it is asynchronous. This method is very handy when multithreading is used along-with Java Swing.

Moving a Line using Arrow Key

The following Java program shows how to create an animated bouncing ball Java using swing.

FileName: MovingLineExample.java

 // For color and graphics
 import java.awt.*;
 // For listener interface
 import java.awt.event.*;
 // For Swing's containers and components
 import javax.swing.*;   
 // The MovingLineExample class uses button / arrow keys to move the line right or left
 public class MovingLineExample extends JFrame
 {
    // Define constants for the various dimensions
    // Defining width and height of the canvas
    public static final int CNVS_HEIGHT = 350;
    public static final int CNVS_WIDTH = 550;
    // the line is of white colour
    // and the background color is black
    public static final Color LINE_CLR = Color.WHITE;
    public static final Color CNVS_BACKGROUND = Color.BLACK;
    // Drawing a vertical line from (i1, j1) to (i2, j2)
    // The position of the line is initially at center of the canvas
    private int i1 = CNVS_WIDTH / 2;
    private int j1 = CNVS_HEIGHT / 9;
    private int i2 = i1;
    private int j2 = CNVS_HEIGHT / 9 * 6;
    private DrawCnvs cnvs; // The custom drawing canvas (an inner class extends JPanel)
    // Constructor to set up the GUI components and event handlers
    public MovingLineExample()
    {
       // Creating a panel for buttons
       JPanel buttonPanel = new JPanel(new FlowLayout());
      // create a button for leftward movement of the line
       JButton buttonLeft = new JButton("Move <- ");
       buttonPanel.add(buttonLeft); // adding the button to button panel
       buttonLeft.addActionListener(new ActionListener()
       {
          public void actionPerformed(ActionEvent event)
         {
             // control is reached here only when the button is pressed
             i1 = i1 - 11;
             i2 = i2 - 11;
             cnvs.repaint(); // repainting the canvas with new coordinates
             requestFocus(); // change the focus to JFrame to receive KeyEvent
          }
       });
       // create a button for rightward movement of the line
       JButton buttonRight = new JButton("Move ->");
       buttonPanel.add(buttonRight); // adding the button to the button panel
       buttonRight.addActionListener(new ActionListener()
       {
          public void actionPerformed(ActionEvent event)
         {
             // control is reached here only when the button is pressed
             i1 = i1 + 11;
             i2 = i2 + 11;
             cnvs.repaint(); // repainting the canvas with new coordinates
             requestFocus(); // change the focus to JFrame to receive KeyEvent
          }
       });
       // creating an object for the canvas
       cnvs = new DrawCnvs();
       // creating a canvas of specified width and height
       cnvs.setPreferredSize(new Dimension(CNVS_WIDTH, CNVS_HEIGHT));
       // Adding canvas and the button panel to
       // the content pane of the JFrame
       Container cp = getContentPane();
       cp.setLayout(new BorderLayout());
       cp.add(cnvs, BorderLayout.CENTER);
       cp.add(buttonPanel, BorderLayout.SOUTH);
       // Handling the arrow movement when left and right
       // arrow keys are pressed
       addKeyListener(new KeyAdapter()
       {
          @Override
          public void keyPressed(KeyEvent event)
          {
             switch(event.getKeyCode())
             {
                case KeyEvent.VK_LEFT:
                                                  // when left arrow key is pressed do the following
                   i1 = i1 - 11;
                   i2 = i2 - 11;
                   repaint(); // repaint the line with new coordinates
                   break;
                case KeyEvent.VK_RIGHT:
                  // when right arrow key is pressed do the following
                   i1 = i1 + 11;
                   i2 = i2 + 11;
                   repaint(); // repaint the line with new coordinates
                   break;
             }
          }
       });
       // Terminating the application when x button is pressed
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Setting the title
       setTitle("Moving a Vertical Line");
       pack(); // include all the parts in the frame          
       setVisible(true); // display it
        // focus should be on the frame
        // to get a KeyEvent
       requestFocus();  
    }
     // Defining the inner class DrawCnvs, which extends JPanel to do the custom      
     // drawing.
    class DrawCnvs extends JPanel
    {
        // painting the canvas and drawing the line
        // with the specified parameters
       @Override
       public void paintComponent(Graphics grph)
       {
             // painting the background of parent
             super.paintComponent(grph);
            // setting background color
            setBackground(CNVS_BACKGROUND);
           // setting line color
          grph.setColor(LINE_CLR);
           // drawing a vertical line
          grph.drawLine(i1, j1, i2, j2);
       }
    }
    // main method
    public static void main(String argvs[])
    {
       // To achieve thread safety, put the GUI codes on the Event-Dispatcher Thread
       SwingUtilities.invokeLater(new Runnable()
       {
          @Override
          public void run()
          {
                // invoking the constructor to do the set up and event hanlding job
             new MovingLineExample();
          }
       });
    }
 } 

Output:

Pressing the Move -> button / right arrow key three times to change the position of the line as shown in the following snapshot.

Pressing the Move <- button / left arrow key six times the following position of the line is achieved.

Explanation: Initially, the line is positioned at the center of the canvas, which is achieved by dividing the canvas width by 2. For buttons, an appropriate panel is created. The panel is then added to the container of JFrame. The same treatment is done to canvas.

To handle the movement of the line using buttons, appropriate buttons with their actionListeners() are used. Similar scenario for the left and right arrow keys.

The requestFocus() method plays a very important role in the functioning of the application. This method shifts the focus to the keys or buttons when we move from one application to the other application. If the method is removed, the functioning of arrows keys stops, as the focus is never shifted towards the arrow key. The rest of the above-written code is similar to the previous program.


Related Topics

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java concurrency interview questions

During technical interviews, one of the most challenging and sophisticated subjects is concurrency in Java. This page offers responses to some of the related interview questions you might come across. 1....

11 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.