我创建了此程序,该程序获取鼠标坐标,现在我想添加一个鼠标,该程序在按下鼠标时会在某些坐标上创建矩形。但是它行不通。这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameSetup extends JPanel implements MouseMotionListener {
public static JFrame njf = new JFrame("Test");
public static int x = 0, y = 0;
public static boolean c = false;
public static void main(String[] args) {
GameSetup gs = new GameSetup();
gs.addMouseMotionListener(gs);
njf.add(gs);
njf.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
g.setColor(Color.GREEN);
g.drawRect(150, 75, 200, 100);
g.setColor(Color.ORANGE);
g.drawString("Play", 239, 123);
if (x > 150 && y > 75 && x < 350 && y < 175){
g.drawRect(150, 75, 200, 100);
}
if(x > 150 && y > 75 && x < 350 && y < 175 && c){
g.fillRect(10 , 10 ,100 ,100);
}
}
public GameSetup() {
super();
setSize(500, 500);
njf.setSize(500,500);
njf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
njf.setResizable(false);
njf.setLocationRelativeTo(null);
}
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
if (x > 0 && y > 0) repaint(); else repaint();
}
public void mouseClicked(MouseEvent ea){
c = true;
}
}
我该如何完成这项工作?谢谢
与充满鳗鱼的气垫船说的话,我有点重构。注意:
- 实现Mouselistener或扩展Mouseadapter,并注册听众(
addMouseListener
) - 变量不需要公开或静态 - 尝试最大程度地减少范围,将更多的封装用作最佳实践
- 将
repaint()
添加到MouseClicked - 将
c
更改为clicked
,并使其在True和False之间切换,因此您可以看到它更改
尝试以下操作:
public class Game {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(new GamePanel());
frame.setVisible(true);
}
@SuppressWarnings("serial")
private static class GamePanel extends JPanel {
private int x = 0;
private int y = 0;
private boolean clicked = false;
public GamePanel() {
super();
setSize(500, 500);
setBackground(Color.BLACK);
addMouseListener(new MouseListenerImpl());
addMouseMotionListener(new MouseMotionListenerImpl());
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.drawRect(150, 75, 200, 100);
g.setColor(Color.ORANGE);
g.drawString("Play", 239, 123);
if (x > 150 && y > 75 && x < 350 && y < 175) {
g.drawRect(150, 75, 200, 100);
if (clicked) {
g.fillRect(10, 10, 100, 100);
}
}
}
private class MouseListenerImpl extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
clicked = !clicked;
repaint();
}
}
private class MouseMotionListenerImpl extends MouseMotionAdapter {
@Override
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
}
}
}
您不能将@Override
放在mouseClicked(...)
上吗?那是因为您的班级无法实现适当的接口:Mouselistener。也实现此界面,也将对象添加为Mouselistener,您将可以访问Mouselistener事件。还要考虑使用您的侦听器方法进行repaint()
调用,以便在必要时进行GUI重新粉刷。
我自己的偏爱不是让我的视图类也实现我的侦听器接口,因此,如果这是我的程序,我会创建一个扩展MouseAdapter
的内部类,并将其用作Mouselistener和MouseMotionListener。
其他问题:
- 不要在绘画方法中调用
setBackground
。而是将其称为构造函数。 - 如果可能的话,请避免"魔术"数字。
- 考虑使用jlabel或jbutton完成这项工作,因为通常最好使用更高级别的结构来轻松调试和增强。
例如没有jlabel ...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameSetup2 extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final Color BG = Color.BLACK;
private static final Color PASSIVE_COLOR = Color.GREEN;
private static final Color ACTIVE_COLOR = Color.ORANGE;
private static final String PLAY = "Play";
private static final int PLAY_X = 239;
private static final int PLAY_Y = 123;
private Rectangle rectangle = new Rectangle(150, 75, 200, 100);
private Color rectColor = PASSIVE_COLOR;
private Color playColor = PASSIVE_COLOR;
public GameSetup2() {
setBackground(BG);
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
// TODO Auto-generated constructor stub
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(playColor);
g2.drawString(PLAY, PLAY_X, PLAY_Y);
g2.setColor(rectColor);
g2.draw(rectangle);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class MyMouse extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e) {
if (rectangle.contains(e.getPoint())) {
rectColor = ACTIVE_COLOR;
} else {
rectColor = PASSIVE_COLOR;
}
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
if (rectangle.contains(e.getPoint())) {
playColor = ACTIVE_COLOR;
}
repaint();
}
public void mouseReleased(MouseEvent e) {
playColor = PASSIVE_COLOR;
repaint();
};
}
private static void createAndShowGui() {
GameSetup2 mainPanel = new GameSetup2();
JFrame frame = new JFrame("GameSetup2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}