【问题标题】:mouse location and graphics painting鼠标定位和图形绘制
【发布时间】:2013-07-31 17:40:05
【问题描述】:

这个程序假设每次我在 x 坐标 300 或更大处单击屏幕时,将两个矩形向右移动 10 个点,但它没有,有什么问题?

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * To change this template use File | Settings | File Templates.
 */
public class MasterMind extends JComponent implements ActionListener,MouseListener {
    //private MouseEvent me;
    private int screenX=0;
    private int screenY=0;
    private ActionEvent e;
    private int xX=10;

    public MasterMind() throws  IOException {
    }

    public static void main(String [] args) throws IOException {
        JFrame window = new JFrame("Master Mind");
        MasterMind game= new MasterMind();
        window.add(game);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setVisible(true);
        Timer t =   new Timer(30, game);
        t.start();
        window.addMouseListener(game);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800,600);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(Color.red);
        g.drawRect(xX,30,200,200);
        g.setColor(Color.red);
        g.drawString("x,y coordinates: "+screenX+" , "+screenY,400,100);

        g.setColor(Color.blue);
        g.drawRect(xX+3, 33, 194, 194);
    }

    @Override
    public void mousePressed(MouseEvent me) {    
    }
    @Override
    public void mouseReleased(MouseEvent me) {
        repaint();
    }
    @Override
    public void mouseEntered(MouseEvent me) {
    }
    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
            MouseEvent mouseIvent = (MouseEvent) e;
            int screenX = mouseIvent.getX();
            int screenY = mouseIvent.getY();
            System.out.println("screen(X,Y) = " + screenX + "\t" + screenY);
            repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
        if (screenX>300)  {
        xX=xX+10;  }
        //timer animation

        repaint();
    }
}

我对 Java 很陌生。所以如果我可以问,请详细回答。谢谢大家。

【问题讨论】:

  • 在您的mouseClicked 方法中,您再次将screenX 声明为局部变量,隐藏了成员变量。成员变量永远不会改变;它的值总是0

标签: java swing location paintcomponent mouse-listeners


【解决方案1】:

这些行应该在mouseClicked 方法中,而不是在actionPerformed 中。

if (screenX > 300) {
    xX = xX + 10;
}

就在现有的repaint 方法之前。他们确保更新 X 坐标变量 xX 以供稍后在 paintComponent 方法中使用。

不相关但确保调用super.paintComponent(g) 作为paintComponent 的第一条语句

【讨论】:

    【解决方案2】:

    把这个,放在你的 MouseClickedMousePressedMouseReleased 方法中

    if(me.getX()>300)
                xX=xX+10;
    repaint();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2019-03-25
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多