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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Images

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    Default Images

    Hi there. I'm new to programming and taking my first Java programming class. We're currently working on GUI things and I was given the assignment of working on panels and images.

    Here are the instructions:
    II. Read, run, and understand the sample program: NextedPanels and LabelDemo on pages 149/152.
    III. Write a program that displays a frame containing at least four panels. Each panel should contain two images (use four unique images- your choice). Fix the size of the first panel so that both of its images remain side by side. Allow the other panel to change size as needed. Experiment with the size of the window to see the images change orientation. Make sure you understand why the application behaves as it does.
    However, I am not sure on how to do one thing - How do I display two images in one panel? Like, how would I put the akali and viktor gifs into the League of Legends panel to be displayed, side by side?

    Here is what I have so far:
    import java.awt.*;
    import javax.swing.*;
    public class Images{
       public static void main (String[] args)
          JFrame frame = new JFrame ("Nested Panels  -   HOSHI");
          frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     		ImageIcon devil = new ImageIcon ("devil.gif");
    		ImageIcon akali = new ImageIcon ("akali.gif");
    		ImageIcon sketch = new ImageIcon ("sketch.gif");
    		ImageIcon victor = new ImageIcon ("viktor.gif");
    		ImageIcon deva = new ImageIcon ("deva.gif");
    		JLabel label1, label2, label3, label4;
          JPanel subPanel1 = new JPanel();
          subPanel1.setPreferredSize (new Dimension(150, 100));
          subPanel1.setBackground (Color.green);
          label1 = new JLabel ("Devil Left", devil, SwingConstants.CENTER);
          subPanel1.add (label1);
          JPanel subPanel2 = new JPanel();
          subPanel2.setPreferredSize (new Dimension(400, 150));
          subPanel2.setBackground (Color.red);
          label2 = new JLabel ("Art in Progress", akali, SwingConstants.CENTER);
    		label2.setHorizontalTextPosition (SwingConstants.LEFT);
    		label2.setVerticalTextPosition (SwingConstants.CENTER);
          subPanel2.add (label2);
    		JPanel subPanel3 = new JPanel();
          subPanel3.setPreferredSize (new Dimension(400, 150));
          subPanel3.setBackground (Color.red);
          label3 = new JLabel ("Girl Characters", deva, SwingConstants.CENTER);
    		label3.setHorizontalTextPosition (SwingConstants.CENTER);
    		label3.setVerticalTextPosition (SwingConstants.BOTTOM);
          subPanel3.add (label3);
    		JPanel subPanel4 = new JPanel();
          subPanel4.setPreferredSize (new Dimension(400, 150));
          subPanel4.setBackground (Color.red);
          label4 = new JLabel ("League of Legends", viktor, SwingConstants.CENTER);
    		label4.setHorizontalTextPosition (SwingConstants.RIGHT);
    		label4.setVerticalTextPosition (SwingConstants.CENTER);
          subPanel4.add (label4);
          JPanel primary = new JPanel();
          primary.setBackground (Color.blue);
          primary.add (subPanel1);
          primary.add (subPanel2);
    		primary.add (subPanel3);
    		primary.add (subPanel4);

    Thanks for reading.

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    My Mood
    Thanked 263 Times in 232 Posts

    Default Re: Images

    In java a LayoutManager can assist you in placement of components within the panel as you desire. The link I provided has visual layout examples to help you decide which manager will best suit your needs. Be sure to read them all as some of them have subtle but important differences in their use/strenghts/weaknesses.

Similar Threads

  1. switch between 2 images
    By mohamed_saleh2012 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 12th, 2012, 05:22 AM
  2. Drawing 3D images
    By SrideviSridhar in forum Java Theory & Questions
    Replies: 2
    Last Post: December 30th, 2011, 12:49 AM
  3. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM
  4. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 04:19 PM