简体   繁体   English

使用按钮和鼠标侦听器移动图形对象

[英]Moving graphic object with button and mouse listener

Need little help, I am trying to make a simple game for my school project but I can not make my tanks move. 需要一点帮助,我正在尝试为我的学校项目制作一个简单的游戏,但是我无法移动坦克。

Is it even possible to make a graphic object move using only buttons? 甚至可以仅通过按钮移动图形对象?

I know this code is awful and lots of things could have been done easier and shorter, but we have to make it mostly from what we learned... 我知道这段代码很糟糕,许多事情本来可以更轻松,更短地完成,但是我们必须主要根据我们学到的知识来做...

I have tried it using mouse Listener, but so far I could not do it. 我已经使用鼠标侦听器尝试过它,但是到目前为止我还做不到。 Here is my code. 这是我的代码。

public class Hra extends JPanel{
public int zakladc=1300;
public int zakladm=35;

public int GetZakladc(){
    return zakladc;
}

public int GetZakladm(){
    return zakladm;
}

    Pozadi();
    tank_cerveny();
    tank_modry();

    JTextField uhel_text=new JTextField(3);

    JTextField sila_text=new JTextField(3);

    JButton prava=new JButton("VPRAVO");           
    add(prava);
    class obsluha_prava implements MouseListener{
        public void mouseClicked(MouseEvent e) {
            if (natahu==0){
                zakladc=zakladc+1;


                System.out.println(zakladc);
            }
        }
        public void mousePressed(MouseEvent e) {

        }
        public void mouseReleased(MouseEvent e) {

        }
        public void mouseEntered(MouseEvent e) {

        }
        public void mouseExited(MouseEvent e) {

        }
    }

    JButton leva=new JButton("VLEVO");                
    add(leva);
    class obsluha_leva implements MouseListener{
        public void mouseClicked(MouseEvent e) {

        }
        public void mousePressed(MouseEvent e) {

        }
        public void mouseReleased(MouseEvent e) {

        }
        public void mouseEntered(MouseEvent e) {

        }
        public void mouseExited(MouseEvent e) {

        }
    }

}
public void Pozadi(){
Graphics g = img.getGraphics();
Color pisek=new Color(242,197,102);
    g.setColor(pisek);
    g.fillRect(0,665,1368,100);
}

public void tank_cerveny(){
    Graphics g = img.getGraphics();
    g.setColor(Color.BLACK);
    g.fillOval(zakladc,y-13,40,13);
    g.fillRect(zakladc-10,y-2*13+6,15,4);
    g.setColor(Color.RED);
    g.fillRect(zakladc+5,y-2*13+1,30,13);
}

public void tank_modry(){
    Graphics g = img.getGraphics();
    g.setColor(Color.BLACK);
    g.fillOval(zakladm,y-13,40,13);
    g.fillRect(zakladm+35,y-2*13+6,15,4);
    g.setColor(Color.BLUE);
    g.fillRect(zakladm+5,y-2*13+1,30,13);
}

The way it should work is, that you would press button "VPRAVO" and one tank would move to the right or "VLEVO" and the tank would move to the left. 它的工作方式是,您将按下按钮“ VPRAVO”,一个储罐将向右移动,或“ VLEVO”而储罐将向左移动。

Yes it is totally possible , here is a simplified example. 是的,这是完全可能的,这是一个简化的示例。

When you want to paint something on a component, you do that inside its overriden paintComponent method. 当您想在组件上绘画时,可以在其重写的paintComponent方法内进行。 In that method you have access to the actual Graphics object that is used to paint the component. 在该方法中,您可以访问用于绘制组件的实际Graphics对象。

Add an ActionListener to each button that will be called on click, adapt the direction and coordinates depending on the button, then call repaint() which will force the component to repaint itself using the new values : 向将在单击时调用的每个按钮添加一个ActionListener ,根据按钮调整方向和坐标,然后调用repaint() ,这将强制组件使用新值重新绘制自身:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Hra extends JPanel {

    private int x = 130;
    private int y = 100;

    private boolean moveRight = false;

    public Hra() {

        JButton prava = new JButton("VPRAVO");

        prava.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {

                moveRight = true;
                x = x - 5;
                repaint();

            }
        });
        add(prava);

        JButton leva = new JButton("VLEVO");

        leva.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {

                moveRight = false;
                x = x + 5;
                repaint();

            }
        });
        add(leva);

    }

    @Override
    public void paintComponent(final Graphics g) {

        super.paintComponent(g);

        if (moveRight) {

            tank_cerveny(g);
        } else {

            tank_modry(g);
        }
    }

    public void tank_cerveny(final Graphics g) {

        g.setColor(Color.BLACK);
        g.fillOval(x, y - 13, 40, 13);
        g.fillRect(x - 10, y - 2 * 13 + 6, 15, 4);
        g.setColor(Color.RED);
        g.fillRect(x + 5, y - 2 * 13 + 1, 30, 13);
    }

    public void tank_modry(final Graphics g) {

        g.setColor(Color.BLACK);
        g.fillOval(x, y - 13, 40, 13);
        g.fillRect(x + 35, y - 2 * 13 + 6, 15, 4);
        g.setColor(Color.BLUE);
        g.fillRect(x + 5, y - 2 * 13 + 1, 30, 13);
    }

    public static void main(final String[] args) {

        Hra hra = new Hra();

        JFrame frame = new JFrame();

        frame.setContentPane(hra);

        frame.setSize(400, 200);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM