I want to paint a board that looks like this:
by extending JPanel:
import javax.swing.*;
import java.awt.Graphics;
class GoBoard extends JPanel{
private int lines;
public GoBoard(){
this.lines = 9;
}
public GoBoard(int pLines){
this.lines = pLinien;
}
public void paint(Graphics g){
super.paint(g);
开发者_JAVA百科 int d = 0;
int h = 0;
for(int i = 0; i < this.lines+1; i++){
g.drawLine(0,h, getWidth(), h);
g.drawLine(h,0,h,getHeight());
h += getHeight()/this.lines;
}
}
}
For 9 lines I came up with this:
Which layout do I have to use to get the space around the board? In my example I used a box layout aligning some labels around the jpanel.
How do I have to change my paint method to get the grid you see in the first picture? It seems that I am missing the two last lines.
The reason why your grid doesn't look finished in the x direction, is that you are using the same
h += getHeight()/this.lines;
increment for each direction, but each direction is a different size. So either make the board a square, or have different increments in each direction.
精彩评论