在 MouseDragged 函数中获取空指针异常。几天来一直试图找出是什么原因造成的,但我不知道是什么原因。任何帮助将不胜感激。我的程序应该添加一个图像文件并能够在它上面绘画。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.geom.*;
import java.util.Vector;
import java.util.*;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.Toolkit;
public class Lab3 {
public static void main(String[] args)
{
new Lab3Frame();
}
}
class Lab3Frame extends JFrame implements ActionListener, MouseListener, MouseMotionListener, ChangeListener
{
JFrame frame;
JPanel toolbar;
JMenuBar menuBar;
JMenu tool, file;
JLabel image;
JToggleButton b1, b2, b3, b4, bcol;
JMenuItem pencil, eraser, brush, line, open;
final JFileChooser fc = new JFileChooser();
BufferedImage img;
JColorChooser tcc;
JScrollBar hbar, vbar;
PaintPanel inkPanel;
private Point[] stroke;
public Color ink_color = Color.black;
public Stroke ink_stroke = new BasicStroke(5.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
final int MAX_SAMPLES = 500;
private int sampleCount;
//private Vector<Line2D.Double> v;
Lab3Frame(){
frame = new JFrame();
menuBar = new JMenuBar();
image = new JLabel();
tool = new JMenu("Tools");
file = new JMenu("File");
pencil = new JMenuItem("Pencil");
eraser = new JMenuItem("Eraser");
brush = new JMenuItem("Brush");
line = new JMenuItem("Line");
tool.add(pencil);
tool.add(line);
tool.add(eraser);
tool.add(brush);
open = new JMenuItem("Open");
file.add(open);
menuBar.add(file);
menuBar.add(tool);
frame.setJMenuBar(menuBar);
tcc = new JColorChooser();
inkPanel = new PaintPanel();
toolbar = new JPanel();
toolbar.setSize(100,300);
image.setBackground(Color.blue);
//TOOLBAR BUTTONS
toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.Y_AXIS));
b1 = new JToggleButton("1");
toolbar.add(b1);
b2 = new JToggleButton("2");
toolbar.add(b2);
b3 = new JToggleButton("3");
toolbar.add(b3);
b4 = new JToggleButton("4");
toolbar.add(b4);
bcol = new JToggleButton(" ");
toolbar.add(bcol);
tcc.setPreviewPanel(new JPanel());
//LISTENERS
pencil.addActionListener(this);
open.addActionListener(this);
image.addMouseMotionListener(this);
pencil.addMouseMotionListener(this);
tcc.getSelectionModel().addChangeListener(this);
inkPanel.addMouseMotionListener(this);
inkPanel.addMouseListener(this);
//hbar.addAdjustmentListener(this);
//vbar.addAdjustmentListener(this);
//tcc.addChangeListener(this);
//ADD ELEMENTS TO FRAME
frame.setSize(600, 600);
hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300);
vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300);
frame.setLayout(new BorderLayout());
image.setLayout(new BorderLayout());
image.setBackground(Color.white);
frame.add(toolbar, BorderLayout.WEST);
frame.add(tcc, BorderLayout.SOUTH);
frame.add(image, BorderLayout.CENTER);
frame.setGlassPane(inkPanel);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open){
int returnVal = fc.showOpenDialog(Lab3Frame.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
// frame.add(inkPanel);
img=ImageIO.read(file);
ImageIcon icon=new ImageIcon(img); // ADDED
image.setIcon(icon); // ADDED
Dimension imageSize = new Dimension(icon.getIconWidth(),icon.getIconHeight()); // ADDED
image.setPreferredSize(imageSize); // ADDED
image.setVisible(true);
//
//image.add(hbar, BorderLayout.SOUTH);
//image.add(vbar, BorderLayout.EAST);
//image.revalidate(); // ADDED
//image.repaint(); // ADDED
}
catch(IOException e1) {
e1.printStackTrace();
}
}
}
}
public void stateChanged(ChangeEvent changeEvent) {
Color customCol = tcc.getColor();
bcol.setBackground(customCol);
}
/*
AdjustmentListener adjustmentListener = new AdjustmentListener(){
public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
System.out.println("Adjusted: " + adjustmentEvent.getValue());
}
};
*/
public void mouseClicked(MouseEvent me) {
int x = me.getX();
int y = me.getY();
if (SwingUtilities.isLeftMouseButton(me))
{
stroke[sampleCount] = new Point(x, y);
int x1 = (int)stroke[sampleCount - 1].getX();
int y1 = (int)stroke[sampleCount - 1].getY();
int x2 = (int)stroke[sampleCount].getX();
int y2 = (int)stroke[sampleCount].getY();
if (sampleCount < MAX_SAMPLES - 1)
++sampleCount;
// draw ink trail from previous point to current point
inkPanel.drawInk(x1, y1, x2, y2);
}
}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {
int x = me.getX();
int y = me.getY();
stroke[sampleCount] = new Point(x, y);
if (sampleCount < MAX_SAMPLES - 1)
++sampleCount;
}
public void mouseDragged(MouseEvent me) {
sampleCount = 1;
int x = me.getX();
int y = me.getY();
System.out.println("y2:" + y );
if (SwingUtilities.isLeftMouseButton(me))
{
// try{
stroke[sampleCount] = new Point(x, y);
int x2 = (int)stroke[sampleCount].getX();
int y2 = (int)stroke[sampleCount].getY();
int x1 = (int)stroke[sampleCount - 1].getX();
int y1 = (int)stroke[sampleCount - 1].getY();
if (sampleCount < MAX_SAMPLES - 1){
++sampleCount;}
//draw.Line2D.
inkPanel.drawInk(x1, y1, x2, y2);
//inkPanel.repaint();
}
/*catch ( Exception err1 ) {
System.out.println( err1.getMessage( ) );
}*/
// draw ink trail from previous point to current point
// }
}
public void mouseReleased(MouseEvent me) {
}
public void mouseMoved(MouseEvent me) {
}
class PaintPanel extends JPanel
{
private Vector<Line2D.Double> v;
private final Stroke INK_STROKE = new BasicStroke(5.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
PaintPanel()
{
v = new Vector<Line2D.Double>();
this.setBackground(Color.white);
}
@Override
public void paintComponent(Graphics gd) {
super.paintComponent(gd);
paintImage(gd);
}
public void paintImage(Graphics gd){
Graphics2D g2 = (Graphics2D)gd;
//Rectangle aa = new Rectangle( 10, 10, 100, 100);
//g2.draw(aa);
g2.setColor(Color.red);
Stroke s = g2.getStroke(); // save current stroke
g2.setStroke(INK_STROKE);
for (int i = 0; i < v.size(); ++i)
g2.draw((Line2D.Double)v.elementAt(i));
g2.setStroke(s);
repaint();
}
public void drawInk(int x1, int y1, int x2, int y2)
{
// get graphics context
Graphics2D g2 = (Graphics2D)this.getGraphics();
//System.out.println("dn " + g2 + "method was called");
// create the line
Line2D.Double inkSegment = new Line2D.Double(x1, y1, x2, y2);
g2.setColor(Color.red); // set the inking color
Stroke s = g2.getStroke(); // save current stroke
g2.setStroke(INK_STROKE); // set desired stroke
g2.draw(inkSegment); // draw it!
g2.setStroke(s); // restore stroke
v.add(inkSegment); // add to vector
repaint();
}
}
}