Hi, pple im interested to know how to put a drawn 'image', a combination of a circle and a line into another class?
If i like to draw that 'image' in the void paint() method multiple times, e.g 10. How should i go about doing it?
Below is a sample of my code...
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Balloons extends Applet implements MouseListener, ActionListener
{
// The X-coordinate and Y-coordinate of the last move.
int xpos, ypos;
int xsrc, ysrc;
Color fill, outline;
Button butt1 = new Button("Start");
private Timer timer;
long time = 60000; //60 seconds
int displayTime, i;
int delay = 1000; //milliseconds
ActionListener timerAction = new ActionListener() {
long x = time - 1000;
public void actionPerformed(ActionEvent evt) {
ysrc -= 1;
x -= 1000;
i += 1;
displayTime = 60 - i;
if (x < 0) {
timer.stop();
}
repaint();
}
};
public void init()
{
fill = Color.red;
outline = Color.blue;
butt1.setBackground(Color.yellow);
add(butt1);
butt1.addActionListener(this);
/*timer start*/
timer = new Timer( delay, timerAction);
// Add the MouseListener to your applet
addMouseListener(this);
this.setBackground(Color.white);
}
public void paint(Graphics g)
{
// A circle using the oval at 150,310 and 80 pixels diameter
g.setColor(fill);
g.fillOval(10, 450+ysrc, 30, 30);
g.setColor(outline);
g.drawOval(10, 450+ysrc, 30, 30);
g.setColor(fill);
g.fillRect(25, 480+ysrc, 1, 20);
g.setColor(outline);
g.drawRect(25, 480+ysrc, 1, 20);
g.drawString("Timer: ("+displayTime+") seconds", 10, 10);
}
/*mousemoved method*/
public void mouseMoved(MouseEvent me) {}
//method called when mouse is being dragged
public void mouseDragged(MouseEvent me) {}
// This method will be called when the mouse has been clicked.
public void mouseClicked (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseEntered (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
public void actionPerformed(ActionEvent click){
timer.start();
}
}