Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
i understand that, but can't figure out, how to load images only once.
i do load same image for all blocks. and sometimes when the ball hits a block, errors appears:
Code :
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at Zaidimas.Main.paint(Main.java:229)
at Zaidimas.Main.update(Main.java:189)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
My paint method:
Code :
public void paint(Graphics g){
//background
try {
url_bg = getClass().getResource("bg.png");
bg = ImageIO.read(url_bg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(bg, 0, 0, this);
//ball
ball.Draw(g);
//paddle
try {
url_paddle = getClass().getResource("paddle.png");
paddle = ImageIO.read(url_paddle);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(paddle, paddle.x, paddle.y, this);
//blocks
for(Block b : blocks){
b.draw(g);
try {
url_block = getClass().getResource("block.png");
block = ImageIO.read(url_block);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(block, b.x, b.y, this);
}
My update method (find this on youtube, to reduce/remove flickering in an applet):
Code :
private Image dbImage;
private Graphics dbGraphics;
public void update(Graphics g){
if (dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbGraphics = dbImage.getGraphics();
}
dbGraphics.setColor(this.getBackground());
dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbGraphics.setColor(this.getForeground());
paint(dbGraphics);
g.drawImage(dbImage, 0, 0, this);
}
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Quote:
java.util.ConcurrentModificationException
Are you changing a List while iterating through it?
Have you read the API doc for that exception? Its doc is in with the doc for all the classes.
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Quote:
Originally Posted by
Norm
Are you changing a List while iterating through it?
Have you read the API doc for that exception? Its doc is in with the doc for all the classes.
if i understand what u asking, then:
i have array list of blocks (bricks).
when an applet is running blocks are destroying by a ball collision. So yes, list are changing while the game is playing.
so, how to load images only once?
edit: i fixed lag, by putting ImageIO.read methods to my init() method,
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
Quote:
so, how to load images only once?
Don't load them twice.
Put the load of the images in a method that is only called one time.
Define the image variables as class variables.
Re: Images works on an applet, but doesn't work when an applet is in a JFrame.
ok, done, this one is SOLVED.
Big thanks to Norm. :)