简体   繁体   中英

Graphics object doesn't work inside a method

Consider the following code snippets in java: class: GraphPanel.java

package graph_draw;

import graph_draw.LocationPrinter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
class GraphPanel extends JPanel {

    private Color lineColor = new Color(44, 102, 230, 180);

    private List<Point> graphPoints = null;
    private static Graphics2D gra2;

    public GraphPanel(List<Point> gPoints) {
        graphPoints = gPoints;        
    }    

    @Override
    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);
        //gra2 = (Graphics2D)g;
          gra2 = (Graphics2D)g.create();

        gra2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        drawGraph(gra2, graphPoints);
    }

    public void drawGraph(Graphics2D g2, List<Point> graphPoints)
    {
        Graphics2D g = (Graphics2D)g2;

        g.setColor(lineColor);

        int x1 = graphPoints.get(0).x;
        int y1 = graphPoints.get(0).y;

        int x2 = graphPoints.get(1).x;
        int y2 = graphPoints.get(1).y;

        int x3 = graphPoints.get(2).x;
        int y3 = graphPoints.get(2).y;

        int x4 = graphPoints.get(3).x;
        int y4 = graphPoints.get(3).y;

        int x5 = graphPoints.get(4).x;
        int y5 = graphPoints.get(4).y;

        g.drawLine(x1, y1, x2, y2);
        g.drawLine(x2, y2, x3, y3);
        g.drawLine(x2, y2, x4, y4);
        g.drawLine(x3, y3, x4, y4);
        g.drawLine(x2, y2, x5, y5);
        g.drawLine(x3, y3, x5, y5);

    }

    public static void drawNewColoredLine(List<Point> lst)
    {
        Graphics2D g21 = (Graphics2D)gra2;  // gra2 is 'null' here. So g21 is null. Hence: NullPointerException.

        g21.setColor(Color.GREEN);
        int SIZE = lst.size();

        for(int i = 0; i < SIZE -1 ; i++)
        {
            int xa = lst.get(i).x;
            int ya = lst.get(i).y;

            int xb = lst.get(i+1).x;
            int yb = lst.get(i+1).y;

            g21.drawLine(xa, ya, xb, yb);
        }       
    }


    public static void createAndShowGui(List<Point> graphPoints) {        

        GraphPanel mainPanel = new GraphPanel(graphPoints);
        mainPanel.setPreferredSize(new Dimension(1366, 768));
        JFrame frame = new JFrame("DrawGraph");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}

now the class containing main - class: DriverClass.java

package graph_draw;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

public class DriverClass {

    public DriverClass() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args)
    {       
        List<Point> graphPoints = new ArrayList<>();

        // add points here.

        graphPoints.add(new Point(358, 237));
        graphPoints.add(new Point(366, 467));
        graphPoints.add(new Point(661, 468));
        graphPoints.add(new Point(636, 229));
        graphPoints.add(new Point(527, 648));

        GraphPanel.createAndShowGui(graphPoints);

        List<Point> ls = new ArrayList<Point>();

        ls.add(graphPoints.get(0));
        ls.add(graphPoints.get(1));
        ls.add(graphPoints.get(3));
        ls.add(graphPoints.get(2));
        ls.add(graphPoints.get(4)); 

        GraphPanel.drawNewColoredLine(ls); // not working.          
    }    
}

Problem: In the last line of the class DriverClass.java , the static method ' drawNewColoredLine ' is called containing a list as parameter. I want to use the static object 'gra2' as graphics object for all the methods like ' drawNewColoredLine '.

'gra2' is initialized to a graphics object inside the method 'paintComponent' . 'gra2' works fine for the method ' drawGraph' . But it doesn't woek for the method 'drawNewColoredLine' - a null pointer exception is raised. [ 'gra2' is null inside 'drawNewColoredLine' ]. So how to make it work inside 'drawNewColoredLine' ?

Is there any reason you use static methods? You should get rid of that. You create object of class GraphPanel, but you don't have reference for that, so after calling another static method you don't have initialized fields, as you expected. Try to remove all statics, remove creating new object inside createAndShowGui and call things like this:

....
GraphPanel graphPanel = new GraphPanel(graphPoints);
graphPanel.createAndShowGui(graphPoints); // actually you don't need to pass it again.
....
graphPanel.drawNewColoredLine(ls);
....

The Graphics object received in paintComponent should not be stored. Instead, call all the painting code from within paintComponent , and pass the Graphic2D object to the other painting functions, like you did for drawGraph .

public void paintComponent(Graphics g) 
{
    super.paintComponent(g);
    //gra2 = (Graphics2D)g;
      gra2 = (Graphics2D)g.create();

    gra2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    drawGraph(gra2, graphPoints);
}

public void drawGraph(Graphics2D g, List<Point> graphPoints)
{

    g.setColor(lineColor);
....
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM