单击时尝试定位鼠标坐标,但始终显示为 0,0


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
public class GameWindow extends JComponent implements ActionListener,     MouseListener{
private Timer t = new Timer(5, this);
private int scorenum = 0;
public  int mouseX, mouseY, 
ranX = ((int)(Math.random()*5))*200,
ranY = (((int)(Math.random()*4)+1))*200;
int[] cy = new int[] {200, 400, 600, 800}, cx= new int[] {0, 200, 400, 600, 800};
private JLabel score = new JLabel("score: " + scorenum, SwingConstants.CENTER);
private Color c1 = Color.RED, c2 = Color.BLUE;
public GameWindow() //constructor
{
setLayout(new BorderLayout());
score.setPreferredSize(new Dimension(1015,200));
add(score, BorderLayout.NORTH);
score.setFont(new Font("Cooper Black", Font.PLAIN, 50));
score.setBackground(Color.WHITE);
t.start();
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
setFocusable(true);
this.requestFocusInWindow();
for (int x = 0; x <= 4; x ++) 
{
for (int y = 0; y <= 3; y ++) 
{
g.setColor(c1);
g.fillOval(cx[x], cy[y], 200, 200);
}
}   
g.setColor(c2);
g.fillOval(ranX, ranY, 200, 200);
g.fillOval(mouseX, mouseY, 200, 200);

}
@Override
public void actionPerformed(ActionEvent e) {
Ellipse2D rancircle = new Ellipse2D.Double(ranX,ranY,200,200);
if (rancircle.contains(mouseX, mouseY))
{
c2 = c1;
ranX = ((int)(Math.random()*5))*200;
ranY = (((int)(Math.random()*4)+1))*200;
}  
}
@Override
public void mouseClicked(MouseEvent m) {
mouseX=m.getX();
mouseY=m.getY();
}
@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 m) {
}
@Override
public void mouseReleased(MouseEvent m) {
}

}

我正在尝试制作一个游戏,玩家点击从 c1 更改为 c2 的圆圈,通过点击,颜色从 c2 回到 c1。越来越多的圆圈应该从c1变为c2,如果玩家点击的速度没有超过颜色变化的速度(当所有圆圈变成c2时),玩家就输了。

通过用坐标mouseX,mouseY画一个圆圈,我意识到我没有正确获得鼠标点击的坐标,我想知道我做错了什么。

另外,我是一个完全的初学者,所以任何关于如何制作这个游戏的建议都会非常有帮助。

我也不知道我怎样才能使颜色的变化速度增加。

请帮帮我!!提前感谢!!

因此,我获取了您的代码,将其包装在一个框架中,单击组件后,mouseX值和mouseY值将更改

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GameWindow());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GameWindow extends JComponent implements ActionListener, MouseListener {
private Timer t = new Timer(5, this);
private int scorenum = 0;
public int mouseX, mouseY,
ranX = ((int) (Math.random() * 5)) * 200,
ranY = (((int) (Math.random() * 4) + 1)) * 200;
int[] cy = new int[]{200, 400, 600, 800}, cx = new int[]{0, 200, 400, 600, 800};
private JLabel score = new JLabel("score: " + scorenum, SwingConstants.CENTER);
private Color c1 = Color.RED, c2 = Color.BLUE;
public GameWindow() //constructor
{
setLayout(new BorderLayout());
score.setPreferredSize(new Dimension(1015, 200));
add(score, BorderLayout.NORTH);
score.setFont(new Font("Cooper Black", Font.PLAIN, 50));
score.setBackground(Color.WHITE);
t.start();
addMouseListener(this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
setFocusable(true);
this.requestFocusInWindow();
for (int x = 0; x <= 4; x++) {
for (int y = 0; y <= 3; y++) {
g.setColor(c1);
g.fillOval(cx[x], cy[y], 200, 200);
}
}
g.setColor(c2);
g.fillOval(ranX, ranY, 200, 200);
g.fillOval(mouseX, mouseY, 200, 200);
}
@Override
public void actionPerformed(ActionEvent e) {
Ellipse2D rancircle = new Ellipse2D.Double(ranX, ranY, 200, 200);
System.out.println(mouseX + "x" + mouseY);
if (rancircle.contains(mouseX, mouseY)) {
c2 = c1;
ranX = ((int) (Math.random() * 5)) * 200;
ranY = (((int) (Math.random() * 4) + 1)) * 200;
}
}
@Override
public void mouseClicked(MouseEvent m) {
mouseX = m.getX();
mouseY = m.getY();
}
@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 m) {
}
@Override
public void mouseReleased(MouseEvent m) {
}
}
}

相关内容

  • 没有找到相关文章

最新更新