2013-01-13 30 views
1

我在做一個類似於遊戲Unblockme的程序,您必須移動塊並將紅色移到另一側才能移動到下一個級別。現在我加載程序,單擊一級,然後我可以選擇Redblock的單選按鈕,當我嘗試然後單擊向右移動Jbutton,在動作偵聽器中,我將它編程爲移動,並且它不能嘗試像3天,我仍然卡住了。謝謝我將包括加載和繪製圖像圖像的加載圖片類,以及我的labelframe包含actionlistener和其他所有內容。我發佈的第一堂課是labelframe,然後是加載圖片課。我需要幫助,通過Jbutton ActionListener在java屏幕上移動圖像圖標

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




    } 
+0

您將需要查看Swing Timer上的教程並將其用於您的動畫。 –

+0

我只是在看着它,它會繼續與我的Jbutton一起工作,所以它點擊按鈕時它會移動? –

+0

爲什麼只包含ImageIcon的開銷來加載圖像?相反,使用ImageIO更直接。 – user1354999

回答

5

使用按鈕或鍵盤這example移動的線。您可以撥drawImage()paintComponent()而不是drawLine()。重要的步驟是在每次更改位置後致電repaint()

5

除了trashgods例如,你也可以檢查出

雖然有點偏離主題,動畫是複雜的話題。還有,你可以用它讓你的生活更輕鬆真正優秀的開源的基於Swing動畫庫的數量...

兩者都是優秀的,並根據各地類似的理論,但做不同的事情。您將必須確定它們是否適合您的需求。

+1

我會看看他們,感謝您的幫助。我是一名中級初學者,其中一些看起來不符合我的知識,但我會嘗試。 –

+1

只要你對Swing中的繪製過程和併發有基本的瞭解,你應該是對的 – MadProgrammer

+2

@NickB:好的。這就是你如何從初級初學者範圍中走出來的。 –