我创建了一个名为Robit的类(故意拼写错误),它使用paint(Graphics g)
方法定义了一个正方形。我正在扩展JFrame,所有这些工作正常我只是不能让正方形绘制
import java.awt.*;
public class Robit {
int[] location = new int[4];
double[] vectors = new double[2];
Color mainColor;
public Robit(Color color, int x) {
mainColor = color;
switch (x) {
case 0:
location[0] = 50;
break;
case 1:
location[0] = 700;
break;
default:
location[0] = 350;
break;
}
location[1] = 400;
location[2] = 50;
location[3] = 50;
}
public void paint(Graphics g) {
g.setColor(mainColor);
g.fillRect(location[0], location[1], location[2], location[3]);
}
public int getX() {
return location[0];
}
public int getY() {
return location[1];
}
}
`这就是我实例化的类,继承人在哪里实现它 import java.awt.Color; import javax.swing。*;
public class Frame extends JFrame {
public Frame(){
setLayout(null);
Robit r1 = new Robit(Color.red, 0);
Robit r2 = new Robit(Color.blue, 1);
}
}
和主要课程
import javax.swing.JFrame;
public class App {
public static void main(String[] args) {
Frame f = new Frame();
f.setSize(800, 450);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
答案 0 :(得分:0)
您没有在paint
课程中调用Robit
方法。请尝试以下方法:
public class Frame extends JFrame {
protected Robit r1;
protected Robit r2;
public Frame(){
setLayout(null);
r1 = new Robit(Color.red, 0);
r2 = new Robit(Color.blue, 1);
}
public void paint(Graphics g)
{
super.paint(g);
r1.paint(g);
r2.paint(g);
}
}