2010-03-07 61 views
0

协调这个问题是涉及到我刚才的问题How to generate Cartesian Coordinate (x,y) from GridBaglayout?如何获得图像的上的JPanel

我已经顺利拿到然而,每个图片的坐标,当我检查过的坐标(的System.out.println)和放置在屏幕上的图像,它似乎是错误的。例如如果在屏幕上显而易见,第一张图片的x点位于坐标为20的单元格2上,但程序显示x = 1。

下面是部分代码:

public Grid(){ 

    setPreferredSize(new Dimension(600,600)); 
    .... 
    setLayout(new GridBagLayout()); 
    GridBagConstraints gc = new GridBagConstraints(); 
    gc.weightx = 1d; 
    gc.weighty = 1d; 
    gc.insets = new Insets(0, 0, 0, 0);//top, left, bottom, and right 
    gc.fill = GridBagConstraints.BOTH; 

    JLabel[][] label = new JLabel[ROWS][COLS];   
    Random rand = new Random(); 

    // fill the panel with labels 
    for (int i=0;i<IMAGES;i++){ 
    ImageIcon icon = createImageIcon("myPics.jpg"); 
    int r, c; 
    do{ 
    //pick random cell which is empty 
    r = (int)Math.floor(Math.random() * ROWS); 
     c = (int)Math.floor(Math.random() * COLS); 
    } while (label[r][c]!=null); 

    //randomly scale the images     
    int x = rand.nextInt(50)+30; 
    int y = rand.nextInt(50)+30;     
    Image image = icon.getImage().getScaledInstance(x,y, Image.SCALE_SMOOTH); 
    icon.setImage(image); 

    JLabel lbl = new JLabel(icon); // Instantiate GUI components 
    gc.gridx = r; 
    gc.gridy = c;   
    add(lbl, gc); //add(component, constraintObj);   
    label[r][c] = lbl; 
} 

我查过这个代码的坐标:

Component[] components = getComponents();  
for (Component component : components) { 
    System.out.println(component.getBounds()); 
} 

回答

1

您可以使用SwingUtilitiesconvertPointToScreen()convertPointFromScreen()屏幕和组件坐标之间的转换。

附录:这里有一个简单的例子,我试图理解组件在布局管理器的影响下如何移动和调整大小。

public class MyPanel extends JPanel { 

    public MyPanel() { 
     super(new GridLayout(4, 4)); 
     for (int i = 0; i < 16; i++) { 
      JPanel panel = new JPanel(new GridLayout()); 
      panel.add(new CenterLabel()); 
      this.add(panel); 
     } 
    } 

    private static void create() { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new MyPanel()); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       create(); 
      } 
     }); 
    } 

    private static class CenterLabel extends JLabel { 

     public CenterLabel() { 
      this.setHorizontalAlignment(JLabel.CENTER); 
      this.setVerticalAlignment(JLabel.CENTER); 
      this.setOpaque(true); 
      this.setBackground(Color.lightGray); 
      this.setBorder(BorderFactory.createLineBorder(Color.blue)); 
      this.setPreferredSize(new Dimension(160, 100)); 
      this.addComponentListener(new ComponentAdapter() { 

       @Override 
       public void componentResized(ComponentEvent e) { 
        int w = e.getComponent().getWidth(); 
        int h = e.getComponent().getHeight(); 
        CenterLabel.this.setText("[" + w/2 + "\u253C" + h/2 + "]"); 
       } 
      }); 
     } 
    } 
} 
+0

谢谢...在我的情况下,我想它关于gridbagconstraint ...我想坐标跟随JPanel坐标不是整个屏幕,所以我重新检查它,我通过更改gc.fill = GridBagConstraints来修复它。 NONE而不是BOTH – Jessy 2010-03-07 19:01:19

1

如果在屏幕上很明显, 第一画面的x点在 小区2这是对20, 的坐标但该程序示出了X = 1。

第一个图像的x/y坐标为0/0。第二个图像的坐标为1/0。从0开始的偏移量的X/Y值。那是你在说什么?

或者是您的听众添加到图像而不是面板,在这种情况下,您需要将图像坐标转换为面板坐标。请查看我们的SwingUtilities类以获取此方法。

如果您需要更多帮助,请发送您的SSCCE