日期:2014-05-20 浏览次数:20864 次
//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);
//
}