Results 1 to 6 of 6
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default [new] need help on GUI adding images

    hi all,

    well im just confused on how i would create an object from one class and display it on another class.

    For example on my main class i have created a frame which just has a blue background.

    I have an crab class where i want to create like crab objects.
    What im stuck on is how to i give a image of a crab ive got to the objects that i made? Would i somehow in the crab constructors give the image? or does the image not go in the constructors?

    Also how when the crab objects have an image would i display it on the frame i create on the main class?

    thanks everyone for your time

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I hate to respond this way, but how much Java experience do you have?

    Here's the short answer.

    Create an ImageIcon, add it to a JLabel, and set the properties of the JLabel appropriately to cause the image to display the way you want.

    In your JFrame, start by adding a JPanel that fills the entire ContentPane (use GridLayout with one cell). This is a good practice for *all* GUI's. Then add the JLabel to the JPanel.

  3. #3
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Thank you for your reply. I have started learning java for about 6 months, so i guess im pretty much a begginner.

    Ive looked on the interent aswell for different ways but some just seeem comfusing, ill have a look on the internet for methods to implement your way.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Sun's Java Tutorial is the best place to start...

    GUI programming in Java is powerful, but it is also complicated at first. There is a right way to do things, and not following it will lead to all kinds of problems. You want to read the "Swing" section.

  5. #5
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    I have looked at turtorials regarding swing.

    I have now created a class which extends JFrame which has a constructor which creates an JFrame and creates a JPanel

    (This is what creats the panel) // JPanel panel1 = new JPanel();

    adds the panel to the frame, // add(panel1);

    creates a imageicon of the crab image1 // ImageIcon image1 = new ImageIcon("crab.jpg");

    creates the JLabel and it holds the crab image // JLabel label1 = new JLabel(image1);

    and then adds the label to the panel //panel1.add(label1);

    then sets it to show // setVisible(true);

    in the main method of the program i will call this constructor and it willl create the frame and the image of one crab.

    This works and shows the picture, however what im confused on is what is the way to create more crabs and show them on the JPanel? For example say i want to create four crabs from a crab class i have, i want to create four crab objects which show on the JPanel so when the program is run four crab images are shown.
    Last edited by counterfox; 03-19-2009 at 08:24 AM.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Given what you have done so far, simply create more JLabel's and add them to the JPanel. Use a layout manager to control their positions.

Similar Threads

  1. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 11:19 AM
  2. images
    By amith in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2008, 09:38 PM
  3. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 11:54 AM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 06:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 07:01 AM

Posting Permissions

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