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,请高手帮忙 展开
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,请高手帮忙 展开
1个回答
展开全部
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);
}
}
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询