您好,经过一番困惑后,我创建了以下代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tri extends RandomShape{
public void paintComponent (Graphics g){
// create random variables for collor and shape of triangle
int x;
int y;
int x2;
int y2;
x = (int) Math.random()*100;
y = (int) Math.random()*100;
x2 = (int) Math.random()*100;
y2 = (int) Math.random()*100;
int r1;
int g1;
int b1;
r1 = (int) Math.random()*255;
g1 = (int) Math.random()*255;
b1 = (int) Math.random()*255;
Color color = new Color(r1,g1,b1);
//draw triangle
g.setColor(color);
g.drawLine(x,y,y2,y);
g.setColor(color);
g.drawLine(x,y,y2,y2);
g.setColor(color);
g.drawLine(y2,y,y2,y2);
}
}
和我的随机形状代码
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
*
* @author huub
*/
abstract class RandomShape {
/** color used for drawing this shape **/
protected Color color;
/** position of the shape (upper left corner) **/
protected int x, y;
abstract void draw(Graphics g);
}
但是,当我尝试编译时,它给出了一个错误,即 Tri 不是抽象的,并且不会覆盖 Randomshape 中的抽象方法绘制我该如何解决这个问题