making code faster smaller and more elegant

hi just like yesterday I齧 new to java. first of all I want to get familiar with the basics. I wrote a little game therefore. with some help. within the code are no mistakes, compiling errors or such. but now my question is why is my CPU usage nearly 75% when I run it. when the 齢?( Thread.sleep(h); ) is about 5 which makes the game faster but the CPU usage is 100%. Are there any possibilities to make the code faster? next question. are there any tricks to make the code more elegant or smaller? you know what I mean? it齭 quite difficult for me to express what I want cause my English isn齮 the best?br>what ever here is the code

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

publicclass BricksGameextends JFrameimplements MouseListener, MouseMotionListener{

privateint mouseX, mouseY;

double a = -3;

int delta = 1;

int beta = -3;

int x_max = 288;

int y_max = 320;

int x_min = 0;

int y_min = 0;

int x = 5;

int y = 5;

int b = y_max;

int h = 15;

int y_block = 80;

int i;

int x_point;

int y_point =0;

int punkte = 0;

double k ;

double r;

int zahl = 1;

boolean changed =true;

boolean gameNotOver =true;

JPanel panelOben =new MyPanel();

JPanel panelUnten =new JPanel();

JLabel labelU1 =new JLabel();

JLabel labelU2 =new JLabel("Punktestand: 0");

JButton b1 =new JButton("restart");

public BricksGame(){

setSize(400,600);

setLocation(100,200);

Dimension full = getSize();

mouseX = full.width/2;

mouseY = full.height/2;

addMouseMotionListener(this);

addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){System.exit(0);}

});

panelOben.setLayout(null);

//Ausrichtung der Elemente

panelOben.setBounds(50, 50, 400, 500);

panelUnten.addMouseMotionListener(this);

panelUnten.setLayout(null);

//Ausrichtung der Elemente

panelUnten.setSize(300, 100);

panelUnten.setBounds(50, 500, 300, 100);

labelU1.setBounds(100,380,130,25);

labelU2.setBounds(150,430,130,25);

b1.setBounds(150,500, 80, 30);

b1.addMouseListener(this);

ImageIcon ii2 =new ImageIcon(getClass().getResource("TW1.gif"));

labelU1.setIcon(ii2);

//Hinzuf齡en der Elemente

panelUnten.add(labelU1);

panelUnten.add(labelU2);

panelUnten.add(b1);

getContentPane().add(panelOben);

getContentPane().add(panelUnten);

setResizable(false);

(new Mover()).start();

setVisible(true);

validate();

}

publicstaticvoid main (String[]args){

BricksGame object =new BricksGame();

}

publicvoid mousePressed (MouseEvent e){}

publicvoid mouseReleased (MouseEvent e){}

publicvoid mouseClicked (MouseEvent e){

restart();

}

publicvoid mouseEntered (MouseEvent e){}

publicvoid mouseExited (MouseEvent e){}

publicvoid mouseDragged (MouseEvent e){}

publicvoid mouseMoved (MouseEvent e){

mouseX = e.getX();

if (mouseX <= 85){

mouseX = 85;

}elseif (mouseX >= 319){

mouseX = 319;

}

labelU1.setBounds(mouseX -35 ,380,70,25);

}

/**

*

* @author Haferberger

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

class Moverextends Thread{

/**

*

*/

publicvoid run(){

while (gameNotOver){

try{Thread.sleep(h);}

catch (Exception e){

// TODO: handle exception

}

repaint();

}

}

}

/**

*

* @author Haferberger

*

* To change the template for this generated type comment go to

* Window>Preferences>Java>Code Generation>Code and Comments

*/

