| 1 | package SinusDiff; |
| 2 | |
| 3 | import static java.lang.Math.*; |
| 4 | import java.util.logging.Level; |
| 5 | import java.util.logging.Logger; |
| 6 | import javax.swing.*; |
| 7 | import java.awt.*; |
| 8 | |
| 9 | /** |
| 10 | * This example shows how to create GUI application which can be run both |
| 11 | * as HTML embeded web applet and as standalone application |
| 12 | * @author harvie |
| 13 | */ |
| 14 | class AppGui extends JPanel implements Runnable { |
| 15 | |
| 16 | double offA=0,offB=0, |
| 17 | speedA=50,speedB=-55, |
| 18 | freqA=25,freqB=35; |
| 19 | |
| 20 | @Override |
| 21 | public void paint(Graphics g) { |
| 22 | /* |
| 23 | Graphics2D g2D = (Graphics2D) g; // cast to 2D |
| 24 | g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 25 | RenderingHints.VALUE_ANTIALIAS_ON); |
| 26 | */ |
| 27 | |
| 28 | g.setColor(Color.BLACK); |
| 29 | g.fillRect(0, 0, getWidth(), getHeight()); |
| 30 | g.setColor(Color.GREEN); |
| 31 | //g.setXORMode(Color.GREEN); |
| 32 | |
| 33 | int mid = round(getHeight()/2); |
| 34 | int y1,y2; |
| 35 | for(int x = 0;x<=getWidth();x++) { |
| 36 | y1=(int)round(mid+mid*sin((double)(x+offA)/freqA)); |
| 37 | y2=(int)round(mid+mid*sin((double)(x+offB)/freqB)); |
| 38 | g.drawLine(x,y1,x,y2); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | int fps = 24; |
| 43 | |
| 44 | synchronized public void run() { |
| 45 | System.out.println("FPS::: "+fps); |
| 46 | while(true) { |
| 47 | try { |
| 48 | Thread.sleep(round(1000 / fps)); |
| 49 | } catch (InterruptedException ex) { |
| 50 | Logger.getLogger(AppGui.class.getName()).log(Level.SEVERE, null, ex); |
| 51 | } |
| 52 | offA += round(speedA/fps); |
| 53 | offB += round(speedB/fps); |
| 54 | this.repaint(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | Thread t = null; |
| 59 | |
| 60 | AppGui(int fps) { |
| 61 | this.fps = fps; |
| 62 | this.setDoubleBuffered(true); |
| 63 | |
| 64 | t = new Thread(this); |
| 65 | t.start(); |
| 66 | } |
| 67 | |
| 68 | AppGui() { |
| 69 | this(20); |
| 70 | } |
| 71 | |
| 72 | AppGui(String fps) { |
| 73 | this(new Integer(fps)!=null?(int)new Integer(fps):20); |
| 74 | System.out.println("Setting FPS from string: "+fps); |
| 75 | } |
| 76 | |
| 77 | synchronized public void start() throws InterruptedException { |
| 78 | notifyAll(); |
| 79 | } |
| 80 | |
| 81 | public void stop() throws InterruptedException { |
| 82 | t.wait(); |
| 83 | } |
| 84 | |
| 85 | public void destroy() { |
| 86 | this.setVisible(false); |
| 87 | t.interrupt(); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | public class SinusDiff extends JApplet { |
| 93 | |
| 94 | static AppGui appGui = null; |
| 95 | |
| 96 | @Override |
| 97 | public void init() { |
| 98 | appGui = new AppGui(getParameter("fps")); |
| 99 | getContentPane().add(appGui); |
| 100 | } |
| 101 | |
| 102 | public static void main(String[] args) { |
| 103 | JFrame f = new JFrame("Applet window"); |
| 104 | f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
| 105 | f.setSize(640, 480); |
| 106 | appGui = new AppGui(args.length > 0?args[0]:"20"); |
| 107 | f.getContentPane().add(appGui); |
| 108 | f.setVisible(true); |
| 109 | } |
| 110 | |
| 111 | // TODO overwrite start(), stop() and destroy() methods |
| 112 | @Override |
| 113 | public void start() { |
| 114 | try { |
| 115 | appGui.start(); |
| 116 | } catch (InterruptedException ex) { |
| 117 | Logger.getLogger(SinusDiff.class.getName()).log(Level.SEVERE, null, ex); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void stop() { |
| 123 | try { |
| 124 | appGui.stop(); |
| 125 | } catch (InterruptedException ex) { |
| 126 | Logger.getLogger(SinusDiff.class.getName()).log(Level.SEVERE, null, ex); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void destroy() { |
| 132 | appGui.destroy(); |
| 133 | } |
| 134 | } |