www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: getting image from file in standalone application?

  1. #1
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828

    getting image from file in standalone application?

    I've used getImage() to get an image from a file before in applets, but I just found out that it's a method of java.applet.Applet... is there an equivalent for a window in a standalone application? I'm using a class which extends JFrame.

  2. #2
    Join Date
    Oct 2004
    Posts
    12
    you could use Toolkit.getImage()...but an easier way:

    Image myImage = new ImageIcon("my_image.jpg").getImage();

  3. #3
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    okay, cheers

  4. #4
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    do I need to import any additional packages/Objects? it cannot resolve symbol (ImageIcon)

    I don't get how a different object's constructor can be used on the Image variable...

  5. #5
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    it's javax.swing.ImageIcon, I believe

  6. #6
    Join Date
    Oct 2004
    Posts
    12
    BTW:

    the ImageIcon class is located in javax.swing

    the Image abstract class is located in java.awt, and cannot be instantiated.

    Hope that helps! :-)

  7. #7
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    yeah I know Image cannot be instantiated, i just didn't understand how a different object's constructor could construct an image.

    well, I'm more used to getImage(URL) so I'll use Toolkit.getImage()

  8. #8
    Join Date
    Oct 2004
    Posts
    12
    OK, here is an example for that then:

    Image myImage = Toolkit.getDefaultToolkit().getImage("my_image.jpg");

  9. #9
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    don't worry, I figured that part out already. however, when I draw the image to the buffer, and then to the window, the window is just entirely gray. any reason for this?

  10. #10
    Join Date
    Oct 2004
    Posts
    12
    I assume that you have extended JFrame?

    If so, you can use this method for drawing:

    public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(myImage, x, y, null);
    }

    where:
    Image myImage = ...;
    int x = ...;
    int y = ...;
    null - this is an ImageObserver component which isn't necessary

  11. #11
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    It also now throws a large list of exceptions under java.lang.nullPointerException in the command prompt...

  12. #12
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    Originally posted by Java_Warrior
    I assume that you have extended JFrame?

    If so, you can use this method for drawing:

    public void paint(Graphics g) {
    super.paint(g);
    g.drawImage(myImage, x, y, null);
    }

    where:
    Image myImage = ...;
    int x = ...;
    int y = ...;
    null - this is an ImageObserver component which isn't necessary
    hey I know that much don't treat me like a fool. I used buffer.drawImage(image,0,0,null); then drew the image to the window in the paint function. but originally it made the whole window grey - now it just leaves it as it is and paints nothing. a little help?

    plus, the exceptions in the command prompt come up when it's run.

  13. #13
    Join Date
    Oct 2004
    Posts
    12
    Can you post your code please? That way I can look it over and help.

  14. #14
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    it's the very beginning of the main structure for a game i'm creating. It should be fairly clear as it's well commented.

    Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    
    
    
    public class Game extends JFrame
    implements MouseListener,MouseMotionListener,KeyListener,WindowListener
    {
    final int Y_ZERO=31;									//Allow for titlebar height
    final int X_ZERO=5;									//Allow for edge width
    
    
    static Game gM;										//stores the application object
    
    Graphics backstage;									//object for adding to the buffer
    Image buffer;										//buffer
    
    
    Color startBackCol=new Color(80,10,80);							//startGame background colour
    Color startForeCol=new Color(40,40,40,110);						//startGame foreground colour - 4th arg = opacity
    Font startHeaderFont=new Font("Plump MT",Font.BOLD,45);					//startGame header font
    Color startHeaderCol=new Color(120,50,120);						//startGame header colour
    
    
    
    
    int sGB=20;
    
    
    Toolkit tK;										//contains various useful functions
    
    Image startBackImg=Toolkit.getDefaultToolkit().getImage("bkG.jpg");			//background image storage
    
    
    public static void main(String[] args)
    {
    gM=new Game();										//make a new application object and store it in gM
    }
    
     public Game()
      {
      super("insert name here");								//names the window
      
      //LISTENERS------------------------------------------------------------------------------------------------------------->>>>>
      addMouseListener(this);								//mouse
      addMouseMotionListener(this);								//mouse movement
      addKeyListener(this);									//keyboard
      addWindowListener(this);								//window
      //LISTENERS------------------------------------------------------------------------------------------------------------->>>>>
      
      
      setSize(800,500);									//application window's dimensions
      
      setVisible(true);									//make the window visible
      
      setResizable(false);									//make the window unresizeable
      
      buffer=createImage(size().width,size().height);					//buffer initialisation
      backstage=buffer.getGraphics();							//initialises Graphics variable to draw to buffer
      
      startGame(backstage);									//begin the game
      }
    
     public void startGame(Graphics g)
      {
      g.setColor(startBackCol);
      g.fillRect(0,0,size().width,size().height);
      g.drawImage(startBackImg,0,0,this);
      g.setColor(startForeCol);
      g.fillRoundRect(X_ZERO+sGB,Y_ZERO+sGB,size().width-(sGB*2)-(X_ZERO*2),size().height-(sGB*2)-(X_ZERO+Y_ZERO),5,5);
      g.setFont(startHeaderFont);
      g.setColor(startHeaderCol);
      
      repaint();
      }
    
     public void paint(Graphics g)
      {
      g.drawImage(buffer,0,0,null);								//draw the buffer to the screen
      }
     public void update(Graphics g)
      {
      paint(g);
      }
    
    //LISTENER FUNCTIONS----------------------------------------------------------------------------------------------------->>>>>
    public void mouseReleased(MouseEvent m)
     {
     
     }
    public void mousePressed(MouseEvent m)
     {
     
     }
    public void mouseEntered(MouseEvent m)
     {
     
     }
    public void mouseExited(MouseEvent m)
     {
     
     }
    public void mouseClicked(MouseEvent m)
     {
     
     }
    public void mouseMoved(MouseEvent m)
     {
     
     }
    public void mouseDragged(MouseEvent m)
     {
     
     }
    public void keyTyped(KeyEvent k)
     {
     
     }
    public void keyPressed(KeyEvent k)
     {
     
     }
    public void keyReleased(KeyEvent k)
     {
     
     }
    public void windowOpened(WindowEvent w)
     {
     
     }
    public void windowClosing(WindowEvent w)
     {
     System.exit(0);
     }
    public void windowClosed(WindowEvent w)
     {
     
     }
    public void windowIconified(WindowEvent w)
     {
      
     }
    public void windowDeiconified(WindowEvent w)
     {
     
     }
    public void windowActivated(WindowEvent w)
     {
     
     }
    public void windowDeactivated(WindowEvent w)
     {
     
     }
    //LISTENER FUNCTIONS----------------------------------------------------------------------------------------------------->>>>>
    }
    Last edited by agent_x91; 10-12-2004 at 05:24 PM.

  15. #15
    Join Date
    Oct 2004
    Posts
    12
    Originally posted by agent_x91
    hey I know that much don't treat me like a fool. I used buffer.drawImage(image,0,0,null); then drew the image to the window in the paint function. but originally it made the whole window grey - now it just leaves it as it is and paints nothing. a little help?

    plus, the exceptions in the command prompt come up when it's run.
    I'm not treating you like a fool. It is difficult for me to gauge your problems without seeing your code. Also, you don't need to paint the image to a buffer. Swing components are usually double buffered by default anyway.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles