×

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 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 Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate...

3 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Java Class Methods

Methods called on a class rather than a specific object instance are known as class methods. The static modifier guarantees uniform implementation across all instances of the class. Syntax public class NameOfClass...

3 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.