程序一直都不画图形  找不到什么原因  求大牛指点~//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  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);
//
}求解~

解决方案 »

  1.   

    代码没粘全
    Circle类,Rectangle类没有
      

  2.   

    package jwindows;import java.awt.Color;
    import java.awt.Graphics;import javax.swing.JOptionPane;public class Rectangle extends Shape {
    int width, height;
    int dx,dy;
    //Scanner input = new Scanner(System.in);
    public Rectangle()
    {

    init();
    }
    public void init() {
    String hi = JOptionPane
    .showInputDialog("长度");
    width = Integer.parseInt(hi);
    hi = JOptionPane
    .showInputDialog("宽度");
    height = Integer.parseInt(hi);
    //System.out.println(":");
    //width = input.nextInt();
    //height = input.nextInt();
    if (width <= 0 || height <= 0) {
    width = 0;
    height = 0;
    }
    }

    public void draw(Graphics g,int dx,int dy)
    {
    g.setColor(Color.BLACK);
    g.drawRect(dx, dy, dx+this.width, dy+this.height);
    }



    }package jwindows;import java.awt.Color;
    import java.awt.Graphics;import javax.swing.JOptionPane;//import Shape.Shape;public class Circle extends Shape {
    int radius; // Scanner input = new Scanner(System.in);
    int dx, dy; public Circle() {

    init();
    } public void init() {
    String hi = JOptionPane.showInputDialog("请输入半径:");
    radius = Integer.parseInt(hi); if (radius <= 0)
    radius = 0;
    } public void draw(Graphics g,int dx,int dy) {
    g.setColor(Color.BLACK);
    g.drawOval(dx, dy, dx + this.radius, dy + this.radius);
    } }
      

  3.   

    楼主,main 函数里面的i增加了吗? 
      

  4.   

    函数里面的i没有增加,Shape[]xing没传过去,所以画不出来
      

  5.   

    那个main函数啊   都加了啊   不懂
      

  6.   

    新问题啊 
    为什么我画出来的不是圆形啊  
    我吧dx+radius 和dy+radius变成 dx+2×radius和dy+2×radius了啊