×

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core Java concepts. We plot with swing, awt, and awt.geom.

  • In program, we use the Swing package to implement Jlabel, JButtons, and JPanel.
  • To create a graphical user interface for the project's front-end, we use AWT (Abstract Window Toolkit).
  • To conduct two-dimensional geometry operations, the awt.geom package is utilised.

To plot a point on a graph, we employ numerous graphics techniques such as draw (), setPaint (), and Fill (), among others. Let's have a look at a graph to see how we can plot coordinates in Java.

PlotJex.java:

import java.awt.*;  
import javax.swing.*;  
import java.awt.geom.*;  
  
public class PlotJex extends JPanel{  
   
    int[] crd = {75, 30, 50, 90};  
    int mrg = 78;  
      
    protected void paintComponent(Graphics grcs){  
        
        super.paintComponent(grcs);  
        Graphics2D gph = (Graphics2D)grcs;  
          
        //Sets the value of a single preference for the rendering algorithms.  
        gph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
          
        // wdt-width and hgt-height  
        int wdt = getWidth();  
        int hgt = getHeight();  
        gph.draw(new Line2D.Double(mrg, mrg, mrg, hgt-mrg));  
        gph.draw(new Line2D.Double(mrg, hgt-mrg, wdt-mrg, hgt-mrg));  
        double x = (double)(wdt-2*mrg)/(crd.length-1);  
        double scale = (double)(hgt-2*mrg)/getMax();  
        // color the points  
        gph.setPaint(Color.GREEN);  
        // points on the graph  
        for(int i=0; i<crd.length; i++){  
            double xp1 = mrg+i*x;  
            double yp1 = hgt-mrg-scale*crd[i];  
            gph.fill(new Ellipse2D.Double(xp1-2, yp1-2, 4, 4));  
        }  
    }  
    // getMax() to find max value  
    private int getMax(){  
        int mx = -Integer.MAX_VALUE;  
        for(int i=0; i<crd.length; i++){  
            if(crd[i]>mx)  
                mx = crd[i];               
        }  
        return mx;  
    }         
    public static void main(String args[]){  
        // instance for JFrame   
        JFrame frm = new JFrame();  
        // size,location and layout  
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frm.add(new PlotJex());  
        frm.setSize(350, 340);  
        frm.setLocation(250, 210);  
        frm.setVisible(true);  
    }  
}  

Output :

Java Plot

PlotExample is the class created in the preceding code for plotting the graph, and chord is an array of values that are treated as coordinates to be plotted on the graph. In addition, we enhance the JPanel class to represent the graph.

Note: The Panel's layout is Flow-Layout by default, but we may change it to any other layout we like, such as grid layout, card layout, box layout, and border layout, among others.

To draw the graph on JPanel, we use super.paintComponent(g). We use the Super keyword in our code to invoke the parent class method or function Object () { [native code] }. In our scenario, the Super function invokes the JPanel class's paintComponent (g) method.

For control over geometry, we use the Graphic 2D class (a fundamental class for two-dimensional shapes that extends the Graphics class).

We employ RenderingHints, which are basic suggestions for Java 2D on how it should render. Simply put, rendering refers to how something is done.

The setRenderingHints () function is used to set new hints and change old ones.

Antialiasing is used to smooth jagged edges when the resolution is poor.

We need four coordinates to create lines representing the x and y axes: x1, y1, x2, and y2. In our code, we utilise the g1.draw () function to create lines.

To change the colour of the points on the graph, we utilise the setpaint () function.

We utilise the for loop in our code, and coordinates are used within that loop.

length () returns the coordinates array's length.

We utilise the getWidth () and getHeight () methods of the component to get the component's height and width.

Instead of the cartesian plane, we plot the points with regard to the size of the component using the xp1 and yp1 variables.


Related Topics

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

How to increment and decrement date using Java?

Before understanding how to increment and decrement the date, one must know about the Calendar class in Java. The Java calendar class offers methods for converting dates between a given moment...

3 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

How Many Ways to Create an Object in Java?

As you know, a class is a blueprint for an object, and you can create objects from it. There are several ways to create objects of classes in Java. This...

6 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.