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 10 of 10

Thread: Help with adding multiple images to an application

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with adding multiple images to an application

    I have the following code that I found on the web and modified to my likings:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class ImagePanel extends JPanel {
        BufferedImage image;
        BufferedImage image2;
     
     
        public ImagePanel(BufferedImage image) {
            this.image = image;
     
        }
     
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 50, 50, this);
            g.drawImage(image, 100, 100, this);
            g.drawImage(image2, 120, 120, this);
        }
     
        public static void main(String[] args) throws IOException {
            BufferedImage image = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile1.png"));
            BufferedImage image2 = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile2.png"));
            ImagePanel contentPane = new ImagePanel(image);
            ImagePanel contentPane2 = new ImagePanel(image2);
     
     
            JFrame f = new JFrame("Bl");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(contentPane);
            f.getContentPane().add(contentPane2);
     
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

    The code successfully imports and displays two identical images as shown in the paintComponent. What I am trying to do is change it so that it can load multiple different image files and display them separately. As you can see I added "image2" into in a few places in hopes that I might find a way to add a second image. Unfortunately I had no luck with this. What am I doing wrong? Sorry, I'm sure this is probably something that is very obvious to some, but I'm still pretty new to programming.
    Last edited by Jumbosize; February 26th, 2012 at 11:48 AM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Help with adding multiple images to an application

    [.code] [./code] taking out the periods.

    hey guys how do i display a tag with out it being executed?
    I would have liked to type out [.code] [./code] with out the periods, but it displaying the characters instead executing
    Last edited by JonLane; February 25th, 2012 at 12:09 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with adding multiple images to an application

    Thanks man, I fixed the OP now.

    But back to the original question... does anybody have a fix for me?
    Last edited by Jumbosize; February 25th, 2012 at 01:37 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help with adding multiple images to an application

    load multiple different image files and display them separately
    Can you explain your problem? I see that you are using ImageIO class's read method to read files
    and are using the Graphics class's drawImage method to draw images.

    If you are getting errors you need to tell us. Copy and paste the full text of the error messages here.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with adding multiple images to an application

    Actually I'm not getting any errors, it just plain will not display the second image file. My problem is that i just can't figure out how to make it display that image. Sorry if I was unclear.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help with adding multiple images to an application

    One problem I see is that you are not telling the layout manager where to place the JPanels that you are adding to the frame. Your setting of the contentpane and adding to the contentpane seems confused.
    Also where do you assign a value to the image2 variable in the ImagePanel class?

    Why not create two instances of the class and add them in different places.
    Or create one instance of the class and give it the two images so it can draw them.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with adding multiple images to an application

    Ah, thank you very much Norm. I took your advice and added a second argument to the constructor so it is now able to load two different images at once. However, I have a new problem. I started wondering how I could add a very large assortment of images to the program. I'm sure I could just keep adding on to the constructor, but that would get to be very, very cluttered. My thought was that I could just create an array of BufferedImages and use that as the argument for my constructor. I worked on it a little and came up with this code:

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class ImagePanel extends JPanel {
     
        public ImagePanel(BufferedImage images[]) {
            this.image = images[0];
        }
     
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
     
            g.drawImage(image[0], 50, 50, this);
            g.drawImage(image[0], 100, 100, this);
            g.drawImage(image[0], 120, 120, this);
     
        }
     
        public static void main(String[] args) throws IOException {
        		BufferedImage image[] = new BufferedImage[1];
           	image[0] = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile1.png"));
           	image[1] = ImageIO.read(new File("http://www.javaprogrammingforums.com/images/tile2.png"));
    		ImagePanel contentPane = new ImagePanel(image[]);
     
            JFrame f = new JFrame("dslkh");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(contentPane);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }


    Unfortunately I get an error when trying to compile it. I get this message:

    C:\Users\Drew\Documents\JCreator LE\MyProjects\ImagePanel\src\ImagePanel.java:59: error: '.class' expected
    ImagePanel contentPane = new ImagePanel(image[]);

    I don't have a clue what that means. Granted I'm pretty new to Java, but I've never seen anything like that before. Do you have any advice for how to fix this?
    Also, is there a better way to set this up than the way I am? This was just the simplest way I could think to get multiple images.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help with adding multiple images to an application

    You don't use the array referencing notation: ([]) when passing an array. Just use the name of the array.
    Leave off the []s

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with adding multiple images to an application

    Thank you yet again! I was able to fix up my code and it now does exactly what I want. I can't thank you enough for all of your help. I really appreciate everything that you have done for me.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Help with adding multiple images to an application

    Glad I could help.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Jumbosize (February 26th, 2012)

Similar Threads

  1. adding rectangles in images
    By mserrao in forum AWT / Java Swing
    Replies: 4
    Last Post: September 1st, 2011, 06:27 AM
  2. Please help!!! Adding java app to a web application
    By snapper in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2011, 10:26 AM
  3. Saving multiple images
    By Dario in forum Java Theory & Questions
    Replies: 3
    Last Post: January 25th, 2011, 12:59 PM
  4. adding multiple ints into a string
    By straw in forum Java Theory & Questions
    Replies: 1
    Last Post: March 18th, 2010, 06:02 PM
  5. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 03:15 AM