Results 1 to 6 of 6
  1. #1
    Miwrath's Avatar
    Miwrath is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Exclamation How to put multiple image in java?

    ahm, im newbie!

    i just got problem about my work...
    i just want to make a 4 picture inside a jframe...
    [i want to put another 3 pictures inside the frame.]

    but im confused i tried anything but it does'nt work...

    help me please...

    here's the code:


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class ImageApplet extends Applet
    {
      public static final int IDEAL_WIDTH = 500;
      public static final int IDEAL_HEIGHT = 500;
    
      private Image loadImage(String name)
      {
        Image result = null;
        MediaTracker tracker = new MediaTracker(this);
    
        Toolkit toolkit = Toolkit.getDefaultToolkit();
    
        result = toolkit.getImage(name);
        tracker.addImage(result, 0);
        try
        {
          tracker.waitForAll();
        } catch (InterruptedException e)
        {
          return null;
        }
    
        return result;
      }
    
      public void paint(Graphics g)
      {
        Image see = loadImage("E:\\Miwrath\\pan.jpg");
        g.drawImage(see, 0, 0, 200,300,this);
      }
     
    
      public static void main(String args[])
      {
        Applet applet = new ImageApplet();
        Frame frame = new Frame();
        frame.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.exit(0);
          }
        });
    
        frame.add(applet);
        frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
        frame.show();
        
        
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Miwrath View Post
    i just got problem about my work...
    i just want to make a 4 picture inside a jframe...
    [i want to put another 3 pictures inside the frame.]
    I see no code that will create a JFrame here.

  3. #3
    Miwrath's Avatar
    Miwrath is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    ahm...thats it...
    im sorry Sir, but i need your help to add some pictures into it...
    it has a frame...but i just use an applet...
    i admit im just a beginner...im starting to knew things out of this language...
    im still studying...

    how can i modify it Sir...
    Last edited by Miwrath; 10-09-2010 at 06:01 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    My suggestion: get rid of the code you have. Instead create a true JFrame object, create four ImageIcons loaded with your images (the tutorials will show you how), place your ImageIcons into JLabels, and add the JLabels to the JFrame's contentPane. Perhaps the contentPane could use a GridLayout(2, 2) to show all four images the same size. Again, the tutorial will give you the best information on the details on how to do this.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    public void paint(Graphics g)
      {
        Image see = loadImage("E:\\Miwrath\\pan.jpg");

    I can't think of any valid reason why you would want to load an image inside the paint() method. This method is called every time a portion of the applet needs repainting. Better to load just once.

    Java Code:
    public static void main(String args[])
      {
        Applet applet = new ImageApplet();

    Are you sure you want to do this? Generally speaking applets run in a "hosted" environment like a web browser, or a special purpose applet viewer. They don't need a main() method to get started.

    Creating an applet instance without the environment it needs the way you do here can lead to grief.

    So it would help if we got this straight are you writing an applet or an application? (or something that can be made to function as either.)

    If you are writing an applet I would suggest subclassing JApplet and doing away with the main() method. The are plenty of details in Oracle's Tutorial.

    ------------------

    [edit] SLow post, sorry. ok forget all that if you don't specifically want an applet for some reason. ("Applet" is not a word for any old Java application.)
    Last edited by pbrockway2; 10-09-2010 at 06:06 AM.

  6. #6
    Miwrath's Avatar
    Miwrath is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    ahm..@pbrockway2..thank you so much sir...
    i do appreciate your effort for helping and informing me...
    i am really trying to learn more and more...

    @Fubarable...thank you so much sir!
    :)

Similar Threads

  1. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 04:48 PM
  2. Multiple Java in a single BOX
    By luke_devon in forum New To Java
    Replies: 2
    Last Post: 07-05-2010, 02:59 PM
  3. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  4. multiple inheritance in java
    By pawanspace in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 05:08 AM
  5. Multiple Inheritance in java
    By paty in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 03:25 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
  •