class MyPanelextends JPanel{

/**

*

*/

publicvoid paint(Graphics g){

if (y <= y_min && !changed){

//System.out.println("up");

//wie oft oben gegen

i++;

//Koordinaten des G齨en Punktes

x_point =x_max -x;

y_point =0;

//Nach 5 mal oben gegen wird der Winkel ge齨dert

if (i%5==0){

zZahl();

alpha();

}

//Richtungs齨derung

a = -a;

b = (int)(y_min - a*x);

changed =true;

}elseif (y >= y_max && !changed){

//System.out.println("down");

//Bei Ber齢rung unten wird der Block verschoben

if (y_block == 221){

beta = -beta;

}elseif (y_block == 80){

beta = -beta;

}

y_block+= beta;

//Betimmen wo der Ball aufkommt

if (x + 5 >= mouseX - 84 && x + 5 <= mouseX -27 ){

//System.out.println("Mitte");

}elseif(x + 5 >= mouseX - 95 && x + 5 <= mouseX -85 ){

//System.out.println("au齟n links");

if(delta > 0){

delta = -delta;

a = -a;

b = (int)(y_min - a*x);

}

}elseif(x + 5 <= mouseX -16 && x + 5 >= mouseX -26 ){

//System.out.println("au齟n rechts");

if(delta < 0){

delta = -delta;

a = -a;

b =(int)(y_min - a*x);

}

}else{

System.out.println("daneben");

gameNotOver=false;

}

a = -a;

b = (int)(y_max - a*x);

changed =true;

}elseif (x >= x_max && !changed){

//System.out.println("right");

a = -a;

b = (int)(y - a*x_max);

delta = -delta;

changed =true;

}elseif (x <= x_min && !changed){

//System.out.println("left");

a = -a;

b = (int)(y - a*x_min);

delta = -delta;

changed =true;

}elseif (y == y_block && x>72 && x<216 && !changed){

//System.out.println("Balken unten");

a = -a;

b = (int)(y - a*x);

changed =true;

}elseif (y == y_block+20 && x>72 && x<216 && !changed){

//System.out.println("Balken oben");

a = -a;

b = (int)(y - a*x);

changed =true;

}else{

changed =false;

}

g.setColor(Color.cyan);

g.fillRect(0, 0, 300, 330);

x+= delta;

y = (int)(a*x + b);

g.setColor(Color.red);

g.fillOval(x,y+5,10,10);

g.setColor(Color.magenta);

g.fillRect(72, y_block,144,20);

y_point+=2;

if(y_point==310){

y_point=500;

if(x_point + 5 >= mouseX - 94 && x_point + 5 <= mouseX -20 ){

punkte+=50;

}

}

g.setColor(Color.green);

g.fillOval(x_point,y_point,20,20);

labelU2.setText("Punktestand: " + punkte);

labelU2.validate();

}

}

/**

*

*

*/

publicvoid zZahl(){

r = (Math.random() * 10);

zahl = (int)r%5 + 1;

}

publicvoid alpha(){

switch(zahl){

case 4:

if (a<0){

a=-1;

}elseif (a>0){

a=1;

};

if (delta<0){

delta=-4;

}elseif (delta>0){

delta=4;

};

break;

case 3:

if (a<0){

a=-2;

}elseif(a>0){

a=2;

};

if (delta<0){

delta=-4;

}elseif(delta>0){

delta=4;

};

break;

case 5:

if (a<0){

a=-3;

}elseif (a>0){

a=3;

};

if (delta<0){

delta=-1;

}elseif (delta>0){

delta=1;

};

break;

case 2:

if (a<0){

a=-0.5;

}elseif (a>0){

a=0.5;

};

if (delta<0){

delta=-4;

}elseif (delta>0){

delta=4;

};

break;

case 1:

if (a<0){

a=-0.2;

}elseif (a>0){

a=0.2;

};

if (delta<0){

delta=-5;

}elseif (delta>0){

delta=5;

};

break;

}

}

publicvoid restart(){

gameNotOver=true;

y_block=80;

beta = -3;

x=mouseX;

(new Mover()).start();

punkte = 0;

labelU2.validate();

}

}

thanks

[18230 byte] By [TiloH.a] at [2007-9-29 16:23:05]
# 1
Simplify that alpha method...
PinkyDeada at 2007-7-15 14:40:14 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
...you could also simplify the paint method. Try to get out of it as quickly as possible.And your thread is refreshing every 15ms, maybe thats what you want, but it is very costly....
PinkyDeada at 2007-7-15 14:40:14 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

First of all, big friendly advice: split your code into few classes and methods. Currently your code looks very ugly.

And one simple question: what is happening with Mover instance after restart? I don't see anything that stops old Mover thread instance before starting a new one...

And so on...

It is much simpler to answer your question when code is splited in small parts - problems then are located in small pieces of code.

cebanencoa at 2007-7-15 14:40:14 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
i interrupted the thread anywhere with gameNotOver = false.
TiloH.a at 2007-7-15 14:40:14 > top of Java-index,Archived Forums,New To Java Technology Archive...