简体   繁体   中英

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.

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. In that method you have access to the actual Graphics object that is used to paint the component.

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 :

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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