• Post Reply Bookmark Topic Watch Topic
  • New Topic

using too much computer resources

 
Chitlesh GOORAH
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i tweak this in order it doesnt slows down my computer ?
note that index.jpg =284Kb
and i dont want to change this picture

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.Timer;
import java.awt.event.*;
import javax.swing.JDesktopPane;

public class Jeu extends JApplet
implements ActionListener {
JDesktopPane desktop;
int taille=100;
Point position=new Point(50,50);
Timer minuteur;

public void init() {
desktop = new JDesktopPane();
desktop.setToolTipText("Chitlesh GOORAH");
add(desktop);
loadBackgroundImage();
}

public void start() {
minuteur=new Timer(100,this);
minuteur.start();
}

public void actionPerformed(ActionEvent evt) {repaint();}

public void paint(Graphics g) {
g.drawRect(position.x,position.y,taille,taille);
position.x+=(int)(Math.random()*10);
position.y+=(int)(Math.random()*10);
loadBackgroundImage();
}
protected void loadBackgroundImage() {
ImageIcon icon = new ImageIcon("index.jpg");
JLabel l = new JLabel(icon);

l.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
desktop.add(l, new Integer(Integer.MIN_VALUE));
}
}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your timer calls 'repaint' 10 times a second. Each call to 'paint' causes the applet to load a new image all over again. ImageIcon will block the execution of the applet until the image is loaded. Try removing 'loadBackgroundImage();' from the 'paint' method. Once you get the image loaded the first time it will stay with you.
 
Chitlesh GOORAH
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nop it didnt stay, !!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you need to call super.paint(g); first in your paint method so that it knows to redraw the components (JLabel with your image on it?).

 
Chitlesh GOORAH
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nop, neither;
this too publish once the image and afterwards it vanishes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!