fork(30) download
  1. import java.applet.Applet;
  2. import java.awt.Graphics;
  3. import java.awt.Color;
  4. import java.awt.Image;
  5. import java.awt.Dimension;
  6.  
  7. /* <applet code="Main.class" width="500" height="500"></applet> */
  8.  
  9. class ForDimChange {
  10. private Dimension dim;
  11. private Image buff;
  12. private Graphics ct;
  13. private int x, y;
  14. private Applet applet;
  15. ForDimChange(Applet applet) {
  16. this.applet = applet;
  17. x = y = 0;
  18. this.update();
  19. }
  20. void update() {
  21. boolean flag = false;
  22. dim = applet.getSize();
  23. if (x != dim.width) {
  24. x = dim.width;
  25. flag = true;
  26. }
  27. if (y != dim.height) {
  28. y = dim.height;
  29. flag = true;
  30. }
  31. if (flag) {
  32. buff = applet.createImage(x, y);
  33. ct = buff.getGraphics();
  34. }
  35. }
  36. int get_dimx() { return x; }
  37. int get_dimy() { return y; }
  38. Image get_buff() { return buff; }
  39. Graphics get_ct() { return ct; }
  40. }
  41.  
  42. public class Main extends Applet implements Runnable {
  43. final static int square_size = 20;
  44. final static int quantum = 5;
  45. boolean chkx = true, chky = true;
  46. int x = 250, y = 140;
  47. Thread th;
  48. ForDimChange fdc;
  49.  
  50. public void init() {
  51. fdc = new ForDimChange(this);
  52. th = new Thread(this);
  53. th.start();
  54. }
  55.  
  56. public void run() {
  57. try {
  58. while (true) {
  59. fdc.update();
  60. if (chkx) x++; else x--;
  61. if (chky) y++; else y--;
  62. if (x >= fdc.get_dimx() - square_size) chkx = false;
  63. if (x <= 0) chkx = true;
  64. if (y >= fdc.get_dimy() - square_size) chky = false;
  65. if (y <= 0) chky = true;
  66.  
  67. repaint();
  68. Thread.sleep(quantum);
  69. }
  70. } catch (InterruptedException e) { };
  71. }
  72. public void update(Graphics g) {
  73. paint(g);
  74. }
  75. public void paint(Graphics g) {
  76. ct = fdc.get_ct();
  77. ct.setColor(Color.white);
  78. ct.fillRect(0, 0, fdc.get_dimx(), fdc.get_dimy());
  79. ct.setColor(Color.red);
  80. ct.fillRect(x, y, square_size, square_size);
  81. g.drawImage(fdc.get_buff(), 0, 0, this);
  82. }
  83. }
  84. /* end */
  85.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty