Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: Images works on an applet, but doesn't work when an applet is in a JFrame.

  1. #26
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

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

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  4. #28
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    Quote Originally Posted by Norm View Post
    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,
    Last edited by scorpas; May 18th, 2012 at 01:54 PM.

  5. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Images works on an applet, but doesn't work when an applet is in a JFrame.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    scorpas (May 18th, 2012)

  7. #30
    Member
    Join Date
    May 2012
    Posts
    34
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default 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.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Trying to make 3 images become random in an Applet. Need help
    By slahsdash in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 9th, 2012, 09:39 PM
  2. Replies: 2
    Last Post: February 24th, 2012, 07:56 PM
  3. Can't get applet to work on Safari..
    By Bertorox in forum Java Applets
    Replies: 10
    Last Post: September 20th, 2011, 10:00 PM
  4. how to convert applet to a JPanel or JFrame
    By flipsyde02 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 29th, 2011, 07:40 AM
  5. [SOLVED] Java Noob, Jframe in Applet.
    By rLLZORS in forum AWT / Java Swing
    Replies: 2
    Last Post: May 5th, 2011, 10:42 AM

Tags for this Thread