2013-10-29 25 views
0

我無法在我的花園網格面板頂部顯示動畫。無論我最後添加到顯示的內容窗格。無論如何要同時顯示多個組件。我試過切換類動畫和花園網格來擴展jpanel,但沒有任何工作。動畫是一系列圖像,顯示用鼠標拖動時移動的角色。我需要這個角色在花園網格面板上移動。任何幫助?使用jframe的多個jComponents動畫

package view; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.GridBagLayout; 
import java.awt.Panel; 

import javax.swing.JFrame; 
import javax.swing.JLayeredPane; 


public class Driver { 
public static void main(String[] args) { 

    JFrame frame = new JFrame(); 
    JLayeredPane pane = new JLayeredPane(); 
    frame.add(pane); 

    Animation animation = new Animation(); 
    GardenPanel garden = new GardenPanel(6,4,600,800); 


    pane.add(animation,new Integer(1)); 

    pane.add(garden, new Integer(0)); 
    frame.setSize(800, 600); 


    System.out.println(pane.highestLayer());// just to check I dont have hidden layers 







    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

    while(true){ 
     frame.repaint(); 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

}

}

--------動畫類-----------

你應該會看到一個移動的獸人。你可以用你的鼠標拖動這個獸人。如果按下 *鼠標而不移動它,功率會增加。 * */

package view; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLayeredPane; 
import javax.swing.JPanel; 

public class Animation extends JComponent implements MouseMotionListener, MouseListener{ 
    final int frameCount = 10; 
    int picNum = 0; 
    BufferedImage[][] pics; 
    int xloc = 0; 
    int yloc = 0; 
    final int xIncr = 8; 
    final int yIncr = 2; 
    final static int frameWidth = 900; 
    final static int frameHeight = 600; 
    final static int imgWidth = 165; 
    final static int imgHeight = 165; 
    //basic info about the orc 

    BufferedImage seedImage; 
    int mouseX; 
    int mouseY; 
    int seedX = xloc; 
    int seedY = yloc; 
    boolean mouseholding; 
    boolean moving = false; 
    int power; 
    public enum stage { 
     MOVE, POWER 
    } 
    stage s; 

    //Override this JPanel's paint method to cycle through picture array and draw images 
    public void paint(Graphics g) { 
     picNum = (picNum + 1) % frameCount; 
     if(mouseholding && (s == s.POWER)){ 
      System.out.println(power++); 
     } 


     g.drawImage(pics[0][picNum], xloc, yloc, Color.gray, this); 
     g.drawImage(seedImage, seedX, seedY, imgWidth/8, imgHeight/8, this); 
    } 


    //Constructor: get image, segment and store in array 
    public Animation(){ 
     seedImage = createImage(); 
     BufferedImage[] img = createAnimation(); 
     pics = new BufferedImage[img.length][10]; 
     for(int j = 0; j < img.length; j++){ 
      for(int i = 0; i < frameCount; i++) 
       pics[j][i] = img[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight); 
     } 
     addMouseMotionListener(this); 
     addMouseListener(this); 
    } 

