#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106

    It wont display this image...


    I have this code, and i put the image in the same folder as the .java file but it wont display the image.

    Changed the code
    Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.*;
    
    // This is a simple example applet that loads an image and
    // displays it.
    
    public class MyFirst extends Applet
    {
         Image pic;
    
         public void init()
         {
            
         }
    
         public void paint(Graphics g)
         {
              Image pic = Toolkit.getDefaultToolkit().getImage("C.gif");
              if(pic !=null)g.drawImage(pic,15,5, this);
         }
    }
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    I keep trying i got an example from my book but no luck, is there something special i have to do with the file besides put it in the same folder witht the .java file?
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Originally Posted by swattkidd
    I keep trying i got an example from my book but no luck, is there something special i have to do with the file besides put it in the same folder witht the .java file?
    Hmmm, i still cant figure this out, I tried putting it so if the pic == null then it would write some text but it didnt do that either.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Omg What is going on! I am having the same problem with other files, In this program it says that it cant find age.txt! but i put the age.txt right next to MyFirst.java...

    Code:
    import java.io.*;
    
    public class OMG {
        public static void main(String[] args) throws IOException {
    	File inputFile = new File("age.txt");
    	File outputFile = new File("outagain.txt");
    
            FileReader in = new FileReader(inputFile);
            FileWriter out = new FileWriter(outputFile);
            int c;
    
            while ((c = in.read()) != -1)
               out.write(c);
    
            in.close();
            out.close();
        }
    }
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    In an applet, you use the method getImage to load an image.
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Originally Posted by bullet
    In an applet, you use the method getImage to load an image.
    Can you give me an example script where i can just switch the name of the image? because i tried an exact example from my book and it still wont work.
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Originally Posted by swattkidd
    Can you give me an example script where i can just switch the name of the image? because i tried an exact example from my book and it still wont work.
    My book must be messed up then because i got this script from them and it says uncompatible types

    *PLEASE SOMEONE GIVE ME AN EXAMPLE CODE THAT JUST DISPLAYS AN IMAGE BECAUSE THIS IS PISSING ME OFF*

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Graphics.*;
    
    
    public class Image extends JFrame
    {
       public Image()
       {
           super("Hey Welcome");
           setSize(300,300);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
           Container cp = getContentPane();
           
           FlowLayout flowManager = new FlowLayout();
           cp.setLayout(flowManager);
           
           JButton thing = new JButton("Ok So Far So dAmn GOOD!");
           cp.add(thing);
           cp.setBackground(Color.red);
           
           TextArea t = new TextArea("WELCOME");
           cp.add(t);
           
           TextArea c = new TextArea("CHAT");
           cp.add(c);
           
           JComboBox Games = new JComboBox();
           Games.addItem("PSP");
           Games.addItem("DS");
           Games.addItem("NGAGE LOL");
           cp.add(Games);
           
           setVisible(true);
       }
    
        public int getDefaultCloseOperation() {
        }
       public void paint(Graphics g)
       {
           Image pic = Toolkit.getDefaultToolkit().getImage("paddle.gif");
           if(pic!=null) g.drawImage(pic,15,5,this);
       } 
       public static void main(String [] args)
       {
           Image th = new Image();
       }
    
    }
  14. #8
  15. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Here is an example of displaying an image.

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ShowImage extends JApplet {
       private Image image;
    
       public void init() {
          image = getImage(getCodeBase(),"tranlobster1.gif");
    
          MediaTracker tracker = new MediaTracker(this);
    
          tracker.addImage(image,1);
    
          try {
             tracker.waitForAll();
          } catch (InterruptedException ie) {
          }
       }
    
       public void paint(Graphics g) {
          if (image != null) {
             int width = image.getWidth(this);
             int height = image.getHeight(this);
    
             g.drawImage(image,
                         getWidth()/2-width/2,
                         getHeight()/2-height/2,
                         this);
          }
       }
    }
    You can load it with this HTML.

    Code:
    <applet code=ShowImage width=400 height=400>
    </applet>
    You need to put the attached file in the same directory as the .class file.
    Attached Images
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Originally Posted by bullet
    Here is an example of displaying an image.

    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ShowImage extends JApplet {
       private Image image;
    
       public void init() {
          image = getImage(getCodeBase(),"tranlobster1.gif");
    
          MediaTracker tracker = new MediaTracker(this);
    
          tracker.addImage(image,1);
    
          try {
             tracker.waitForAll();
          } catch (InterruptedException ie) {
          }
       }
    
       public void paint(Graphics g) {
          if (image != null) {
             int width = image.getWidth(this);
             int height = image.getHeight(this);
    
             g.drawImage(image,
                         getWidth()/2-width/2,
                         getHeight()/2-height/2,
                         this);
          }
       }
    }
    You can load it with this HTML.

    Code:
    <applet code=ShowImage width=400 height=400>
    </applet>
    You need to put the attached file in the same directory as the .class file.
    I took your exact code... PLaced it in netbeans, went to go compile and it says incompatible types. and 4 other errors.!
  18. #10
  19. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by swattkidd
    I took your exact code... PLaced it in netbeans, went to go compile and it says incompatible types. and 4 other errors.!
    Have you tried compiling on the command line?
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Originally Posted by bullet
    Have you tried compiling on the command line?
    what is that? do you mean from the command prompt? no i havent tried that, and i dont think i will because i ran into so many problems doing it like that ill shot myself if i try to do that, Im very confused right now
  22. #12
  23. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by swattkidd
    what is that? do you mean from the command prompt? no i havent tried that, and i dont think i will because i ran into so many problems doing it like that ill shot myself if i try to do that, Im very confused right now
    As long as you have the correct JDK in your PATH, it should compile correctly. I just wrote it and compiled it a few minutes and almost always use the command line.
  24. #13
  25. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    106
    Originally Posted by bullet
    As long as you have the correct JDK in your PATH, it should compile correctly. I just wrote it and compiled it a few minutes and almost always use the command line.
    Yea my netbeans has been screwing up lately so i am going to uninstall it and then reinstall it.

IMN logo majestic logo threadwatch logo seochat tools logo