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:
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:
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):
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); }