    //Read image from file and return 
    private BufferedImage[] createAnimation(){ 
     BufferedImage[] bufferedImage = new BufferedImage[4]; 
     try { 
      bufferedImage[0] = ImageIO.read(new File("images/orc_forward_southeast.png")); 
      bufferedImage[1] = ImageIO.read(new File("images/orc_forward_southwest.png")); 
      bufferedImage[2] = ImageIO.read(new File("images/orc_forward_northeast.png")); 
      bufferedImage[3] = ImageIO.read(new File("images/orc_forward_northwest.png")); 
      return bufferedImage; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private BufferedImage createImage(){ 
     BufferedImage bufferedImage; 
     try { 
      bufferedImage = ImageIO.read(new File("images/seed.png")); 
      return bufferedImage; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 

     // TODO: Change this method so you can load other orc animation bitmaps 
    } 

    // implement the method in mouseListener and mouseMotionListener interface 
    @Override 
    public void mouseDragged(MouseEvent event) { 
     mouseX = event.getX(); 
     mouseY = event.getY(); 
     if((xloc<mouseX) && (xloc+imgWidth >mouseX) && (yloc<mouseY) && (yloc+imgHeight>mouseY) && (s==stage.MOVE)){ 

      //System.out.println("Imagecoor:("+xloc+", "+yloc+")"+" mousecoor:("+mouseX+","+mouseY+")"); 
      // you can print the coordinate if you want  
      xloc = mouseX-imgWidth/2; 
      yloc = mouseY-imgHeight/2; 
      seedX = mouseX; 
      seedY = mouseY; 
     } 
    } // draging the image 
    @Override 
    public void mouseMoved(MouseEvent arg0) { 
    } 
    @Override 
    public void mouseClicked(MouseEvent e) { 
    } 
    @Override 
    public void mouseEntered(MouseEvent e) {  
    } 
    @Override 
    public void mouseExited(MouseEvent e) { 
    } 

    // for increasing the power. 
    @Override 
    public void mousePressed(MouseEvent event) { 
     mouseholding = true; 
     mouseX = event.getX(); 
     mouseY = event.getY(); 
     if((xloc == mouseX-imgWidth/2) && (yloc == mouseY-imgHeight/2)){ 
      s = stage.POWER; 
     } 
    } 
    @Override 
    public void mouseReleased(MouseEvent event) { 
     mouseholding = false; 
     s = stage.MOVE; 
     power = 0; 
    } 
} 

-------- GardenPanel類------------ 應顯示2條黑線

package view; 

import java.awt.Graphics; 

import javax.swing.JComponent; 
import javax.swing.JLayeredPane; 
import javax.swing.JPanel; 

public class GardenPanel extends JComponent { 
    private int numOfRows; 
    private int numOfColumns; 
    private int frameWidth; 
    private int frameHeight; 

    public GardenPanel(int numOfRows, int numOfColumns, int frameHeight, int frameWidth){ 
     this.numOfRows = numOfRows; 
     this.numOfColumns = numOfColumns; 
     this.frameHeight = frameHeight; 
     this.frameWidth = frameWidth; 


    } 
    int secondX = ((frameWidth -((frameWidth/3) *2))/4) * 3; 

    @Override 
    public void paint(Graphics g){ 
     g.drawLine((frameWidth/3) * 2, (frameHeight/4) * 3, (frameWidth/3 * 2) - 30, frameHeight/4); 

     g.drawLine(((frameWidth/3) * 2) + 200, (frameHeight/4) * 3, ((frameWidth -((frameWidth/3) *2)/4) * 3) - 30, frameHeight/4); 



    } 

} 
+0

「Animation」和「GardenPanel」類是什麼樣的? –

+0

Idk你的項目是什麼,但它聽起來像揮杆可能不是這種情況下的API ... –

回答

2

幽州:

我無法在我的花園網格面板上顯示動畫。無論我最後添加到顯示的內容窗格。

JFrame的contentPane使用BorderLayout,這就是BorderLayouts的工作原理。如果你默認添加一個組件(沒有int 2nd參數),它默認放置在BorderLayout.CENTER位置,並覆蓋之前添加的任何東西。您需要閱讀Swing佈局管理器教程以獲取更多詳細信息。

有無論如何顯示在同一時間多個組件。

是的,使用不同的佈局管理器和組件。如果您希望組件相互重疊,請考慮使用JLayeredPane。

我試過切換類動畫和花園網格來擴展jpanel,但沒有任何工作。動畫是一系列圖像,顯示用鼠標拖動時移動的角色。我需要這個角色在花園網格面板上移動。任何幫助?

不知道你確切的問題在這裏。

1

有兩種方法可以使動畫坐在花園面板之上。然而,正如「Hovercraft Full Of Eels」所指出的,你如何使用「GridBagLayout」存在問題。

當使用動畫時,我傾向於使用絕對定位,這種方式很容易手動指定組件的確切位置,並且可以確保一切都可見。我通過將jLayeredPane/jPanel添加到jFrame並向它們添加組件而不是jFrame來做到這一點。

這裏有兩種方法可以使動畫坐在GardenPanel的頂部。

1)以不同的結果以不同的順序添加組件。試一試。

2)一旦所有的組件都被添加到「contentPane的」你可以設置組件的你想在頂部的Z順序,這樣的事情:

//Create this object first so we can set the Z-Order later 
Animation animation = new Animation() 
frame.getContentPane().add(animation); //use object above, not a "new" Animation 
frame.getContentPane().add(new GardenPanel(6,4,600,800)); 
//Do something like this after all other components have been added to the ContentPane. 
frame.getContentPane().setComponentZOrder(animation, 0); 

編輯: 從你已經證明你有基本的東西,所以這裏是一個自包含的例子,你可以編譯,我相信你可以演示你想要做的事情。

animation用紅色框表示,GardenPanel用藍色表示。

請注意:

