java applet, 急需一个applet,每次点击按钮,就会出现一个随机的图形。 随机的三角形应该怎么画?

importjava.applet.Applet;importjava.awt.*;importjava.util.*;publicclassMondrianextend... import java.applet.Applet;
import java.awt.*;
import java.util.*;

public class Mondrian extends Applet {

Vector shapes = new Vector();

public void init() {
add(new Button("new shape"));
generateShape();
}

public boolean action(Event e, Object arg) {
generateShape();
repaint();
return(true);
}

public void paint(Graphics g) {
Enumeration e = shapes.elements();
Shape shape;
while (e.hasMoreElements()) { // step through all vector elements
shape = (Shape) e.nextElement();
shape.draw(g);
}
}

int randomNumber (int low, int high) {
return( low + (int) Math.round(Math.random()*(high-low)) );
}

void generateShape() {
Shape shape = null;
int randomInt = randomNumber(0,3);
switch (randomInt) {
case 0:
shape = new Ellipse();
break;
case 1:
shape = new Circle();
break;
case 2:
shape = new RectangularShape();
break;
case 3:
shape = new Square();
break;
default: // case 4
shape = new Triangle();
}
shape.randomize(); // choose random properties of shape
shapes.addElement(shape);
}
}
-----------------------------------------------------------------
import java.awt.*;

class Shape {
int x, y;
Color color;

void draw(Graphics g) {
g.setColor(color);
drawShape(g);
}

void drawShape(Graphics g) {
}

void translate(int tx, int ty) {
x = x + tx;
y = y + ty;
}

int randomNumber (int low, int high) {
return( low + (int) Math.round(Math.random()*(high-low)) );
}

void randomize() {
randomPosition();
randomSize();
randomColor();
}

void randomPosition() {
x = randomNumber(50,300);
y = randomNumber(50,300);
}

void randomSize() {
}

void randomColor() {
color = new Color( (float) Math.random(),
(float) Math.random(),
(float) Math.random() );
}
}
--------------------------------------------------------------------
import java.applet.Applet;
import java.awt.*;

class RectangularShape extends Shape {
int height, width;

RectangularShape() {
this(0,0,10,20);
}
RectangularShape(int x, int y, int height, int width) {
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
void randomSize() {
height = randomNumber(5,35);
width = randomNumber(5,35);
}

public void drawShape(Graphics g) {
g.fillRect(x, y, width, height);
}
}
---------------------------------------------

随机的三角形应该怎么画?
在第一个class里,规定一定要有一个arraylist,请高手帮忙
展开
 我来答
kehengqun
2011-05-02 · 超过41用户采纳过TA的回答
知道小有建树答主
回答量:82
采纳率:0%
帮助的人:95.7万
展开全部
class Triangle extends Shape{
Point a,b,c;
Triangle(int w,int h){
a = new Point((int)(w*Math.random()),(int)(h*Math.random()));
b = new Point((int)(w*Math.random()),(int)(h*Math.random()));
c = new Point((int)(w*Math.random()),(int)(h*Math.random()));
}
void drawShape(Graphics g){
g.drawLine(a.x,a.y,b.x,b.y);
g.drawLine(a.x,a.y,c.x,c.y);
g.drawLine(b.x,b.y,c.x,c.y);
}
}
追问
谢谢,可是我还是不会,里面显示有错误,能不能qq解决一下,我愿意把分数都给你 308454608
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式