Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By gimbal2

Thread: Need help with adding multiple of the same image to a JFrame

  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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);
       }
    
    }
    I am not receiving any error when running or compiling it is just sitting there. Thank you so much for the help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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?
    ndsmith20 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

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

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

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

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,723
    Rep Power
    5

    Default 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 09:31 AM.
    ndsmith20 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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

  1. adding a JFXPanel to a JFrame
    By such orb in forum AWT / Swing
    Replies: 2
    Last Post: 11-29-2012, 06:32 PM
  2. adding keylistener to JFrame
    By Alkor in forum New To Java
    Replies: 3
    Last Post: 03-08-2012, 12:02 PM
  3. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 07:29 PM
  4. Replies: 6
    Last Post: 07-28-2008, 06:04 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

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