gpt4 book ai didi

java - 每次单击鼠标都清除绘制的屏幕?

转载 作者:行者123 更新时间:2023-11-30 09:53:18 25 4
gpt4 key购买 nike

似乎无法弄清楚如何只显示一个圆圈。试图//g.clearRect(0, 0, 400, 400);但这也清除了背景。还试图再次用黄色填充背景,但也无法正常工作。有什么建议么?

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.setBackground(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) {
}
}

最佳答案

看起来您正在混合使用 AWT 和 Swing 绘画。相反,重写 paintComponent(),如下例所示。参见 Painting in AWT and Swing了解详情。

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();
}
});
}
}

附录:

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

您可以几乎逐字地将您的鼠标方法恢复到 MouseHandler。唯一的区别是需要 qualify this ,例如

    @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);
}

关于java - 每次单击鼠标都清除绘制的屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com