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

时间:2018-01-30 23:47:04

标签: java graphics mouselistener

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的圆圈,我意识到我没有正确获得mouseClicked坐标,我想知道我做错了什么。

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

我也不知道如何改变速度的颜色增加。

请帮帮我!!提前致谢!!

1 个答案:

答案 0 :(得分:0)

所以,我把你的代码包裹在一个框架中,点击该组件后,mouseXmouseY值将会改变

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) {

        }

    }
}