如何从(0,0)坐标移动面板

时间:2015-04-09 04:11:07

标签: java user-interface border-layout

如您所见,5x5窗口与0,0坐标相邻。我想像10,10那样移动它们。我该怎么办?

enter image description here

我认为问题出在我的BorderLayout上。北部和东部是自由的。我怎样才能用10,10维填充它们

import java.awt.*;
import javax.swing.*;

public class Main {

public static void main(String[] args) {

    SOS game = new SOS(5);

    // JPanel for sos template
    SOSCanvas window = new SOSCanvas(game); 

    // JPanel which includes info panel, time and exit panel
    SOSGUIPanel info = new SOSGUIPanel(window, "Cihangir", "Mercan"); 

    JFrame jf = new JFrame();

    jf.setTitle( "SOS game");
    jf.setSize(340, 450);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setLocationRelativeTo(null);

    jf.setLayout( new BorderLayout() );
    jf.add(window, BorderLayout.CENTER ); 
    jf.add(info, BorderLayout.SOUTH );

}

}

该类提供了网格;

import java.awt.*;
import javax.swing.*;


public class SOSCanvas extends JPanel {

// PROPERTIES

protected SOS game;


// CONSTRUCTORS

public SOSCanvas( SOS game )
{
    this.game= game;                
}

@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    int dimension = game.getDimension();

    g.drawRect (0, 0, 300, 300);    

    // drawing horizontal lines
    for ( int i = 1; i < dimension; i++ )
    {
        g.drawLine(0, i * (300 / dimension), 300, i * (300 / dimension) );
    }

    // drawing vertical lines
    for ( int i = 1; i < dimension; i++ )
    {
        g.drawLine( i * (300 / dimension), 0, i * (300 / dimension), 300);
    }
}

}

1 个答案:

答案 0 :(得分:1)

您需要做的第一件事就是停止依赖幻数,您需要知道组件的当前大小。

您需要获取组件的当前宽度和高度,而不是使用g.drawRect (0, 0, 300, 300);。由此您可以决定如何处理您期望的差异以及组件的实际宽度和高度。

例如,以下是可扩展的网格,可以根据可用空间增长和缩小,但始终保持方形。它总是试图将自己置于可用空间内......

public class SOSCanvas extends JPanel {

    public SOSCanvas() {
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int dimension = 5;//game.getDimension();

        int width = getWidth() - 1;
        int height = getHeight() - 1;

        int cellSize = Math.min(width, height);
        int xOffset = (width - cellSize) / 2;
        int yOffset = (height - cellSize) / 2;

        g.drawRect(xOffset, yOffset, cellSize, cellSize);

        // drawing horizontal lines
        for (int i = 1; i < dimension; i++) {
            g.drawLine(
                            xOffset, 
                            yOffset + (i * (cellSize / dimension)), 
                            xOffset + cellSize, 
                            yOffset + (i * (cellSize / dimension)));
        }

        // drawing vertical lines
        for (int i = 1; i < dimension; i++) {
            g.drawLine(
                            xOffset + (i * (cellSize / dimension)), 
                            yOffset, 
                            xOffset + (i * (cellSize / dimension)), 
                            yOffset + cellSize);
        }
    }

}

即使你想要一个静态网格(比方说)300x300,你需要水平居中网格的x偏移量将使用类似xOffset = (getWidth() - 300) / 2的东西来计算,那么你所有的水平绘图都需要从这个偏差......