Jumpbox.java


import java.awt.*;
import java.util.Vector;

public class Jumpbox extends Frame {

  private static String usage = "Aufruf: Jumpbox <dauer>";
  private static String me = "Xxxxxx Xxxxxxx, 0742941";
  static Vector alleFarben;
  static Vector rText;
  static Image smilyImage;
  static Image 
other
nonSmilyImage; private static long maxTime; private Arbeitsbereich ab; private int xPosB=50, yPosB=50, R; private int oldXPosB,oldYPosB; private static int punkte
; private int sprungCounter; Vector farben; Color actualFarbe; private static int commandLineOK(String args[]) { try { return Integer.parseInt(args[0]); } catch (Exception e) { return 15; } } public static void main(String args[]) { int parameter; parameter=
other
commandLineOK(args); maxTime = System.currentTimeMillis() + parameter * 1000; Toolkit tk = Toolkit.getDefaultToolkit(); smilyImage = tk.getImage("smily.gif"); nonSmilyImage = tk.getImage("nonSmily.gif"); alleFarben =
new Vector(8); alleFarben.addElement(Color.blue); alleFarben.addElement(Color.cyan); alleFarben.addElement(Color.green); alleFarben.addElement(Color.magenta); alleFarben.addElement(Color.orange); alleFarben.addElement(Color.pink); alleFarben.addElement(Color.red); alleFarben.addElement(Color.yellow); Jumpbox window
other
= new Jumpbox(); window.setTitle("Jumpbox (" + String.valueOf(parameter) + " seconds): " + me); window.resize(511,435); window.show(); window.awaitDelete(); System.out.println("Ergebnis " + String.valueOf
(punkte) + " Punkte in " + String.valueOf(parameter) + " Sekunden"); System.exit(0); } public Jumpbox() { punkte = 0; sprungCounter = 0; farben = new Vector(4); setFarben(); setActualFarbe(); setActualPos(); rText=new Vector(4); rText.addElement("L"); rText.addElement("R"); rText.addElement("O"); rText.addElement("U"); ab = new Arbeitsbereich(); add(ab); } private void setFarben() { Vector tmp=new Vector(8); for(int i=0;i<8;i++) tmp.addElement(alleFarben.elementAt(i)); for(int i=0;i<4;i++) { int index = (int)(Math.random()*(8-i)); farben.addElement(tmp.elementAt(index)); tmp.removeElementAt(index); } } private void setActualFarbe() { int index = (int)(Math.random()*4); actualFarbe=(Color)farben.elementAt(index); R = index; } private void setActualPos() { oldXPosB = xPosB; oldYPosB = yPosB; xPosB = (int)(Math.random()*450); yPosB = (int)(Math.random()*300)+50; } public synchronized void awaitDelete() { try { wait(); } catch (InterruptedException e) {} } public synchronized void signalDelete() { notify(); } private boolean isInB(int x, int y) { return (x>=xPosB)&&(x<xPosB+50)&&(y>=yPosB)&&(y<yPosB+50); } private boolean isInIntimB(int x, int y) { return false; } private void bSpringt() { if(sprungCounter>=2) { sprungCounter = 0; setFarben(); } else { sprungCounter++; } setActualFarbe(); setActualPos(); ab.repaint(); } private synchronized void bGrummelt() { System.out.
other
println("bGrummelt"); try { wait(1000); } catch (InterruptedException e) {} } private synchronized void bGrinst() { System.out.println
("bGrinst"); try { wait(500); } catch (InterruptedException e) {} } /* #################################################################### */ class Arbeitsbereich extends Canvas { public synchronized void paint (Graphics g) {
other
update(g); } public void update (Graphics g) { for(int i=0;i<4;i++) { g.setColor((Color)farben.elementAt(i)); g.fillRect
(i*50,0,50,49); g.setColor(Color.black); g.drawString((String)rText.elementAt(i),i*50+25,25); } g.clearRect(oldXPosB,oldYPosB,50,50); g.setColor(actualFarbe); g.fillRect(xPosB,yPosB,50,50); g.setColor(Color.black); g.drawRect(0,50,500,350); } public boolean handleEvent(Event evt) { switch(evt.id) { case Event.MOUSE_MOVE: case Event.MOUSE_DRAG: if(System.currentTimeMillis()<maxTime) { if(isInIntimB(evt.x,evt.y)) { punkte--; bGrummelt(); bSpringt(); repaint(); } else { if(isInB(evt.x,evt.y)) { punkte++; bGrinst(); bSpringt(); repaint(); } } } else signalDelete(); return(true); default: return false; } } } }