-2

我一直在 youtube 上观看有关如何使用 MouceListeners 的视频,以及它的工作视频,但在我的电脑上却没有。这里是代码包Applets;

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    public class MouseListenerTut 
    extends Applet
    implements MouseListener{


private Graphics GG = null;


private void Init(){
    this.addMouseListener(this);


}

private int Mx;
private int My;


public void paint(Graphics g){


    GG = g.create();

    this.setSize(new Dimension(800,600));

}


public void DrawDot(int x, int y){

    int r = (int) (Math.random() * 255);
    int g = (int) (Math.random() * 255);
    int b = (int) (Math.random() * 255);

    Color Col = new Color(r,g,b);
    GG.setColor(Col);
    GG.fillOval(x, y, 10, 10);
}


@Override
public void mouseClicked(MouseEvent e) {
     Mx = e.getX();
     My = e.getY();

    DrawDot(Mx,My);
}


@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}


@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}






    }

我真的不明白为什么它不起作用,它没有显示任何错误,而且它看起来非常适合他......

4

2 回答 2

1

g.create()返回图形对象的副本,因此您修改的是副本而不是绘制的内容。即使您没有传递副本,您也将在绘制图形对象后对其进行修改。

paint()你真的应该在你的方法中修改 g ,然后repaint()在点击鼠标时调用,就像这样;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseListenerTut 
extends Applet
implements MouseListener{

    private void Init(){
        this.addMouseListener(this);
        this.setSize(new Dimension(800,600));
    }

    private int Mx=0, My=0;
    private Color Mc = Color.WHITE;

    public void paint(Graphics g){
        g.setColor(Mc);
        g.fillOval(Mx, My, 10, 10);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Mx = e.getX();
        My = e.getY();
        int r = (int) (Math.random() * 255);
        int g = (int) (Math.random() * 255);
        int b = (int) (Math.random() * 255);
        Mc = new Color(r,g,b);
            repaint();
    }

    //Spare Listeners
    @Override
    public void mouseEntered(MouseEvent arg0) {}
    @Override
    public void mouseExited(MouseEvent arg0) {}
    @Override
    public void mousePressed(MouseEvent arg0) {}
    @Override
    public void mouseReleased(MouseEvent arg0) {}
}
于 2013-08-07T14:49:42.123 回答
1

您正在绘制已停用的图形。使用 BufferedImage 来跟踪您的所有绘图。

使用此绘制方法:

public void paint(Graphics g)
{
    g.drawImage(buffer, 0, 0, null);
}

使用这个 mouseClicked 方法:

public void DrawDot(int x, int y)
{
    int r = (int) (Math.random() * 255);
    int g = (int) (Math.random() * 255);
    int b = (int) (Math.random() * 255);

    Graphics g = buffer.createGraphics();
    Color Col = new Color(r,g,b);
    g.setColor(Col);
    g.fillOval(x, y, 10, 10);
    g.dispose();

    repaint(); // make sure you show your edits on screen
}

最后确保你有一个buffer对象。把它放在你的课堂上:

 private BufferedImage buffer = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGBA_PRE);
于 2013-08-07T14:51:40.077 回答