Results 1 to 6 of 6
- 09-26-2013, 01:48 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 55
- Rep Power
- 0
Need help with adding multiple of the same image to a JFrame
Hi, Im trying to set up a program that will add a determined number of .gif images to a JFrame (using GridLayout to add each one to seperate line) and then will eventually have them race across the screen. For some reason my images and program will not appear when i run it. This is my code:
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Races extends JFrame{ //attributes private int racers; private JLabel jlImage; private Timer timer; int w = 1; public Races(int _racers){ racers = _racers; setTitle("Off to the Races - Nicholas Smith"); setLayout(new GridLayout(0,1)); setLocationByPlatform(true); setLocationRelativeTo(null); while(0 <= racers){ final RacerProgress rp = new RacerProgress(); add(rp); } pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class MyTimerListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ Point loc = jlImage.getLocation(); loc.translate(((int)(Math.random()*100)),0); jlImage.setLocation(loc); repaint(); } } class RacerProgress extends JPanel implements Runnable{ public RacerProgress(){ Icon image = new ImageIcon("races.gif"); jlImage = new JLabel(image); jlImage.setBounds(0,0,66,45); jlImage.setDoubleBuffered(true); add(jlImage); } public void run(){ } } public static void main(String[] args){ Races racetime = new Races(6); } }
- 09-26-2013, 01:52 AM #2
Re: Need help with adding multiple of the same image to a JFrame
Is the program executing an infinite loop that could be keeping it from doing what you want it to do?
If you don't understand my response, don't ignore it, ask a question.
- 09-26-2013, 02:42 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 55
- Rep Power
- 0
Re: Need help with adding multiple of the same image to a JFrame
yes it was an infinite loop so i do have the pictures showing now...thank you!
- 09-26-2013, 04:15 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 55
- Rep Power
- 0
Re: Need help with adding multiple of the same image to a JFrame
i do need assistance with one more thing...i have the pictures appearing and the finish line is showing as well...now i need the images to all be on the left hand side. Is there any way to do this i tried aligning left.
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; public class Races extends JFrame{ //attributes private int racers; private JLabel jlImage; private Timer timer; int w = 1; public Races(int _racers){ racers = _racers; setTitle("Off to the Races - Nicholas Smith"); setLayout(new GridLayout(0,1)); setLocationByPlatform(true); setLocationRelativeTo(null); setSize(600, 600); while(0 <= racers){ final RacerProgress rp = new RacerProgress(); add(rp); rp.setAlignmentX(Component.LEFT_ALIGNMENT); System.out.println("running"); racers--; } //pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class MyTimerListener implements ActionListener{ public void actionPerformed(ActionEvent ae){ Point loc = jlImage.getLocation(); loc.translate(((int)(Math.random()*100)),0); jlImage.setLocation(loc); repaint(); } } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; Line2D lin = new Line2D.Float(500, 100, 500, 600); g2.draw(lin); } class RacerProgress extends JPanel implements Runnable{ public RacerProgress(){ Icon image = new ImageIcon("races.gif"); jlImage = new JLabel(image); jlImage.setBounds(0,0,66,45); jlImage.setDoubleBuffered(true); add(jlImage); } public void run(){ } } public static void main(String[] args){ Races hw4 = new Races(6); } }
- 09-26-2013, 10:27 AM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Need help with adding multiple of the same image to a JFrame
I suggest you take some time to study layout managers.
Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
Because any "how do I get positioning XYZ?" requirements are solved by applying either the right layout manager, or a combination of the right layout managers by nesting components. I tend to subdivide my GUI with a sequence of BorderLayout components and I tend to use either FlowLayout or BoxLayout at the "lowest level".Last edited by gimbal2; 09-26-2013 at 10:31 AM.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 09-27-2013, 01:33 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 55
- Rep Power
- 0
Re: Need help with adding multiple of the same image to a JFrame
normally in this instance i would use a borderlayout and then put in the west but for this problem i have to use a grid layout...i guess i could use border layout and then create a panel with grid layout for the west panel...thank you...i guess i just needed to think it out more
Similar Threads
-
adding a JFXPanel to a JFrame
By such orb in forum AWT / SwingReplies: 2Last Post: 11-29-2012, 07:32 PM -
adding keylistener to JFrame
By Alkor in forum New To JavaReplies: 3Last Post: 03-08-2012, 01:02 PM -
Adding a KeyListener to a JFrame with buttons.
By jamhead in forum AWT / SwingReplies: 1Last Post: 12-11-2010, 08:29 PM -
Adding Multiple Panels and Single Scroll bar on the JFrame
By SANDY_INDIA in forum AWT / SwingReplies: 6Last Post: 07-28-2008, 07:04 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 06:29 PM
Bookmarks