Java GUI JComponent 绘制网格问题
想要绘制一个五子棋的19 * 19 网格为什么一旦对DrawComponent使用数组就绘制不出来图像?
只有dp的时候就能用;
加上dp1,dp2的时候就不正常了???
package com.ceshi;
import java.awt.*;
import javax.swing.*;
public class drawline extends JFrame {
/**
* @param args
*/
drawline() {
setTitle("zhixian");
setSize(1000, 1000);
DrawComponent dp = new DrawComponent(100, 100 , 400 , 100);
add(dp);
DrawComponent dp1[] = new DrawComponent[19];
DrawComponent dp2[] = new DrawComponent[19];
for (int i = 1; i < 19; i++) {
dp1[i] = new DrawComponent( i * 50, 50, 19 * 50, i * 50);
dp2[i] = new DrawComponent(50, i * 50, i * 50, 19 * 50);
add(dp1[i]);
add(dp2[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
public void run() {
drawline dline = new drawline();
dline.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dline.setVisible(true);
}
});
}
}
class DrawComponent extends JComponent {
DrawComponent(int a, int b, int c, int d) {
l1 = a;
l2 = b;
h1 = c;
h2 = d;
}
public void paintComponent(Graphics g) {
Graphics g2 = (Graphics) g;
g2.drawLine(l1, l2, h1, h2);
}
private int l1, l2, h1, h2;
}
Java 网格 GUI --------------------编程问答-------------------- 楼主的意思我是真没明白,不过画五子棋棋盘还是比较简单的啊! --------------------编程问答--------------------
就是为什么加上
DrawComponent dp1[] = new DrawComponent[19];
DrawComponent dp2[] = new DrawComponent[19];
for (int i = 1; i < 19; i++) {
dp1[i] = new DrawComponent( i * 50, 50, 19 * 50, i * 50);
dp2[i] = new DrawComponent(50, i * 50, i * 50, 19 * 50);
add(dp1[i]);
add(dp2[i]);
}
就不能运行了,这段程序的问题在哪? --------------------编程问答-------------------- 将这两句改了就好了
DrawComponent dp1[] = new DrawComponent[19];
DrawComponent dp2[] = new DrawComponent[19];
改为
DrawComponent dp1[] = null;--------------------编程问答-------------------- 干嘛不用g.drawLine();
DrawComponent dp2[] = null;
--------------------编程问答--------------------
这个不行。。。。 --------------------编程问答--------------------
在下面的类里面啊。
补充:Java , Java SE