0
我是新來的Java圖形,我需要一些幫助。我正在嘗試爲遊戲kakuro創建一個gui。在這裏你可以看到董事會如何看起來像http://en.wikipedia.org/wiki/Kakuro的圖像。我已經有一些代碼來創建矩形的網格,但我不知道如何填充矩形中心的數字,最重要的是,我怎樣才能將一些矩形對角地分割成一行需要放置的線兩個不同的數字在這些例子中?kakuro gui佈局網格與矩形
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class Grid extends JComponent {
public void paint(Graphics g) {
int width=30;
int height=30;
for(int x=0;x<10;x++)
{
for(int y=0 ;y < 10;y++)
{
// create rectangles
g.drawRect(x*width,y*height,width,height);
// Fill in values. However, how to make it in the centre?
g.drawString("2", x*width,y*height);
}
}
}
}
public class Cube {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setSize(200,200);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(new Grid());
window.setVisible(true);
}
}