fork download
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class SunPanel extends JPanel implements ActionListener{
  5. int width = 700; int height = 300;
  6. int delay = 50;
  7. JButton starter = new JButton("Start");
  8. Timer ticker = new Timer(delay,this);
  9. int x = 0; int y = 300; int r = 50;
  10.  
  11. public SunPanel(){
  12. setPreferredSize(new Dimension(width,height));
  13. this.add(starter);
  14. starter.addActionListener(this);
  15. }
  16.  
  17. public void paintComponent(Graphics g){
  18. super.paintComponent(g);
  19. g.setColor(Color.yellow);
  20. g.fillOval(x,y,r,r);
  21. }
  22.  
  23. public void actionPerformed(ActionEvent e){
  24. if(x<250){
  25. if (e.getSource() == ticker) {
  26. x = x + 2; y = y - 2;
  27. }
  28. }
  29. if(x>=250&&x<350){
  30. x = x + 2; y = y - 1;
  31. }
  32. if(x>=350&&x<450){
  33. if (e.getSource() == ticker) {
  34. x = x + 2; y = y + 1;
  35. }
  36. }
  37. if(x>=450){
  38. if (e.getSource() == ticker) {
  39. x = x + 2; y = y + 2;
  40. }
  41. }
  42. if(e.getSource() == starter){
  43. ticker.start();
  44. }
  45. repaint();
  46. }
  47. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty