How could I delete an object just created with a mouse-click? (when creating another one)

问题: I've been searching through answers and all and I couldn't find a solution for my problem. I wish I can delete the previous circle (created on mouse click) when I create a...

问题:

I've been searching through answers and all and I couldn't find a solution for my problem. I wish I can delete the previous circle (created on mouse click) when I create a new one ?

Thank you so much in advance. :)

package javaapplication1;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class CercuriRandom extends Applet implements MouseListener{

    int x,y,z,r,v,a;
    Thread t;
    Color culoare;
    Random rand;

    @Override
    public void init(){
        t=new Thread();
        rand= new Random();
        culoare=new Color(r, v, a);
        addMouseListener(this);
    }

    @Override
    public void update(Graphics g){
        g.setColor(culoare);
        g.fillRoundRect(x, y, z, z, z, z);
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) { }
        //repaint();
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        if (me.getButton()==MouseEvent.BUTTON1) {
            r=rand.nextInt(256);
            v=rand.nextInt(256);
            a=rand.nextInt(256);
            culoare=new Color(r,v,a);
            x=rand.nextInt(getWidth());
            y=rand.nextInt(getWidth());
            z=rand.nextInt(100);
            repaint();
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {

    }

    @Override
    public void mouseReleased(MouseEvent me) {

    }

    @Override
    public void mouseEntered(MouseEvent me) {

    }

    @Override
    public void mouseExited(MouseEvent me) {

    }

}

I'll leave here the code needed for creating the circles. I've been searching through answers and all and I couldn't find a solution for my problem. I wish I can delete the previous circle (created on mouse click) when I create a new one ?

Thank you so much in advance. :)


回答1:

You can refer to the circles by storing the values of the last circle that was drawn as class variables:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class CercuriRandom extends Applet implements MouseListener {

    int x, y, z, r, v, a;
    // class attributes that store the last circle that was drawn
    int lastX, lastY, lastZ;
    Thread t;
    Color culoare;
    Random rand;

    @Override
    public void init() {
        t = new Thread();
        rand = new Random();
        culoare = new Color(r, v, a);
        addMouseListener(this);
    }

    @Override
    public void update(Graphics g){
        // clear the last drawn rectangle (before painting the new one)
        g.clearRect(lastX, lastY, lastZ, lastZ);

        g.setColor(culoare);
        g.fillRoundRect(x, y, z, z, z, z);

        // store the last values in order to delete them on next draw
        lastX = x;
        lastY = y;
        lastZ = z;
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) { }
        //repaint();
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        if (me.getButton() == MouseEvent.BUTTON1) {
            r = rand.nextInt(256);
            v = rand.nextInt(256);
            a = rand.nextInt(256);
            culoare = new Color(r, v, a);
            x = rand.nextInt(getWidth());
            y = rand.nextInt(getWidth());
            z = rand.nextInt(100);
            repaint();
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {

    }

    @Override
    public void mouseReleased(MouseEvent me) {

    }

    @Override
    public void mouseEntered(MouseEvent me) {

    }

    @Override
    public void mouseExited(MouseEvent me) {

    }

}
  • 发表于 2018-07-07 00:24
  • 阅读 ( 248 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除