All pastes #187644 Raw Edit

Jonte

public java v1 · immutable
·published 2006-10-01 09:46 UTC
rendered paste body
import javax.swing.*;import java.util.Vector;import java.awt.*;public class jattack extends JFrame implements Runnable{		public static Vector blockVector = new Vector();	public Thread t;		public void run(){ // Målar om skärmen var 10:e ms		while (true){			this.repaint();			try { // Behövs				t.sleep(5);			}catch (InterruptedException e){}								}			}		public jattack(){		t=new Thread(this);		t.start();				this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		this.setSize(400,200);		this.setVisible(true);	}		public void paint(Graphics g){	//	g.setColor(Color.white);	//	g.fillRect(10,10,this.getWidth(),this.getHeight());		for (int i=0;i<blockVector.size();i++){						block b = ((block)blockVector.get(i));						g.setColor(Color.white);			g.drawRect(b.x,b.y,b.width,b.height);			g.setColor(Color.black);			b.calculateydirValue(this.getHeight());			g.drawRect(b.x,b.y,b.width,b.height);		}	}		public static void spawnBlocks() {		int flagDir = -1;		for (int i=0;i<10;i++){			block b = new block();			b.x += b.width*i; // Sätt kuben lite till höger						if (flagDir == -1){ // Ge kuberna olika y-riktnings värden				b.ydir=-1;				flagDir = 1;			}else{				b.ydir = 1;				flagDir = -1;			}						blockVector.add(b);		}	}			public static void main(String[] args){		spawnBlocks();		jattack frame = new jattack();	}	}class block{	public int width = 20;	public int height = 20;	public int x = 100;	public int y = 100;	public int ydir = -1; // ner	public void calculateydirValue(int max){		if (y>max){			ydir = -1;		}		if (y<0){			ydir = 1;		}			y+=ydir;	}			}