Results 1 to 7 of 7
  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    109
    Rep Power
    0

    Default jframe hashmap image (rectangle) animation problems

    i am trying to move rectangles acros the screen. i have a swing timer ading rectangles to the list and i am trying to iterate in my paint method. my problem(s) are ilegalstateexception at my remove method or i tried removing with a key using list.remove(rec); but it is not removing as you can see if you run the program. has anything changed in java since five years ago? i know paintComponent does not work anymore or whatever.

    Java Code:
    package test;
    // GCS Exercise 10.1 Solution: DrawPanel.java
    // Program randomly draws shapes.
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Random;
    import javax.swing.*;
    
    public class DrawFrame extends JFrame implements ActionListener
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	HashMap<Integer, Rectangle> list;
    	Integer x, hn;
    	Timer timer;
        public static void main(String[] args) {
        	new DrawFrame();
        }
        @SuppressWarnings("deprecation")
    	public DrawFrame()
        {
        	hn = new Integer(0);
        	list = new HashMap<Integer, Rectangle>();
        	timer = new Timer(1000, this);
        	timer.setInitialDelay(0);
        	timer.start();
        	x = new Integer(222);
            setTitle("TestDraw");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 500);
            setVisible(true);
        }
    
       // draw shapes using polymorphism
       @SuppressWarnings("unlikely-arg-type")
    public void paint(Graphics g)
       {
          super.paint(g);
          Iterator<Rectangle> listiterator = list.values().iterator();
          while (listiterator.hasNext()) {
        	  Rectangle rec = list.values().iterator().next();
              g.fillRect(rec.x, 222, 22, 22);
              rec.x--;
              if (rec.x <= 0) {
            	  System.out.print("working");
            	  listiterator.remove();
            	  hn--;
              }
              System.out.println(rec.x);
              System.out.println(list.size());
          }
          repaint();
    } // end class DrawPanel
    
    @Override
    public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	//if (e.get)
    	list.put(hn, new Rectangle(222, 222, 8, 8));
    	hn++;
    }
    }
    i was able to get one rectangle moving outside the list. i had the same problem with an ArayList and am too tired to make a Rectangle[] list. anyone have any ideas?
    Nathaniel Victor Nelson

  2. #2
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    109
    Rep Power
    0

    Default somewhat working now

    okay,

    so now it is not crashing the JFrame (the code below). it looks like les rectangle are being filled but it is removing the wrong rectangle. i can probably find this online i am sure someone has asked this before. it is suposed to remove a rectangle if it is going off screen (or off JFrame i mean) but it removes ones in the list that are halfway to the edge of the JFrame.

    Java Code:
    package test;
    // GCS Exercise 10.1 Solution: DrawPanel.java
    // Program randomly draws shapes.
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Random;
    import javax.swing.*;
    
    public class DrawFrame extends JFrame implements ActionListener
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	HashMap<Integer, Rectangle> list;
    	Integer x, hn;
    	Timer timer;
        public static void main(String[] args) {
        	new DrawFrame();
        }
        @SuppressWarnings("deprecation")
    	public DrawFrame()
        {
        	hn = new Integer(0);
        	list = new HashMap<Integer, Rectangle>();
        	timer = new Timer(750, this);
        	timer.setInitialDelay(0);
        	timer.start();
        	x = new Integer(222);
            setTitle("TestDraw");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 500);
            setVisible(true);
        }
    
       // draw shapes using polymorphism
       @SuppressWarnings("unlikely-arg-type")
    public void paint(Graphics g)
       {
          super.paint(g);
          Iterator<Rectangle> listiterator = list.values().iterator();
          while (listiterator.hasNext()) {
        	  Rectangle rec = listiterator.next();
              g.fillRect(rec.x, 222, 22, 22);
              rec.x--;
              if (rec.x <= 0) {
            	  System.out.print("working");
            	  listiterator.remove();
            	  hn--;
              }
              System.out.println(rec.x);
              System.out.println(list.size());
          }
          repaint();
    } // end class DrawPanel
    
    @Override
    public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	//if (e.get)
    	list.put(hn, new Rectangle(this.getWidth(), 222, 8, 8));
    	hn++;
    }
    }
    Nathaniel Victor Nelson

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: jframe hashmap image (rectangle) animation problems

    it looks like les rectangle are being filled but it is removing the wrong rectangle
    Where do you see that?
    When I execute the code I see a black rectangles moving very quickly from right to left, one after another.
    What should the code be doing that is different from what it does now?

    There are no comments in the code describing what it is supposed to do and how it was going to do it.
    Last edited by Norm; 07-05-2020 at 03:39 PM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    109
    Rep Power
    0

    Default Re: jframe hashmap image (rectangle) animation problems

    Quote Originally Posted by Norm View Post
    There are no comments in the code describing what it is supposed to do and how it was going to do it.
    okay norman i cleaned it up and put coments

    Java Code:
    package test; // my personal package... do not test with this
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Random;
    import javax.swing.*;
     
    public class DrawFrame extends JFrame implements ActionListener
    {
        private static final long serialVersionUID = 1L;
        HashMap<Integer, Rectangle> list; // peramaterize the hashmap
        Integer x, hn;
        Timer timer;
        public static void main(String[] args) {
            new DrawFrame();
        }
        public DrawFrame()
        {
            hn = new Integer(0);
            list = new HashMap<Integer, Rectangle>(); //peramiterize new list
            timer = new Timer(750, this);
            timer.setInitialDelay(0);
            timer.start();
            x = new Integer(222);
            setTitle("online help");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 500);
            setVisible(true);
        }
     
    public void paint(Graphics g)
       {
          super.paint(g);
          Iterator<Rectangle> listiterator = list.values().iterator(); // set list to listiterator for hasNext() cal
          while (listiterator.hasNext()) { // cycle through list
    // i put this stuf in "paint" because it is caled at a reasonable time
              Rectangle rec = listiterator.next(); // rec wil be the next rectangle in the list
              g.fillRect(rec.x, 222, 22, 22);
              rec.x--;
              if (rec.x <= 0) { // if rec's x quorinate is les than zero
                  System.out.print("working");
                  listiterator.remove(); // remove the curent item (rec right? (the list number))
              }
              System.out.println(rec.x);
              System.out.println(list.size());
          }
          repaint();
    }
     
    @Override
    public void actionPerformed(ActionEvent e) {
        list.put(hn, new Rectangle(this.getWidth(), 222, 8, 8));
    }
    }
    Nathaniel Victor Nelson

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: jframe hashmap image (rectangle) animation problems

    Does the code do what it is supposed to do now?
    I do not see any comments that describe what the code is supposed to do.

    Calling repaint from the paint method is not a good way to do animation. The calls to repaint should be on a timer.

    The debug print outs would be more readable if they had text describing:
    Java Code:
              System.out.println("x="+ rec.x + ", size="+list.size());
    Last edited by Norm; 07-07-2020 at 01:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    109
    Rep Power
    0

    Default Re: jframe hashmap image (rectangle) animation problems

    as far as i know this is solved, i am switching the thread location to https://www.javaprogrammingforums.co...-post-two.html
    Nathaniel Victor Nelson

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: jframe hashmap image (rectangle) animation problems

    Have you moved the call to repaint to be in a method called by a timer? Calling from paint is not a good solution.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 09-29-2013, 03:16 PM
  2. Rotating rectangle animation proble
    By Tarasj in forum AWT / Swing
    Replies: 2
    Last Post: 07-18-2012, 08:29 AM
  3. Why won't this rectangle display in my JFrame?
    By wikemol in forum New To Java
    Replies: 1
    Last Post: 01-07-2012, 08:14 AM
  4. gui animation problems
    By helpisontheway in forum New To Java
    Replies: 2
    Last Post: 12-15-2009, 03:41 PM
  5. Right-clicking a rectangle on a JFrame to remove it
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 03:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •