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.
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  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();
    Wars do not make one great.

  3. #3
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    okay, cheers
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  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...
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  5. #5
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    it's javax.swing.ImageIcon, I believe
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  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! :-)
    Wars do not make one great.

  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()
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


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

    Image myImage = Toolkit.getDefaultToolkit().getImage("my_image.jpg");
    Wars do not make one great.

  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?
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  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
    Wars do not make one great.

  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...
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  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.
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


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

  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 04:24 PM.
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  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.
    Wars do not make one great.

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15410 seconds
  • Memory Usage 3,009KB
  • Queries Executed 13 (?)
More Information
Template Usage (37):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates