鼠标点击重绘一帧?

时间:2015-04-11 19:11:01

标签: java swing

我目前正在尝试制作一款带有GUI的游戏,需要在按钮点击时在屏幕上绘制新内容。例如:

public class GUI() extends JPanel {
    public void paintComponent() {
      /*
       *Basic initial set up here
       */

      // ***** Call method here on mouse click ***** 
    }
    public void setUpGUI() {
        JFrame mainFrame = new JFrame();
        GUI paintGUI = new GUI();
        clickDetector click = new clickDetector();

        mainFrame.addMouseListener(click);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(500, 500);
        mainFrame.getContentPane().add(paintGUI);
        mainFrame.setVisible(true);
    }

    public static void main(String args[]) {
        GUI gui = new GUI();
        gui.setUpGUI();
    }
}

我需要在// ***** Call method here on mouse click *****中实现一个方法,该方法将在框架的新增内容上绘制(在我的例子中,这些是代表棋盘上的棋子的圆圈)但我不确定如何执行此操作点击按钮。如何在每次单击鼠标时重新绘制框架,以便我可以修改我的游戏板?

---- ---- EDIT

这是我的paintComponent代码,以及用于重绘的侦听器。

public void paintComponent(Graphics g) {
        super.paintComponent(g);

        System.out.println("Method called");
        g.setColor(Color.red);

        for(int y = 0; y < 6; y++) {
            for(int x = 0; x < 7; x++) {
                g.fillOval(x*70 + 10, y*70 + 10, 50, 50);
            }
        }

        g.setColor(Color.BLACK);
        g.fillRect(0, 430, 500, 50);
        g.setColor(Color.white);
        g.drawString("CONNECT FOUR", 250, 450);
        g.setColor(Color.LIGHT_GRAY);
        click.paintPiece(g);

    }

public void mouseClicked(MouseEvent e) {
        this.repaint();     

    }

这是paintComponent应该调用的方法,但不是

public void paintPiece(Graphics g) {
        int x = getMouseX() + 10;
        int y = mover.getRow() + 10;
        g.fillOval(x, y, 50, 50);
    }

3 个答案:

答案 0 :(得分:2)

只需创建一个鼠标监听器:

MouseListener listen = new MouseListener()
{
    void mouseClicked(MouseEvent e){}
    void mouseReleased(MouseEvent e){}
    void mousePressed(MouseEvent e){paintGUI.repaint();}
    void mouseExited(MouseEvent e){}
    void mouseEntered(MouseEvent e){}
};
paintGUI.addMouseListener(listen);

每当您点击JPanel内部时,您现在应该看到它重新粉刷。同样,如果您想在按下JButton时进行更新,请改用ActionListener

ActionListener listen = new ActionListener()
{
    public void actionPerformed(ActionEvent e){paintGUI.repaint();}
}
button.addActionListener(listen);

这应该放在setUpGUI()方法中。

答案 1 :(得分:0)

向按钮添加Action Event侦听器。

Oracle Docs Action Event Listener

在actionPerformed方法中添加需要添加的内容然后调用

重绘();

答案 2 :(得分:0)

此更改将允许您每次单击框架时绘制一个矩形

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI extends JPanel implements MouseListener{
    private Rectangle rect;
    private int width = 100;
    private int height = 100;

    public GUI(int x, int y, int width, int height)
    {
        rect = new Rectangle(x, y, width, height);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.draw(rect);
    }

    public void mouseClicked(MouseEvent e) {
        rect = new Rectangle(e.getX(), e.getY(), width, height);
        repaint();
    }


    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public void setUpGUI() {
        JFrame mainFrame = new JFrame();

        mainFrame.addMouseListener(this);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(500, 500);
        mainFrame.getContentPane().add(this);
        mainFrame.setVisible(true);
    }

    public static void main(String args[]) {
        GUI gui = new GUI(0,0,100,100);
        gui.setUpGUI();
    }


}

请注意MouseListener类是由GUI类实现的,当您尝试初始化框架的MouseListener时,您只需将this作为参数,会引用GUI课程,因此会引用您的JPanel