Home arrow static arrow Java Programming [Archive] - huge CPU using : where does it come from ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - huge CPU using : where does it come from ?
This topic has 11 replies on 1 page.

Posts:68
Registered: 10/30/03
huge CPU using : where does it come from ?  
Jul 9, 2004 7:27 AM



 
Hi,
I programm this beginning of game. It's prettty simple , a charachter which rotates following the mouse pointer. The problem is that this simple application uses about 66% of my Athlon 2200+ when I move the mouse.I don't understand , ok there are some mathematics operations and i use two images to do a clip effect , but that's impossible to do a game without that , that's nothing compared with a real game ...
So can you try to look at my code and tell my why it uses the CPU so much ... and if u have any suggestions about my programming you're welcome

Thx.
Julien.

************************************************************************************ import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*; public class JWar extends JFrame{    private Engine2d Engine2d_;        public JWar()     {        getContentPane().setLayout(null);        setSize(800,600);        this.getContentPane().setBackground(new Color(0,0,0));                Engine2d_ = new Engine2d(10,10,600,550);        getContentPane().add(Engine2d_);                  }        public static void main(String args[])     {	JWar jw = new JWar();        jw.setDefaultCloseOperation(EXIT_ON_CLOSE);	jw.setTitle("J-WAR");        jw.show();    }} ************************************************************************************  import javax.swing.*;import java.awt.*;import java.awt.image.*;import java.awt.event.*;import java.util.*; public class Engine2d extends JPanel implements MouseMotionListener{        private charachter charachter1_ ;    private Image background_;    private Image background_gray_;    private int mouse_x_;    private int mouse_y_;        public Engine2d(int x, int y, int width, int height)     {        this.setBounds(x,y,width,height);        this.setBackground(new Color(255,255,255));        this.addMouseMotionListener(this);                 int c1x=200,c1y=60,v1x=1,v1y=0;        charachter1_ = new charachter(c1x,c1y,v1x,v1y,this);        mouse_x_=c1x+v1x;        mouse_y_=c1y+v1y;                background_ = new ImageIcon(getClass().getResource("background.gif")).getImage();        //background_gray_ = new wiki/./ImageIcon(getClass().getResource("background_gray.gif")).getImage();    }        public void paint(Graphics g)     {        super.paint(g);        g.drawImage(background_gray_,0,0,this);        charachter1_.paint(g);        g.setClip(charachter1_.get_viewZone());        g.drawImage(background_,0,0,this);        charachter1_.paint(g);    }          public void mouseDragged(MouseEvent e) {}    public void mouseMoved(MouseEvent e)     {        mouse_x_=e.getX();        mouse_y_=e.getY();        if( (mouse_x_!=charachter1_.get_x()) || (mouse_y_!=charachter1_.get_y()) )           charachter1_.rotate();    }       public int get_mouse_x() { return mouse_x_; }   public int get_mouse_y() { return mouse_y_; }    }  ************************************************************************************  import java.awt.*;import java.util.*; public class charachter {   private int x_;   private int y_;   private double vx_;   private double vy_;   private int length_view_;   private int width_view_;   private Container container_;   private Polygon viewZone_;      public charachter(int x, int y, double vx, double vy, Container container)    {        x_ = x;        y_ = y;        vx_ = vx;        vy_ = vy;        length_view_ = 150;        width_view_ = 60;        container_ = container;        viewZone_=calculate_polygon();   }      private Polygon calculate_polygon()   {       int x1,y1,x2,y2;        int[] pxs = new int[3];       int[] pys = new int[3];               pxs[0]=x_;        pys[0]=y_;                x1 = x_ + (int)(-width_view_*vy_);        x1 = x1 + (int)(vx_*length_view_);        y1 = y_ + (int)(width_view_*vx_);        y1 = y1 + (int)(vy_*length_view_);        x2 = x_ + (int)(width_view_*vy_);        x2 = x2 + (int)(vx_*length_view_);        y2 = y_ + (int)(-width_view_*vx_);        y2 = y2 + (int)(vy_*length_view_);                pxs[1]=x1;        pys[1]=y1;        pxs[2]=x2;        pys[2]=y2;                return(new Polygon(pxs,pys,3));      }      public void paint(Graphics g)   {       g.setColor(new Color(0,0,255));       g.fillOval(x_-5, y_-5, 10,10);       viewZone_ = calculate_polygon();       g.drawPolygon(viewZone_);   }      public void repaint()   {        container_.repaint();   }      public void recaluculate_vector()   {        double vx,vy;             vx=((Engine2d)container_).get_mouse_x()-x_;        vy=((Engine2d)container_).get_mouse_y()-y_;        vx_=vx/(Math.sqrt(vx*vx+vy*vy));        vy_=vy/(Math.sqrt(vx*vx+vy*vy));   }      public void rotate()   {       recaluculate_vector();       repaint();   }      public int get_x() { return x_; }   public int get_y() { return y_; }   public void set_x(int x) { x_=x; }   public void set_y(int y) { y_=y; }   public double get_vx() { return vx_; }   public double get_vy() { return vy_; }   public void set_vx(double vx) { vx_=vx; }   public void set_vy(double vy) { vy_=vy; }   public int get_length_view() { return length_view_; }   public int get_width_view() { return width_view_; }   public Polygon get_viewZone() { return viewZone_; }      public Rectangle get_Rectangle() { return (new Rectangle(x_-5,y_-5,10,10)); }   } 


PS : the two image are 600*500 , the first in color and the second in grey level , to do an effect of limited vison zone ...
 

Posts:68
Registered: 10/30/03
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 7:29 AM (reply 1 of 11)



 
oups the second image loading shouldn't be a commentary ..
Here is the real code

...

public Engine2d(int x, int y, int width, int height)
{
this.setBounds(x,y,width,height);
this.setBackground(new Color(255,255,255));
this.addMouseMotionListener(this);

int c1x=200,c1y=60,v1x=1,v1y=0;
charachter1_ = new charachter(c1x,c1y,v1x,v1y,this);
mouse_x_=c1x+v1x;
mouse_y_=c1y+v1y;

background_ = new ImageIcon(getClass().getResource("wiki/./background.gif")).getImage();
backgroundwiki/./_gray_ = new ImageIcon(getClass().getResource("wiki/./background_gray.gif")).getImage();
}

...

Sorry

 

Posts:68
Registered: 10/30/03
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 9:13 AM (reply 2 of 11)



 
any idea ... really need help on that

Thx again
 

Posts:777
Registered: 9/18/03
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 10:03 AM (reply 3 of 11)



 
I changed your main a little
public Engine()    {		super("J-WAR");        getContentPane().setLayout(null);        setSize(800,600);        this.getContentPane().setBackground(new Color(0,0,0));         Engine2d_ = new Engine2d(10,10,600,550);        getContentPane().add(Engine2d_);    }     public static void main(String args[])    {	Engine jw = new Engine();        jw.setDefaultCloseOperation(EXIT_ON_CLOSE);         jw.setVisible(true);        jw.pack();    }


your right about the cpu stuff

I did a test on other code samples that use java3d, and guess what, same thing.
What you are worried about, I think is not a problem.
I think you could put thread priorities on it, but that might slow it down.
You could also , rather than have it 100% active on the mouse, make it only move with mouse click??

 

Posts:68
Registered: 10/30/03
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 11:35 AM (reply 4 of 11)



 
ok , thx for your answer
But that's impossible , how can i add collsion test , several charachters , animation , net gaming ... if i'm already at 65 % of cpu using with that ridiculous game.
There must be a problem somewhere and a way to improve it ... if not it's really impossible to program a game using java API .

I'm open to every help and suggestions , thx.
 

Posts:8,813
Registered: 10/4/00
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 12:55 PM (reply 5 of 11)



 
You might try making it so that it will give up trying to paint if it gets another mouse event. I think what is happening is that the mouseevents are queueing up and you just keep trying to do a complete refresh on every one.
 

Posts:68
Registered: 10/30/03
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 2:51 PM (reply 6 of 11)



 
ok thx but how can I do that ?
 

Posts:3,534
Registered: 9/30/99
Re: huge CPU using : where does it come from ?  
Jul 9, 2004 4:51 PM (reply 7 of 11)



 
get rid of your repaint after you rotate vector and see if the container will repaint after the change. You may be doubling up on repainting.
 

Posts:68
Registered: 10/30/03
Re: huge CPU using : where does it come from ?  
Jul 10, 2004 5:00 AM (reply 8 of 11)



 
i'm sorry but my english is very bad and i don't really understand what you mean by that ( " get rid of your repaint after you rotate vector and see if the container will repaint after the change ... " ), can u give me some precisions or a litlle exemple ...

Thx again for your help
 

Posts:8,813
Registered: 10/4/00
Re: huge CPU using : where does it come from ?  
Jul 10, 2004 7:58 AM (reply 9 of 11)



 
You might try looking at the game forum. Here is a link to some of the 4K games made over by folks over there.
http://forum.java.sun.com/thread.jsp?forum=406&thread=471050&start=279&range=1

The game by Abuse sounds like what you need. There is source code there too. Unfortunately the code is obfuscated because they were trying to squeeze every last byte out to get to 4K.

You might try running it and making sure it's along the lines of what you are looking for. Then look at the source and get some ideas of how he accomplished it. Then search the game forum to get better ideas (unobfuscated) about coding it. Then start asking people over there if you are having problems.
 

Posts:3,534
Registered: 9/30/99
Re: huge CPU using : where does it come from ?  
Jul 10, 2004 10:58 AM (reply 10 of 11)



 
i'm sorry but my english is very bad and i don't
really understand what you mean by that ( " get rid of
your repaint after you rotate vector and see if the
container will repaint after the change ... " ), can u
give me some precisions or a litlle exemple ...

Thx again for your help

   public void rotate()   {              recaluculate_vector();       repaint();     }
 

Posts:3,534
Registered: 9/30/99
Re: huge CPU using : where does it come from ?  
Jul 10, 2004 10:59 AM (reply 11 of 11)



 
remove the repaint
 
This topic has 11 replies on 1 page.