我开始了一个学校项目试图在更大的JPanel上绘制9 * 9和17 * 17像素的JPanels,例如在Gimp中模仿笔。 我尝试使用MouseClicked来捕获鼠标位置,使用MouseDragged来监听并重绘()我希望笔绘制的(大)JPanel以及要记录的MouseReleased。
问题是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();
}
}
Create.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"));
}
}
答案 0 :(得分:2)
问题是MouseDragged对鼠标的监听不够,所以如果我快速移动,我会得到随机点。
是的,当鼠标快速移动时,你永远不会得到所有的分数。
解决方案是更改绘画代码以在两点之间绘制线条。
因此,您需要遍历包含Points的ArrayList。在第一个和第二个之间画一条线,然后在第二个和第三个等之间划一条线。