2010-05-30 52 views
0

我試圖開發使用TableLayout Java中的地圖編輯器的另一個JPanel的。我的地圖窗口接收作爲構造函數一個地圖對象。從該地圖對象中,我可以檢索網格以及網格中的每個項目以及其他獲取者和設置者。 問題是,即使Mapping擴展JComponent,當我將它放在面板中時,它也不會被繪製。我已經重寫了paint方法來滿足我的需要。這裏是代碼,也許你可以幫助我。添加一個JPanel具有TableLayout

public class MapTest extends JFrame implements ActionListener { 

    private JPanel mainPanel; 
    private JPanel mapPanel; 
    private JPanel minimapPanel; 
    private JPanel relationPanel; 
    private TableLayout tableLayout; 
    private JPanel tile; 

    MapTest(Map map) { 
     mainPanel = (JPanel) getContentPane(); 
     mapPanel = new JPanel(); 
     populateMapPanel(map); 
     mainPanel.add(mapPanel); 
     this.setPreferredSize(new Dimension(800, 600)); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
    } 

    private double[][] generateTableLayoutSize(int x, int y, int size) { 
     double panelSize[][] = new double[x][y]; 
     for (int i = 0; i < x; i++) { 
      for (int j = 0; j < y; j++) { 
       panelSize[i][j] = size; 
      } 
     } 
     return panelSize; 
    } 

    private void populateMapPanel(Map map) { 
     double[][] layoutSize = generateTableLayoutSize(map.getMapGrid().getRows(), map.getMapGrid().getColumns(), 50); 
     tableLayout = new TableLayout(layoutSize); 

     for(int i = 0; i < map.getMapGrid().getRows(); i++) 
     { 
      for(int j = 0; j < map.getMapGrid().getColumns(); j++) 
      { 
       tile = new JPanel(); 
       tile.setName(String.valueOf(((Mapping)map.getMapGrid().getItem(i, j)).getCharacter())); 
       tile.add(map.getMapItem(i, j)); 
       String constraint = i + "," + j; 
       mapPanel.add(tile, constraint); 
      } 
     } 
     mapPanel.validate(); 
     mapPanel.repaint(); 
    } 

    public void actionPerformed(ActionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
} 

我的映射類

public class Mapping extends JComponent implements Serializable{ 

    private BufferedImage image; 
    private Character character; 

    //default 
    public Mapping() { 
     super(); 
     this.image = null; 
     this.character = '\u0000'; 
    } 

    //Mapping from image and char 
    public Mapping(BufferedImage image, char character) { 
     super(); 
     this.image = image; 
     this.character = character; 
    } 

    //Mapping from file and char 
    public Mapping(File file, char character) { 
     try { 
      this.image = ImageIO.read(file); 
      this.character = character; 
     } catch (IOException ex) { 
      System.out.println(ex); 
     } 
    } 

    public char getCharacter() { 
     return character; 
    } 

    public void setCharacter(char character) { 
     this.character = character; 
    } 

    public BufferedImage getImage() { 
     return image; 
    } 

    public void setImage(BufferedImage image) { 
     this.image = image; 
     repaint(); 
    } 

    @Override 
    /*Two mappings are consider the same if 
    -they have the same image OR 
    -they have the same character OR 
    -both of the above*/ 
    public boolean equals(Object mapping) { 
     if (this == mapping) { 
      return true; 
     } 
     if (mapping instanceof Mapping) { 
      return true; 
     } 
     //WARNING! equals might not work for images 
     return (this.getImage()).equals(((Mapping) mapping).getImage()) 
       || (this.getCharacter()) == (((Mapping) mapping).getCharacter()); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     //g.drawImage(image, 0, 0, null); 
     g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null); 
    } 

// @Override 
// public Dimension getPreferredSize() { 
//  if (image == null) { 
//   return new Dimension(10, 10); //instead of 100,100 set any prefered dimentions 
//  } else { 
//   return new Dimension(100, 100);//(image.getWidth(null), image.getHeight(null)); 
//  } 
// } 
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { 
     character = (Character) in.readObject(); 
     image = ImageIO.read(ImageIO.createImageInputStream(in)); 
    } 

    private void writeObject(java.io.ObjectOutputStream out) throws IOException { 
     out.writeObject(character); 
     ImageWriter writer = (ImageWriter) ImageIO.getImageWritersBySuffix("jpg").next(); 
     writer.setOutput(ImageIO.createImageOutputStream(out)); 
     ImageWriteParam param = writer.getDefaultWriteParam(); 
     param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 
     param.setCompressionQuality(0.85f); 
     writer.write(null, new IIOImage(image, null, null), param); 
    } 
} 
+0

您是否嘗試過不重寫paint方法以查看它是否被渲染? – Marc 2010-05-30 14:53:45

+0

同樣的事情。我甚至無法在普通的JPanel中繪製它。所以我猜測映射類是搞砸了。 – user253530 2010-05-30 15:11:47

+1

添加鏈接;如果不正確請回復。 – trashgod 2010-05-30 16:31:16

回答

1

缺省情況下一個JComponent並沒有優選的大小。所以當你把它添加到另一個面板時,沒有什麼可以繪製的。

調用的drawImage()方法不給一個大小到組件。

我不知道TableLayout是如何工作的,但我會嘗試設置您的組件的首選尺寸,然後我猜TableLayout將能夠正常工作。

或者,也許你應該使用網格佈局,它會自動調整基礎上的總空間每個網格。

相關問題