Reputation:
Hi im a bit of a beginner to java programming and am trying to figure out how to draw a shape (oval) from another class to a java applet (I'm sure it's probably a simple problem)
the class with the applet i want to draw to:
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);
}
}
and the ball class:
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);
}
}
Upvotes: 1
Views: 238
Reputation: 2076
Try this:
Applet:
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));
}
}
Ball:
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);
}
}
Upvotes: 1