gpt4 book ai didi

java - MouseMotionListener 捕获 Point 的速度不够快

转载 作者:行者123 更新时间:2023-12-02 04:55:58 31 4
gpt4 key购买 nike

我开始了一个学校项目,试图在更大的 JPanel 上绘制 9*9 和 17*17 像素的 JPanel,例如模仿 Gimp 中的钢笔。我尝试使用 MouseClicked 来启动、使用 MouseDragged 来监听并重新绘制()(我希望笔在其中绘制并使用 MouseReleased 来记录的(大)JPanel)来捕获鼠标位置。

问题是 MouseDragged 没有足够地监听鼠标,因此如果我快速移动,我会得到随机点。

这是我的 MVC 模式,Create.java - DrawGame.java - ControleurGame.java

你认为什么是最好的?

package controleur;

import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.applet.*;
import java.net.*;
import vue.*;
import vue.Draw.*;
import vue.DrawGame.*;
import modele.*;




/**
*
* @author Yann
*/

public class ControleurGame extends ControleurDraw implements MouseMotionListener {

int mode;
Area zone;
Create draw;

public ControleurGame (Background bg, JButton[] jip, Utilisateur uti, int n, Area j, Create d) {

super(bg);
user = uti;
butts = jip;
mode = n;
zone = j;
draw = d;

resume = new JLabel (user.toString());
resume.setForeground(Color.LIGHT_GRAY);
resume.setBounds(45,600,256+128,32);
resume.setVisible(true);

pan.add(resume);
pan.add(zone);
pan.repaint();

}

@Override
public void focusGained (FocusEvent e) {

if (e.getSource() instanceof JButton) {
but = (JButton)e.getSource();
but.setSelected(true);
pan.repaint();
}

}

@Override
public void focusLost (FocusEvent e) {

if (e.getSource() instanceof JButton) {
but = (JButton)e.getSource();
but.setSelected(false);
pan.repaint();
}

}

@Override
public void keyTyped (KeyEvent e) {

}

@Override
public void keyPressed (KeyEvent e) {

if (e.getSource() instanceof JButton) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
but = (JButton)e.getSource();
but.doClick();
}
}
}

@Override
public void keyReleased (KeyEvent e) {

}

@Override
public void actionPerformed(ActionEvent e) {

}

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {
draw.pixels.add(draw.index,new ArrayList<Point>());
draw.pixels.get(draw.index).add(e.getPoint());
System.out.println(e.getPoint().toString());
zone.addP((int)e.getPoint().getX(),(int)e.getPoint().getY());
pan.repaint();
}

@Override
public void mouseDragged(MouseEvent e) {
draw.pixels.get(draw.index).add(e.getPoint());
System.out.println(e.getPoint().toString());
zone.addP((int)(e.getPoint().getX()),(int)e.getPoint().getY());
pan.repaint();


}

@Override
public void mouseReleased(MouseEvent e) {
draw.index++;
}

@Override
public void mouseMoved(MouseEvent e) {
if (draw.dragging) {

}
}

@Override
public void mouseEntered(MouseEvent e) {
pan.repaint();
}

@Override
public void mouseExited(MouseEvent e) {
if (e.getSource() instanceof JButton){
but = (JButton)e.getSource();
but.setSelected(false);
}

pan.repaint();
}

}

创建.java

package modele;

import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
*
* @author Matthew
*/

public class Create {

public static boolean dragging = false;
public int index = 0;
public ArrayList<ArrayList<Point>> pixels;

public Create () {
pixels = new ArrayList<ArrayList<Point>>();
}

}

DrawGame.java

package vue;

import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.applet.*;
import java.net.*;
import controleur.*;
import modele.*;



/**
*
* @author Yann
*/

public class DrawGame extends Draw {

int mode = 0;
Area tableau;
Create dessin;

public DrawGame (int n, Utilisateur u) {
super(n);

user = u;
library();
dessin = new Create();
try {tableau = new Area();} catch (Exception e) {e.printStackTrace();}

poulet = new ControleurGame(fond,butts,user,mode,tableau,dessin);

tableau.addMouseMotionListener(poulet);
tableau.addMouseListener(poulet);
fond.add(tableau);
fond.repaint();

}

public class Area extends JPanel {

BufferedImage bi = ImageIO.read(new File("../src/png/game/01AreaHL.png"));

public Area () throws IOException {

this.setLayout(null);
this.setBounds(10,50,515,450);
this.setBackground(new Color(0,0,0,0));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, null);
}

public void addP (int n, int m) {
try {this.add(new Pix(n,m));} catch (IOException e) {e.printStackTrace();};
}

public class Pix extends JPanel {

BufferedImage bp;
Random rand = new Random();

public Pix (int n, int m) throws IOException {

try {bp = ImageIO.read(new File("../src/png/game/Points16-2/1602"+rand.nextInt(8)+".png"));} catch (IOException e) {e.printStackTrace();};
this.setLayout(null);
this.setBounds(n-8,m-8,17,17);
this.setBackground(new Color(0,0,0,0));


}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bp, 0, 0, null);
}
}

}

public void library() {

try {icons[0] = ImageIO.read(new File("../src/png/game/00FondGame.png"));}
catch (IOException e) {e.printStackTrace();}

}

public static void main (String[] args){
DrawGame d = new DrawGame(1, Database.getUser("n0x"));
}

}

最佳答案

The problem is that the MouseDragged does not listen enough to the mouse so i get random points if i move to fast.

是的,当鼠标快速移动时,你永远无法获得所有的点。

解决方案是更改绘制代码以在两点之间绘制线条。

因此,您需要遍历包含 Points 的 ArrayList。在第一条和第二条之间画一条线,然后在第二条和第三条之间画一条线,依此类推。

关于java - MouseMotionListener 捕获 Point 的速度不够快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28775078/

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