开发者

Clear painted screen at each click of the mouse?

开发者 https://www.devze.com 2023-01-19 04:17 出处:网络
Can\'t seem to figure out how to only show one circle. Was trying to //g.clearRect(0, 0, 400, 400); but that clears the background too. Was also trying to just fill the background with yellow again bu

Can't seem to figure out how to only show one circle. Was trying to //g.clearRect(0, 0, 400, 400); but that clears the background too. Was also trying to just fill the background with yellow again but couldn't get that working either. Any suggestions?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JMouseFrame extends JFrame
        implements MouseListener {

    Container con = null;
    int x, y;
    int size;

    public JMouseFrame() {
        setTitle("JMouseFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con = this.getContentPane();
        addMouseListener(this);
    }

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mouseClicked(MouseEvent e) {
        int whichButton = e.getButton();
        if (whichButton == MouseEvent.BUTTON1) {
            size = 15;
        } else if (whichButton == MouseEvent.BUTTON3) {
            size = 4;
        }
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
        con.setB开发者_如何学JAVAackground(Color.yellow);
    }

    public void mouseExited(MouseEvent e) {
        con.setBackground(Color.black);
    }

    public void paint(Graphics g) {
        //g.clearRect(0, 0, 400, 400);
        g.drawOval(x - size, y - size, size * 3, size * 3);
    }

    public static void main(String[] args) {
        JMouseFrame mFrame = new JMouseFrame();
        mFrame.setSize(400, 400);
        mFrame.setVisible(true);
    }

    public void mouseReleased(MouseEvent e) {
    }
}


It looks like you're mixing AWT and Swing painting. Instead, override paintComponent(), as suggested in the example below. See Painting in AWT and Swing for details.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/questions/3898775 */
public class MousePanel extends JPanel {

    private static final int SIZE = 20;
    Point p = new Point(Short.MAX_VALUE, Short.MAX_VALUE);

    public MousePanel() {
        this.setPreferredSize(new Dimension(400, 400));
        this.setBackground(Color.yellow);
        this.addMouseListener(new MouseHandler());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawOval(p.x - SIZE, p.y - SIZE, SIZE * 2, SIZE * 2);
    }

    private final class MouseHandler extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            p = e.getPoint();
            repaint();
        }
    }

    private void display() {
        JFrame f = new JFrame("MousePanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MousePanel().display();
            }
        });
    }
}

Addendum:

I can't figure out how to make the code you posted work with what I have...

You can restore your mouse methods to the MouseHandler almost verbatim. The only difference is the need to qualify this, e.g.

    @Override
    public void mouseClicked(MouseEvent e) {
        int whichButton = e.getButton();
        if (whichButton == MouseEvent.BUTTON1) {
            MousePanel.this.size = 15;
        } else if (whichButton == MouseEvent.BUTTON3) {
            MousePanel.this.size = 4;
        }
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        MousePanel.this.setBackground(Color.yellow);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        MousePanel.this.setBackground(Color.black);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消