Results 1 to 7 of 7
- 07-03-2020, 09:52 AM #1
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++; } }
Nathaniel Victor Nelson
- 07-05-2020, 04:52 AM #2
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
- 07-05-2020, 03:33 PM #3
Re: jframe hashmap image (rectangle) animation problems
it looks like les rectangle are being filled but it is removing the wrong rectangle
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.
- 07-07-2020, 05:14 AM #4
Re: jframe hashmap image (rectangle) animation problems
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
- 07-07-2020, 01:19 PM #5
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.
- 07-09-2020, 02:16 AM #6
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
- 07-09-2020, 02:54 AM #7
Similar Threads
-
java creating smooth multi image animation? Image flashing :/
By dev.ryanJ in forum New To JavaReplies: 2Last Post: 09-29-2013, 03:16 PM -
Rotating rectangle animation proble
By Tarasj in forum AWT / SwingReplies: 2Last Post: 07-18-2012, 08:29 AM -
Why won't this rectangle display in my JFrame?
By wikemol in forum New To JavaReplies: 1Last Post: 01-07-2012, 08:14 AM -
gui animation problems
By helpisontheway in forum New To JavaReplies: 2Last Post: 12-15-2009, 03:41 PM -
Right-clicking a rectangle on a JFrame to remove it
By busdude in forum New To JavaReplies: 4Last Post: 02-20-2009, 03:44 AM
Bookmarks