  1. 我已經添加組件到pane的順序。第一個:pane.add(animation);然後:pane.add(GardenPanel);
  2. 我通過使用JLabel而不是您自定義的Animation組件和JLayeredPane而不是GardenPanel組件,保持此示例非常簡單,您應該可以將它們都交換出來。
  3. 至少在測試時刪除Drive類中的while循環。一旦您知道兩個組件都正確顯示,您可以稍後再解決重繪問題。

    import java.awt.Point; 
    import javax.swing.JFrame; 
    import javax.swing.JLayeredPane; 
    import javax.swing.JLabel; 
    
    public class Driver 
    { 
    //components 
    static JFrame frame = new JFrame(); 
    static JLayeredPane pane = new JLayeredPane(); 
    static JLabel animation = new JLabel(); 
    static JLayeredPane GardenPanel = new JLayeredPane(); 
    
    //variables 
    static Point startPos = new Point(0, 0); 
    static Point draggedPos = new Point(0, 0); 
    
    public static void main(String[] args) 
    { 
    //setup components 
    frame.setSize(800, 600); 
    pane.setSize(frame.getSize()); 
        pane.setBackground(java.awt.Color.GRAY); 
        pane.setOpaque(true); 
    
    animation.setSize(100, 150); 
    animation.setLocation((pane.getWidth() - animation.getWidth())/2, (pane.getHeight() - animation.getHeight())/2); 
    animation.setOpaque(true); 
    animation.setBackground(java.awt.Color.RED); 
    animation.addMouseListener(new java.awt.event.MouseAdapter() 
    { 
        @Override 
        public void mousePressed(java.awt.event.MouseEvent evt) 
        { 
        startPos = evt.getLocationOnScreen(); 
        } 
    }); 
    animation.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() 
    { 
        @Override 
        public void mouseDragged(java.awt.event.MouseEvent evt) 
        { 
        draggedPos = evt.getLocationOnScreen(); 
        animation.setLocation(animation.getX() + (draggedPos.x - startPos.x), animation.getY() + (draggedPos.y - startPos.y)); 
        startPos = evt.getLocationOnScreen(); 
        } 
    }); 
    
    GardenPanel.setSize(800, 600); 
    GardenPanel.setLocation(0, 0); 
    GardenPanel.setOpaque(true); 
    GardenPanel.setBackground(java.awt.Color.BLUE); 
    
    //add components 
    frame.add(pane); 
    //The order of the following will get the desired results, I do not specify Z-Order, I simply add the top component first 
    pane.add(animation); 
    pane.add(GardenPanel); 
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    } 
    

    }

使用此示例代碼,你可以適應可能你原來的代碼遵循相似的路徑。

+0

我試過這個,但我仍然得到一個空白幀。我已經更新了該操作,以包含我迄今爲止的代碼。我遵循你的建議,並添加了jlayeredpane到框架,然後只添加組件到分層面板,但仍然沒有運氣。 – user2510809

+0

1)在你的更新代碼中,'GardenPanel'仍然在動畫的頂部,按照Z順序,0處的組件將始終是頂層組件,將其更改爲'pane.add(garden); pane.add(動畫,0);'。 2)請包括您的代碼爲'動畫'和'GardenPanel'類,否則我們不能正確提供指針。 3)這個班級如何使用/啓動?我問這是因爲'Thread.sleep'方法會鎖定它的線程,並且會變得無法響應。 – sorifiend

+0

我嘗試了你的建議,但仍然沒有運氣。我包括動畫和花園班級。我知道這個動畫課很長,所以我最初並沒有包含它。我將在稍後修復線程代碼,但現在我只關注讓這兩個組件出現在jframe上。 – user2510809