嗨,我是一个java编程的初学者,我想弄清楚如何从另一个类绘制一个形状(椭圆形)到一个java小程序(我确定它可能是一个简单的问题)
带有我要绘制的小程序的类:
import java.awt.*;
import javax.swing.JApplet;
public class PulsatingBalls extends JApplet{
private static final long serialVersionUID = 1L;
public void init(){
getContentPane().setBackground( Color.black );
new ball(20, 20);
}
}
和球类:
import java.awt.Graphics;
public class ball extends PulsatingBalls{
int x;
int y;
public ball(int y, int x){
this.x = x;
this.y = y;
repaint();
}
public void paint(Graphics g){
g.drawOval(x, y, 50, 50);
}
}
答案 0 :(得分:1)
试试这个:
小程序:
public class PulsatingBalls extends JApplet {
private static final long serialVersionUID = 1L;
private final List<Ball> balls = new ArrayList<Ball>();
@Override
public void init() {
getContentPane().setLayout(new BorderLayout());
final JPanel jp = new JPanel() {
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
for (final Ball b : balls) {
b.paint(g);
}
}
};
jp.setBackground(Color.black);
getContentPane().add(jp, BorderLayout.CENTER);
balls.add(new Ball(20, 20));
}
}
球:
public class Ball {
int x;
int y;
public Ball(final int y, final int x) {
this.x = x;
this.y = y;
}
public void paint(final Graphics g) {
g.drawOval(x, y, 50, 50);
}
}