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.

Results 1 to 4 of 4

Thread: jframe hashmap image (rectangle) animation problems post two

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jframe hashmap image (rectangle) animation problems post two

    i posted the question at java-forums.org ( https://www.java-forums.org/new-java...-problems.html ) but realized no one uses the site much anymore so i am here trying to spread the word. please take a look if you can. i was talking about this in the cafe but i am woried i need more visibility

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: jframe hashmap image (rectangle) animation problems post two

    See response on other forum.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jframe hashmap image (rectangle) animation problems post two

    i fixed it by removing line #52 of the original thread.

    package test;
     
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
     
    import javax.swing.JFrame;
    import javax.swing.Timer;
     
    public class MovingRectangleTwo extends JFrame implements ActionListener{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	HashMap<Integer, Rectangle> rec;
    	Timer timer;
    	Integer reccount;
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new MovingRectangleTwo();
    	}
    	@SuppressWarnings("deprecation")
    	public MovingRectangleTwo(){
    		rec = new HashMap<Integer, Rectangle>();
    		reccount = new Integer(0);
    		timer = new Timer(500, this);
    		timer.start();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(500, 400);
    		setVisible(true);
    		setTitle("MovingRectangleTwo");
    	}
    	public void paint(Graphics g) {
    		super.paint(g);
    		Iterator<Rectangle> reciterator = rec.values().iterator();
    		while (reciterator.hasNext()) {
    			Rectangle recObject = reciterator.next();
    			System.out.println(recObject.x);
    			g.fillRect(recObject.x, recObject.y, recObject.width, recObject.height);
    			recObject.x--;
    			if (recObject.x <= 0){
    				//System.out.println();
    				//System.out.println();
    				reciterator.remove();
    			}
    		}
    		repaint();
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		rec.put(reccount, new Rectangle(getWidth(), getHeight() / 2, 5, 5));
    		reccount++;
    		//System.out.println(rec.keySet());
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: jframe hashmap image (rectangle) animation problems post two

    The code should NOT call repaint from the paint method. Repaint should only be called when there is something new to paint.

    Why does the code use a Map instead of a List?
    Does the Map ever have more than one entry? If not, why have it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Rectangle won't display in JFrame
    By SquidGiant in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2014, 04:45 PM
  2. Shooting animation with one image
    By poldz123 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 16th, 2013, 11:22 PM
  3. How to display a Rectangle object in JFrame (Problem).
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 07:22 PM
  4. Replies: 2
    Last Post: October 31st, 2011, 09:19 AM
  5. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM