import java.applet.*; import java.awt.*; import java.util.*; public class Spill extends Applet implements Runnable { int appletsize_x = 800; int appletsize_y = 800; //Denne lagrer bildet mens det blir "tegnet" i minnet private Image dbImage; //Noe annet du må beholde private Graphics dbg; //Tråden private volatile Thread t; //Vektor er et array med variabel størrelse. Kjekt å ha. Denne begynner med størrelse 0 og øker med 1 hvis nødvendig. Vector F = new Vector(0,1); //Objektet Fly class Fly { double x; double y; double z; double fartx; double farty; int retning; int rotasjon; public Fly(int x1, int y1, int z1, int fartx1, int farty1) { x = (double)x1; y = (double)y1; z = (double)z1; fartx = (double)fartx1; farty = (double)farty1; rotasjon=0; retning=0; } public void flytt() { x += fartx; y += farty; if(rotasjon!=0) { double vinkel = vinkel(fartx,farty); if(rotasjon>0) { retning += 1; vinkel += Math.PI/192.0; } else { retning -= 1; vinkel -= Math.PI/192.0; } double fart = Math.sqrt(Math.pow(fartx,2)+Math.pow(farty,2)); fartx=fart*Math.sin(vinkel); farty=fart*Math.cos(vinkel); if(retning<0) { retning = 288+retning; } else if(retning>288) { retning = retning-288; } if((retning==0)||(retning==96)||(retning==192)||(retning==288)) { if(rotasjon>0) { rotasjon--; } else { rotasjon++; } } } } } public void init() { //Bakgrunnsfarge setBackground(Color.green); } public void start() { //Start tråden t = new Thread(this); t.start(); //Finn størrelsene på appleten appletsize_x = this.getSize().width; appletsize_y = this.getSize().height; } public void stop() { //Drep tråden t = null; } public void destroy() { //Drep tråden t = null; } //Selve spillet public void run() { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); Thread tDenne = Thread.currentThread(); // run a long while (true) this means in our case "always" while (tDenne==t) //Mens spillet skal fortsette { //Det som skal gjøres... //Tegn på nytt repaint(); try { // Stop thread for 20 milliseconds Thread.sleep (30); } catch (InterruptedException ex) { // do nothing } // set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint (Graphics g) { //Bruk metodene til Graphics-klassen på objektet g g.drawString("Tekst",x,y); g.fillPolygon({1,2,3},{2,2,3},3); g.setColor(Color.green); //Fargen du skal tegne med herfra. } //For musehendelser public boolean mouseDown (Event e, int x, int y) { // Change direction Fly fly; if(e.metaDown()) {//Høyre nede for(int i=0;i(fly.x-9)&&x<(fly.x+9)&&y>(fly.y-9)&&y<(fly.y+9)) { fly.rotasjon--; i=F.size(); } } } else { for(int i=0;i(fly.x-9)&&x<(fly.x+9)&&y>(fly.y-9)&&y<(fly.y+9)) { fly.rotasjon++; i=F.size(); } } } // DON'T FORGET (although not necessary here)!! return true; } //For tastaturhendelser /*public boolean keyDown (Event e, int key) { // user presses left cursor key if (key == Event.LEFT) { // changing x - speed so that ball moves to the left side (x_speed negative) x_speed = -1; // user presses right cursor key } else if (key == Event.RIGHT) { // changing x - speed so that ball moves to the right side (x_speed positive) x_speed = 1; } else if (key == 32) { // user presses space bar (value = 32!) // Stop ball (x_speed = 0) x_speed = 0; } else { //Additionally the method prints out the ASCII - value if an other key is pressed. This is not necessary but a possibility for you to test which value a key has. System.out.println ("Character: " + (char)key + " Integer Value: " + key); } // DON'T FORGET (although it has no meaning here) return true; } */ //For å unngå at bildet flimrer public void update (Graphics g) { // initialize buffer if (dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } // clear screen in background dbg.setColor (getBackground()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); // draw elements in background dbg.setColor (getForeground()); paint(dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); } }