×

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

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

How to find characters with the maximum number of times in a string java

Problem statement In this problem, users want to find the maximum count of a character from the string and return the character along with its count. Your task is to create...

3 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Java Math max() Method

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

2 minutes read.

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 minutes read.