如何绘制扩展MouseInputAdapter的监听器?

时间:2012-10-04 16:24:03

标签: java swing animation jpanel paintcomponent

我正在尝试使用扩展MouseInputAdapter的类来绘制自由形状,我稍后可能会制作一个动画对象。

我已经看到了一些答案,但他们正在使用addMouseMotionListener(this),据我所知,你不能用一个对象。我的主要问题是实际得到一些东西。我可能遗漏了一些基本的东西,例如在{a}}初始化我的JPanel或在animate方法中添加我的监听器。每当我假设它重新粉刷时,我都会得到nullPointerException。每次或每次mouseDragged激活时。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.util.*;

public class test {

    JFrame frame = new JFrame("Bouncing Vertices");
    MyDrawPanel drawPanel = new MyDrawPanel();
    private int delay = 5;
    public int z = 0;
    public int a = 0;
    public static int counter = 0;
    public static int[] xs;
    public static int[] ys;
    public static boolean isDone = false;

    public void animate() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(drawPanel);
        frame.setSize(800, 600);
        frame.setVisible(true);
        MyListener alpha = new MyListener();
        drawPanel.addMouseMotionListener(alpha);
        drawPanel.addMouseListener(alpha);
        while (true) {
            drawPanel.repaint();
            try {
                Thread.sleep(delay);
            } catch (Exception ex) {
            }
        }
    }

    private class MyListener extends MouseInputAdapter {

        public void mouseDragged(MouseEvent arg0) {
            int x = arg0.getX();
            int y = arg0.getY();

            if (x != z || y != a) {
                xs[counter] = x;
                ys[counter] = y;
                z = x;
                a = y;
                counter++;
            }

        }

        public void mouseReleased(MouseEvent arg0) {
            isDone = true;
        }
    }

    public static int[] getXs() {
        return xs;
    }

    public static int[] getYs() {
        return ys;
    }

    public static boolean getBoolean() {
        return isDone;
    }

    public static void setBoolean() {
        isDone = false;
    }

    public static void setArrays() {
        for (int i = 0; i < ys.length; i++) {

            xs[counter] = 0;
            ys[counter] = 0;
        }
    }

    public static void main(String[] args) {
        test qwerty = new test();
        qwerty.animate();
    }
}

class MyDrawPanel extends JPanel {

    public void paintComponent(Graphics g) {
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        if (test.getBoolean() && test.getXs().length > 0) {
            g.setColor(Color.BLACK);
            g.drawPolyline(test.getXs(), test.getYs(), test.getYs().length);
            test.setBoolean();
            test.setArrays();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我添加了对Swing实用程序的调用,以确保Swing组件位于事件派发线程上。

我重新安排了你的代码,这样你就可以让你的听众和你的绘图板分开。为此,我将主类的实例传递给了侦听器和绘图类。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

public class DrawingTest implements Runnable {

    private JFrame frame;

    private MyDrawPanel drawPanel;

    private List<Point> points;

    public DrawingTest() {
        points = new ArrayList<Point>();
    }

    @Override
    public void run() {
        frame = new JFrame("Bouncing Vertices");

        drawPanel = new MyDrawPanel(this);
        MyListener alpha = new MyListener(this);
        drawPanel.addMouseMotionListener(alpha);
        drawPanel.addMouseListener(alpha);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(drawPanel);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public JPanel getDrawingPanel() {
        return drawPanel;
    }

    public List<Point> getPoints() {
        return points;
    }

    public void setPoint(int x, int y) {
        points.add(new Point(x, y));
    }

    public void resetPoints() {
        points.clear();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DrawingTest());
    }

    private class MyListener extends MouseInputAdapter {

        private DrawingTest drawingTest;

        public MyListener(DrawingTest drawingTest) {
            this.drawingTest = drawingTest;
        }

        @Override
        public void mouseDragged(MouseEvent event) {
           drawingTest.setPoint(event.getX(), event.getY());
           drawingTest.getDrawingPanel().repaint();
        }

        @Override
        public void mouseReleased(MouseEvent event) {
            drawingTest.resetPoints();
        }

    }

}

class MyDrawPanel extends JPanel {

    private DrawingTest drawingTest;

    public MyDrawPanel(DrawingTest drawingTest) {
        this.drawingTest = drawingTest;
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.BLUE);
        for (int i = 1; i < drawingTest.getPoints().size(); i++) {
            Point p1 = drawingTest.getPoints().get(i - 1);
            Point p2 = drawingTest.getPoints().get(i);
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }
}