考虑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);
}
}
现在包含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.
}
}
问题:在课程DriverClass.java
的最后一行,静态方法&#39; drawNewColoredLine
&#39;被调用包含列表作为参数。我想使用静态对象&#39; gra2&#39;作为&#39; drawNewColoredLine
&#39;等所有方法的图形对象。
'gra2'
初始化为方法'paintComponent'
内的图形对象。 'gra2'
适用于方法&#39; drawGraph'
。但它并没有找到方法&draw;抽奖彩色线&#39; drawNewColoredLine&#39; - 引发空指针异常。 [&#39; gra2&#39;在'drawNewColoredLine'
]中为空。那么如何让它在'drawNewColoredLine'
内工作?
答案 0 :(得分:1)
你有什么理由使用静态方法吗?你应该摆脱它。您创建了GraphPanel类的对象,但是您没有参考,所以在调用另一个静态方法之后,您没有像预期的那样初始化字段。尝试删除所有静态,删除createAndShowGui中创建新对象并调用以下内容:
....
GraphPanel graphPanel = new GraphPanel(graphPoints);
graphPanel.createAndShowGui(graphPoints); // actually you don't need to pass it again.
....
graphPanel.drawNewColoredLine(ls);
....
答案 1 :(得分:0)
不应存储Graphics
中收到的paintComponent
个对象。
相反,从paintComponent
内调用所有绘画代码,并将Graphic2D
对象传递给其他绘画函数,就像您对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);
....
....