2010-06-09 69 views
2

我創建了一個擴展JLabel的類,用作我的對象在遊戲的JPanel中移動。問題將JLabel類繪製到另一個JPanel類

import javax.swing.*; 

public class Head extends JLabel { 

int xpos; 
int ypos; 

int xvel; 
int yvel; 

ImageIcon chickie = new ImageIcon(
     "C:\\Users\\jjpotter.MSDOM1\\Pictures\\clavalle.jpg"); 
JLabel myLabel = new JLabel(chickie); 

public Head(int xpos, int ypos, int xvel, int yvel){ 

    this.xpos = xpos; 
    this.ypos = ypos; 
    this.xvel = xvel; 
    this.yvel = yvel; 
} 

public void draw(){ 
    myLabel.setLocation(xpos, ypos); 
} 

public double getXpos() { 
    return xpos; 
} 

public double getYpos() { 
    return ypos; 
} 

public int getXvel() { 
    return xvel; 
} 

public int getYvel() { 
    return yvel; 
} 

public void setPos(int x, int y){ 

    xpos = x; 
    ypos = y; 

} 

} 

我然後試圖將它添加到我的JPanel。從這裏我將隨機增加它的x和y座標以將其浮在屏幕上。我無法將它繪製到JPanel上。我知道我缺少一個關鍵概念,涉及在不同的面板上繪製組件。這裏是我在我的GamePanel類

import java.awt.Dimension; 
import java.util.Random; 
import javax.swing.*; 


public class GamePanel extends JPanel { 

Random myRand = new Random(); 
Head head = new Head(20,20,0,0); 

public GamePanel(){ 

    this.setSize(new Dimension(640, 480)); 
    this.add(head); 

} 

} 

有關如何獲得此添加到JPanel的任何建議?另外,這是一個很好的方式來讓圖片隨機在屏幕上隨意浮動遊戲嗎?

+1

重新格式化的代碼;如果不正確請回復。 – trashgod 2010-06-09 15:07:27

回答

2

首先,不需要擴展JLabel來完成此操作。

a)您設置標籤的大小,您可以通過使用圖像添加到標籤後:

label.setSize(label.getPreferredSize()); 

b)你不需要平局(),和所有的setter方法。要移動標籤的所有你要做的就是使用:

label.setLocation(...); 

C)如果你想增加你會使用類似的位置:

label.setLocation(label.getLocation()X + 5, ...);

一旦您設置了標籤的大小和位置,您可以直接將其添加到面板。請確保您已完成:

panel.setPreferredSize() 

當您將面板添加到幀的內容窗格時。

您的代碼太含糊不清,無法給出具體的建議。如果您需要更多幫助,請發送您的SSCCE。你的問題可能是佈局管理器的用法,或者你沒有使用佈局管理器。

+0

這確實使它容易得多。謝謝您的幫助 – jjpotter 2010-06-10 13:14:21

1

我認爲的主要問題是,您不會真正將圖像添加到您的構造函數中的Head

你需要做的是創建一個新的ImageIcon像你這樣做,並在你的構造函數做一些謊言這個;

public Head(int xpos, int ypos, int xvel, int yvel){ 
    // calls the JLabel constructor to create a label with an image 
    super(new ImageIcon("C:\\Users\\jjpotter.MSDOM1\\Pictures\\clavalle.jpg")) 
    this.xpos = xpos; 
    this.ypos = ypos; 
    this.xvel = xvel; 
    this.yvel = yvel; 
} 

這將創建您的Head與指定的圖像。

一旦你解決了構造函數的問題,然後你可以調用你已經添加到的JPanel對象Head的setLocation()對象。這就是你可以隨意移動的方式。

另外,在JPanel中,您要添加Head,您需要確保將LayoutManaer設置爲空,以便您可以手動將組件置於面板上。

2

是的,你應該爲你的JPanel(的GamePanel)爲null佈局管理器,告訴系統:

不要把它給我,我會做手工

編輯

我認爲如果我給你一個正在運行的演示會更清楚。

看到這個例子。作爲camickr的要點,您不必對組件進行子類化。

import javax.swing.*; 
import java.util.Timer; 
import java.util.*; 

class FloatingDemo { 
    public static void main(String [] args){ 
     // create the panel   
     JPanel panel = new JPanel(); 
     panel.setLayout(null); 

     // create the label with an image 
     final JLabel label = new JLabel(new ImageIcon("StackOverflowLogo.png")); 
     label.setSize(label.getIcon().getIconWidth(), 
         label.getIcon().getIconHeight()); 
     panel.add(label); 

     // create the frame containing both 
     JFrame frame = new JFrame(); 
     frame.add(panel); 
     frame.setSize(800, 600); 
     frame.setVisible(true); 

     // move it randomly every second 
     Timer timer = new Timer(); 
     final Random random = new Random(); 
     timer.schedule(new TimerTask() { 
      public void run(){ 
       label.setLocation(random.nextInt(800-label.getWidth()), 
            random.nextInt(600-label.getHeight()));  
      } 
     }, 0, 1000); 

    } 
} 

BTW,不設置佈局管理器也爲空的作品,但如果你調整窗口大小,JPanel的會自動設置位置你。

running demo http://img444.imageshack.us/img444/2567/capturadepantalla201006c.png