Java GUI JComponent 绘制网格问题

未来代码 2013-06-02 05:45:40
想要绘制一个五子棋的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;
}


...全文
156 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
A18767101172 2013-06-03
  • 打赏
  • 举报
回复
干嘛不用g.drawLine();
未来代码 2013-06-03
  • 打赏
  • 举报
回复
引用 4 楼 A18767101172 的回复:
干嘛不用g.drawLine();
在下面的类里面啊。
未来代码 2013-06-03
  • 打赏
  • 举报
回复
引用 3 楼 fengmochen 的回复:
将这两句改了就好了
 DrawComponent dp1[] = new DrawComponent[19];
        DrawComponent dp2[] = new DrawComponent[19];
改为
 DrawComponent dp1[] = null;
        DrawComponent dp2[] = null;
这个不行。。。。
fengmochen 2013-06-02
  • 打赏
  • 举报
回复
将这两句改了就好了
 DrawComponent dp1[] = new DrawComponent[19];
        DrawComponent dp2[] = new DrawComponent[19];
改为
 DrawComponent dp1[] = null;
        DrawComponent dp2[] = null;
未来代码 2013-06-02
  • 打赏
  • 举报
回复
引用 1 楼 lwspace65 的回复:
楼主的意思我是真没明白,不过画五子棋棋盘还是比较简单的啊!
就是为什么加上
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]);
        }
就不能运行了,这段程序的问题在哪?
古市轩 2013-06-02
  • 打赏
  • 举报
回复
楼主的意思我是真没明白,不过画五子棋棋盘还是比较简单的啊!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