usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Drawing Graphs in Java | Java Graphics & Coordinate Plane Tutorial



Understanding the Java Coordinate System and Cartesian Plane

The Cartesian Plane is the usual graph surface we are taught in school. It consists of the x and y axes.

Before drawing, it's important to understand how the Java coordinate system works. The origin '(0,0)' is at the top-left corner of the window. The x-axis increases from left to right, while the y-axis increases from top to bottom. This is different from the traditional Cartesian plane, but with some adjustments, you can easily plot graphs in Java.

In Java, the canvas component would represent something like a graph book; and this graph book is plotted or drawn on using code.
There is a subtle difference between the way we use graphs and the Java drawing components:
The y-axis of the Java drawing components is measured from the top and increases as you move downwards.

Cartesian graph compared to Java coordinate plane.
Figure: Cartesian graph compared to Java coordinate plane.

Setting Up Java Graphics with Canvas and Swing

To start, we use the AWT Canvas or a JPanel in Swing as our drawing surface. The 'paint(Graphics g)' method allows us to call graphics commands such as 'drawLine', 'drawRect', and 'drawOval'.

Drawing on the Java Canvas like on Graphs

In this tutorial, we will learn how to draw graphs in Java using the built-in Java graphics libraries. By working with Canvas and Swing components, you'll see how to plot the coordinate plane in Java and draw lines, shapes, and graphs step by step. This is especially useful for students studying senior secondary Java programming or anyone starting with Java 2D graphics.

In Java, drawing is done using the java.awt and/or javax.swing libraries.
With java.awt, drawing is usually done using an Applet or Canvas component.
With javax.swing, drawing is usually done using a JApplet or JPanel component.

Using Java AWT drawing and Swing graphics, you can draw lines, rectangles, ovals, and even plot mathematical functions in Java.

Create a new Java project; call it Dymetric.
Create a new java class file; call it Facet.
Create a second java class file; call it CanvasFeel.

Type out the adjoining Java algorithm to see what the canvas component feels like.


Drawing Shapes and Graphs in Java

With the same techniques, you can extend your program to:

  • Draw multiple lines and shapes (rectangles, ovals, polygons).
  • Create Java graph plotting for mathematical functions.
  • Compare Java AWT vs Swing drawing performance.

Key Takeaway on Java Graphics with Canvas and Swing

Learning to draw graphs in Java helps you understand both programming logic and graphical visualization. Whether you're plotting the Cartesian plane in Java, experimenting with shapes, or working on school projects, these skills are essential for building interactive Java GUI applications.







Java Code for Drawing Graphs - Main Class

package dymetric;

public class Dymetric {

    public static void main(String[] args) {
        Facet lcd = new Facet();
        lcd.setVisible(true);
    }
    
}

Java Code for Drawing Graphs - Window Display

package dymetric;

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

public class Facet extends JFrame {
    
    private Container face;
    private CanvasFeel canvas;
    private ImageIcon logo;
    
    public Facet() {
        // Give our window a title
        super("A window that will hold a Canvas");
        // set window start points and dimensions
        setSize(780,400);
        // close the window when told to
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true); // should this window be resizable?
        
        // It'll be nice to have our own logo (feel free to use yours)
        logo = new ImageIcon(getClass().getResource("studyingPays.png"));
        this.setIconImage(logo.getImage());
        
        face = this.getContentPane();
        // background colour - may not be necessay since our canvas will have a colour of its own
        face.setBackground(Color.PINK);

        
        canvas = new CanvasFeel();
        // using the default layout manager
        face.add(canvas, BorderLayout.NORTH);
    }
}

Java Code for Drawing Graphs - Canvas Class

package dymetric;

import java.awt.*;

public class CanvasFeel extends Canvas {
    public CanvasFeel() {
        setBackground(Color.YELLOW);
    }
}

Important: Get the logo image(.png) used in the code here and save it to the same folder(directory) as your code files.
For NetBeans, this folder should be C:\Users\user_name\Documents\NetBeansProjects\Dymetric\src\dymetric.

Note: The code module for our main class will remain the same for the rest of the demonstrations in this category.
Hence it will be left out hence forth.




Java Drawing Tutorial Reference

For a thorough explanation of drawing graphics in Java, please see the following link:
The Java Tutorials - Creating a GUI With Swing      - largely apt for Java beginners; Knock your head out!!




<< PreviousNext >>