简体   繁体   English

我需要帮助,使用Jbutton ActionListener在Java中在屏幕上移动图像图标

[英]I Need help to move a Image Icon across the screen in java with a Jbutton ActionListener

i am doing a program that is like the game Unblockme, where you have to move blocks and get the red one to the other side to move to the next level. 我正在做一个类似于游戏Unblockme的程序,您必须在其中移动块并将红色的块移到另一侧才能移动到下一个级别。 Right now i load the program, click level one and then i can select the radiobutton for the Redblock and when i try to then click the move right Jbutton which in the action listener i programmed it to move, and it dosen't ive tryed for like 3 days and im still stuck. 现在我加载程序,单击一级,然后我可以为Redblock选择单选按钮,当我尝试单击右移Jbutton时,我在动作侦听器中对其进行了编程以使其移动,但我没有尝试像3天,即时消息仍然卡住。 Thanks i will include my loading pictures class that loads and draws the imageicons, and my labelframe with has the actionlisteners and everything else. 谢谢,我将包括用于加载和绘制图像图标的加载图片类,以及带有actionlistener和其他所有内容的labelframe。 the first class i posted is labelframe then the loading pictures class. 我发布的第一个类是labelframe,然后是加载图片类。

    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;

    public  class LabelFrame extends JFrame{


public LabelFrame( ) 
{
    super("The Structures Game");



    setSize(475,400);
    setLocation(350,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);



    JMenuBar menubar = new JMenuBar();
    setJMenuBar(menubar);

    JMenu Change = new JMenu("Pick a Level");
    menubar.add(Change);
        JMenu Move = new JMenu("Move keys");
    menubar.add(Move);

    JMenu Options = new JMenu("Options");
    menubar.add(Options);

    JMenu About = new JMenu("About");
    menubar.add(About);

    JMenu Select = new JMenu("Select");
    menubar.add(Select);



    final JMenuItem black = new JMenuItem("Black BackGround");
    Options.add(black);

    final JMenuItem blue = new JMenuItem("Blue BackGround");
    Options.add(blue);

    final JMenuItem yellow = new JMenuItem("Yellow BackGround");
    Options.add(yellow);

    final JMenuItem green = new JMenuItem("Green BackGround");
    Options.add(green);

    final JMenuItem white = new JMenuItem("White BackGround");
    Options.add(white);

    JMenuItem info = new JMenuItem("infomation");
    About.add(info);

    JMenuItem Level1 = new JMenuItem("Level 1");
    Change.add(Level1);


    JMenuItem Level2 = new JMenuItem("Level 2");
    Change.add(Level2);

    JMenuItem Level3 = new JMenuItem("Level 3");
    Change.add(Level3);

    JMenuItem Level4 = new JMenuItem("Level 4");
    Change.add(Level4);

    JMenuItem Level5 = new JMenuItem("Level 5");
    Change.add(Level5);
    final JRadioButton redBlock = new JRadioButton("redblock");
    final JRadioButton blockverticala = new JRadioButton("2blockvertical");
    final JRadioButton blockverticalb = new JRadioButton("4blockvertical");
    final JRadioButton blockhorizontal = new JRadioButton("2blcokhorizontal");

    final JButton moveleft = new JButton("Left");
    final JButton moveright = new JButton("Right");
    final JButton moveup = new JButton("Up");
    final JButton movedown = new JButton("Down");



    Select.add(redBlock);
    Select.add(blockverticala);
    Select.add(blockverticalb);
    Select.add(blockhorizontal);
    Move.add(moveleft);
    Move.add(moveright);
    Move.add(moveup);
    Move.add(movedown);

    Level1.addActionListener(new ActionListener()
    {   
        public void actionPerformed(ActionEvent event)
        {
            final LoadingPictures levelpics = new LoadingPictures(1);

            add(levelpics);
            repaint();


            if(redBlock.isSelected())
            {
                moveright.addActionListener(new ActionListener()
                {   
                    public void actionPerformed(ActionEvent right)
                    {
                        //if(right.getSource() = moveright)
                        LoadingPictures.top+=50;

                        levelpics.repaint();
                    }});
            }

            else if(blockverticala.isSelected())
            {
                moveright.addActionListener(new ActionListener()
                {   
                    public void actionPerformed(ActionEvent right)
                    {

                    }});
            }

            else if(blockverticalb.isSelected())
            {
                moveright.addActionListener(new ActionListener()
                {   
                    public void actionPerformed(ActionEvent right)
                    {

                    }});
            }
            if(blockhorizontal.isSelected())
            {
                moveright.addActionListener(new ActionListener()
                {   
                    public void actionPerformed(ActionEvent right)
                    {

                    }});
            }


            black.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    levelpics.setBackground(Color.black);

                }});
            blue.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    levelpics.setBackground(Color.blue);

                }});
            yellow.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    levelpics.setBackground(Color.yellow);

                }});
            green.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    levelpics.setBackground(Color.green);

                }});
            white.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    levelpics.setBackground(Color.white);

                }});


            }


    });






    info.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event)
        {   
            JOptionPane.showMessageDialog(null,"•This is our Final project for computer programming 3 \n" +
                                            "•Created by Jon Eagle and Nick Bonaventure\n•Based off of the " +
                                            "game by KIRA GAmes called UnBlock Me");
                        }});


}

} }

    import java.awt.BorderLayout;
    import java.awt.Graphics;

    import javax.swing.ImageIcon;
    import javax.swing.JPanel;


    public class LoadingPictures extends JPanel{

ImageIcon block = new ImageIcon ("2block.jpg");
ImageIcon block4vertical = new ImageIcon("4block.jpg");
ImageIcon redblock = new ImageIcon("redblockhorizontal.jpg");

static int top = 0;
static int topleft = 200;
private int j;
public LoadingPictures(int i)
{
    super();
    j = i;



}
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    if (j == 1)
    {

        g.drawImage(block4vertical.getImage(),300,200,this);
        g.drawImage(block.getImage(),200,200,this);
        g.drawImage(redblock.getImage(),top,topleft,this);
    }
}




    }

This example moves a line using buttons or the keyboard. 示例使用按钮或键盘移动一条线。 You can call drawImage() in paintComponent() instead of drawLine() . 您可以在paintComponent()调用drawImage()而不是drawLine() The essential step is to call repaint() after each change in position. 基本步骤是在每次位置更改后调用repaint()

In addition to trashgods example, you could also check out 除了垃圾桶示例,您还可以签出

While a little off topic, animation is complex topic. 动画虽然有点偏离主题,但却是一个复杂的主题。 There are number of really good open source Swing based animation libraries you could use to make your life easier... 有很多非常好的基于Swing的开源动画库,可以使您的生活更轻松...

Both are excellent and are based around a similar theory, but do different things. 两者都是优秀的,并且基于相似的理论,但是做的事情却不同。 You will have to determine if they are suitable for you needs. 您将必须确定它们是否适合您的需求。

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

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