首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Java panel画图程序的有关问题

2012-03-18 
Java panel画图程序的问题程序一直都不画图形找不到什么原因求大牛指点~Java code//test.javapackage jwin

Java panel画图程序的问题
程序一直都不画图形 找不到什么原因 求大牛指点~

Java code
//test.javapackage jwindows;import java.awt.Color;import java.awt.Graphics;import java.util.Random;import javax.swing.JPanel;public class Test extends JPanel {    int x1, x2, y1, y2;    public static Random a = new Random();    public void paintComponent(Graphics g) {        super.paintComponent(g);        //g.fillArc(x, y, width, height, startAngle, arcAngle)        g.setColor(Color.CYAN);        g.drawLine(1, 1, 30, 30);        int i = 0;        while (i <= hi.num) {            x1 = a.nextInt(this.getWidth());            y1 = a.nextInt(this.getHeight());            hi.xing[i].draw(g, x1, y1);            i++;        }    }    int type;}//hi.javapackage jwindows;import java.awt.Color;import javax.swing.JFrame;import javax.swing.JOptionPane;public class hi {    public static Shape[] xing = new Shape[10];    public static int num = 0;    public static void main(String args[]) {        int i = 0;        while (i < 10) {            String hi = JOptionPane                    .showInputDialog("Please type 1 for Circle\nPlease type 2 for Rectangle\nPlease type 0 to exit\n");            int type = Integer.parseInt(hi);            if (type == 0)                break;            switch (type) {            case 1:                xing[num] = new Circle();                break;            case 2:                xing[num] = new Rectangle();                break;            default: {                JOptionPane.showMessageDialog(null, "Error!");                num--;                break;            }            }            // xing.draw(g);            num++;        }        JFrame app = new JFrame();        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        app.setTitle("DrawBoard");        Test panel = new Test();        panel.setBackground(Color.WHITE);        app.add(panel);        app.setSize(200, 400);        app.setVisible(true);    }}//Shape.java  图形继承的基类  子类有rectangle  circlepublic abstract class Shape {    abstract public void init();    //abstract public double area();    //abstract public void display();    public abstract void draw(Graphics g,int dx,int dy);    //public abstract void ram();    Scanner input=new Scanner(System.in);//    }



求解~



[解决办法]
代码没粘全
Circle类,Rectangle类没有
[解决办法]
楼主,main 函数里面的i增加了吗?
[解决办法]
函数里面的i没有增加,Shape[]xing没传过去,所以画不出来

热点排行