如何从GridBaglayout生成笛卡尔坐标(x,y)?

时间:2010-03-04 00:11:26

标签: java gridbaglayout coordinate cartesian

我的问题与上一篇文章有​​关 How to fill color in grid boxes randomly

如何在Gridbaglayout中获取每个颜色框填充的笛卡尔坐标(x,y)。例如,如果面板的大小是300 x 300,并且行和列设置为5 x 5,那么我们是否有任何方式可以知道坐标,而不仅仅是通过查看列和行?

http://www.freeimagehosting.net/image.php?a6ec309bd0.jpg

已编辑:完整代码

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Grid extends JPanel{

    public static void main(String[] args){
        JFrame jf=new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(new Grid());
        jf.pack();
        jf.setVisible(true);
    }

    public Grid (){
        setPreferredSize(new Dimension(300,300)); 
        setBackground(Color.BLACK);

        final Color BACKGROUND = Color.WHITE;  
        final Color[] colors = new Color[]
             {Color.BLACK, Color.CYAN, Color.MAGENTA, Color.YELLOW,
              Color.GREEN, Color.RED, Color.ORANGE, Color.BLUE, Color.PINK, Color.LIGHT_GRAY};

        final int ROWS = 5;
        final int COLS = 5;

        setLayout(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1d;
        gc.weighty = 1d;
        gc.insets = new Insets(0, 0, 1, 1);
        gc.fill = GridBagConstraints.BOTH;     

        // fill the whole panel with labels
        Label[][] label = new Label[ROWS][COLS]; 
        for( int r=0 ; r<ROWS ; r++) {
            for( int c=0 ; c<COLS ; c++) {
                Label lbl = new Label();
                lbl.setBackground(BACKGROUND);    
                gc.gridx = r;
                gc.gridy = c;
                add(lbl, gc); //add(component, constraintObj);
                label[r][c] = lbl;             
            }
        }

        // now find random fields for the colors defined in BACKGROUND
        for(Color col : colors) {
            int r, c;
            do { // make sure to find unique fields
                r = (int)Math.floor(Math.random() * ROWS);
                c = (int)Math.floor(Math.random() * COLS);
            } while(!label[r][c].getBackground().equals(BACKGROUND));
            label[r][c].setBackground(col);  
        }// end 

        int i=0;
        Component[] components = getComponents();       
        for (Component component : components) {
            i++;
            Color color = component.getBackground();
            Rectangle bounds = component.getBounds();
            System.out.println("box "+i +",coordinate= "+bounds +", Color= "+color);
        }       
    }
}

输出: 我无法获得坐标,所有坐标看起来都一样, java.awt.Rectangle中[X = 0,Y = 0,宽度= 0,高度= 0]

2 个答案:

答案 0 :(得分:0)

您可以从添加到单元格的组件中获取位置

Rectangle bounds = component.getBounds();

或者要浏览整个网格,您可以使用以下内容:

Component[] components = panel.getComponents();
for (Component component : components) {
    Color color = component.getBackground();
    Rectangle bounds = component.getBounds();
}

答案 1 :(得分:0)

怎么样:

GridBagLayout lay = new GridBagLayout(); 
setLayout(lay);
// Add components
add(comp1, constraints);
add(comp2, constraints);
//
GridBagConstraints c = lay.getConstraints(comp1);
System.out.println(c.gridx + ", " + c.gridy);

在您的情况下,请尝试使用此代替System.out.println:

GridBagConstraints gbc = ((GridBagLayout) getLayout()).getConstraints(component);
System.out.println("box " + i + ",coordinate=" + gbc.gridx + " , " + gbc.gridy);

这对我有用......

如果您需要在屏幕上显示元件位置,请确保首先看到框架。例如,当我使用它而不是main()时,它会正确打印位置:

public static void main(String[] args) {
    final JFrame jf = new JFrame();
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Grid grid = new Grid();
    jf.add(grid);
    jf.pack();
    jf.addWindowListener(new WindowAdapter() {
        public void windowActivated(WindowEvent e) {
            int i = 0;
            Component[] components = grid.getComponents();
            for (Component component : components) {
                i++;
                Color color = component.getBackground();
                Rectangle bounds = component.getBounds();
                System.out.println("box " + i + ",coordinate= " + bounds
                        + ", Color= " + color);
            }
        }
    });
    jf.setVisible(true);
}

它适用于所有情况,不仅适用于WindowListener。只需确保框架已经可见......