日期:2014-05-20  浏览次数:20673 次

Java panel画图程序的问题
程序一直都不画图形 找不到什么原因 求大牛指点~
Java code
//test.java
package 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.java
package 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  circle

public 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没传过去,所以画不出来