我正在为家庭作业写一个Paint App,我正在努力拖动鼠标。
MousePressed方法有效(打印坐标),但是当我拖动鼠标时,它会抛出NullPointerException("线程中的异常" AWT-EventQueue-0" java.lang.NullPointerException" )。
package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Enumeration;
import javax.management.remote.SubjectDelegationPermission;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import com.sun.javafx.geom.Rectangle;
import com.sun.prism.shader.DrawEllipse_Color_AlphaTest_Loader;
public class NetpaintGUI extends JFrame {
// Variables for selecting Color and Shape
private JPanel selectionArea = new JPanel();
private ButtonGroup shapeSelectorGroup = new ButtonGroup();
private JRadioButton ellipseBtn = new JRadioButton("Ellipse");
private JRadioButton rectangleBtn = new JRadioButton("Rectangle");
private JRadioButton lineBtn = new JRadioButton("Line");
private JColorChooser colorChooser = new JColorChooser();
private Graphics2D draw;
private Color currColor;
private int currX, currY, prevX, prevY;
public static void main(String[] args) {
JFrame paintFrame = new NetpaintGUI();
paintFrame.setVisible(true);
}
public NetpaintGUI() {
setUpModel();
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
}
});
}
public void drawShape(Graphics g) {
if (ellipseBtn.isSelected()) {
System.out.println("Ellipse, Bro");
}
else if (rectangleBtn.isSelected()) {
System.out.println("Rectangle, Bro");
}
else if (lineBtn.isSelected()) {
System.out.println("Line, Bro");
}
}
private void setUpModel() {
// TODO: Read info (persistence)
// Set JFrame size, color, font, title
this.setTitle("UA Netpaint");
this.setSize(1200, 700);
this.getContentPane().setBackground(Color.WHITE);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setFont(new Font("Courier", Font.BOLD, 16));
// Add ColorSelector to Panel
selectionArea.add(colorChooser);
// Add RadioButtons to ButtonGroup and to Panel, and add Panel to Frame
shapeSelectorGroup.add(ellipseBtn);
shapeSelectorGroup.add(rectangleBtn);
shapeSelectorGroup.add(lineBtn);
selectionArea.add(ellipseBtn);
selectionArea.add(rectangleBtn);
selectionArea.add(lineBtn);
ellipseBtn.setSelected(true);
this.add(selectionArea, BorderLayout.SOUTH);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
System.out.println("X: " + prevX + " Y: " + prevY);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
currX = e.getX();
currY = e.getY();
draw.setColor(colorChooser.getColor());
if (ellipseBtn.isSelected()) {
draw.drawOval(prevX, prevY, currX-prevX, currY-prevY);
}
else if (lineBtn.isSelected()) {
draw.drawLine(prevX, prevY, currX, currY);
}
else if (rectangleBtn.isSelected()) {
draw.drawRect(prevX, prevY, currX-prevX, currY-prevY);
}
repaint();
prevX = currX;
prevY = currY;
}
});
}
}
这就是我一起扔的东西。有问题的代码段是类中最后一个方法,当" draw.draw [Line / Oval / Rect]"时会抛出异常。线叫。
请让我知道我搞砸了什么!谢谢大家!