我想在两个 JPanel 之间画线;请验证我的代码是否在“g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);”处给出 NULL 指针异常
代码::
Draw(JPanel one , JPanel two)
{
//Draw Line
Graphics2D g=null;
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.lightGray);
2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.black);
Stroke s = new BasicStroke(4.0f);
// For getting the points of JPanel ona and two//
int x1 = one.getX() + one.getWidth() / 2;
int y1 = one.getY() + one.getHeight() / 2;
int x2 = one.getX() + one.getWidth() / 2;
int y2 = two.getY() + two.getHeight() / 2;
//Drawing line
g2d.drawLine(x1, y1, x2, y2);
}