为什么这个Applet不起作用

时间:2013-08-07 14:44:30

标签: java applet awt paint mouselistener

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

    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

}






    }

我真的不明白为什么它不起作用,它没有显示任何错误,并且接缝完美地为他工作......

2 个答案:

答案 0 :(得分: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) {}
}

答案 1 :(得分: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);