gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:54 24 4
gpt4 key购买 nike

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

我已经看到了一些答案,但他们使用的是 addMouseMotionListener(this),据我所知,您无法使用对象来执行此操作。我的主要问题实际上是画一些东西。我可能缺少一些基本的东西,比如在哪里初始化我的 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();
}
}
}

最佳答案

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

我重新排列了您的代码,以便您可以将监听器和绘图面板设为单独的类。为此,我将主类的一个实例传递给监听器和绘图类。

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);
}
}
}

关于java - 如何绘制扩展 MouseInputAdapter 的监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12731584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com