Results 1 to 14 of 14
  1. #1
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Question Can't put the image on the screen??

    For some reason, when I run this code, it won't put the Start.png image on the screen! it puts the 2 Strings on the screen, but not the image..
    Any help?
    P.S, ignore the unneeded imports at the beginning.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    
    public class TheWindow extends JFrame{
            private StockMarket myPanel;
    	Color customColor = new Color(100,149,237);
    	private boolean loaded;
    	private Image sb;
    	
    	public TheWindow(){
    		super("ProjectN (Un-named game)");
    		myPanel = new StockMarket();
    		myPanel.setBackground(customColor);
    		add(myPanel, BorderLayout.CENTER);
    	}
    	public void loadPics(){
    		sb = new ImageIcon(getClass().getResource("Start.png")).getImage();
    		loaded = true;
    		repaint();
    	}
    		  public void paint(Graphics g) {
    			  if(g instanceof Graphics2D){
    					Graphics2D g2 = (Graphics2D)g;
    					g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    				}
    	      super.paint(g);
    	      g.drawString("This is the current state of the game", 60, 100);
    	      g.drawString("Things will be added soon, I'm currently working on the basic code", 60, 130);
    	      g.drawImage(sb, 10, 10, null);
    	}
    		  
    	
    }
    Last edited by nickburris; 01-31-2011 at 12:31 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    First, I recommend using paintComponent rather than paint, doing so on something like a JPanel (see Trail: 2D Graphics (The Java™ Tutorials) ). Second, you define a method to load pics but never use it.

  3. #3
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Yes I do use it,
    " g.drawImage(sb, 10, 10, null);" is at the bottom.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Quote Originally Posted by nickburris View Post
    Yes I do use it,
    " g.drawImage(sb, 10, 10, null);" is at the bottom.
    Did you actually read my post? I'm not talking about g.drawImage, I'm talking about loadPics()...if this is never called the image you try to draw is null.

  5. #5
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    I don't understand what you mean.. I'm kind of new to this, I don't know what you mean by "calling" loadPics

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Your class has a method loadPics(). This method creates the Image which you draw later on. If you do not call this method (the way it is currently written), the Image variable sb will never be assigned (eg it will be Null). Call this method in your constructor to assign the sb variable. If you don't know how to call the method, I suggest reading Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  7. #7
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    Your class has a method loadPics(). This method creates the Image which you draw later on. If you do not call this method (the way it is currently written), the Image variable sb will never be assigned (eg it will be Null). Call this method in your constructor to assign the sb variable. If you don't know how to call the method, I suggest reading Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Okay, I kind of understand now, but where would I write the code that calls the method loadPics?

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Once again, did you read my posts? The answer is right there...I'm done going in circles with this thread.

  9. #9
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Okay. Thanks anyways.
    Quote Originally Posted by nickburris View Post
    I'm kind of new to this

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

    Default

    He told you that you can call the code in your constructor for this class. We can help you much better if you clarify what exactly confuses you. Otherwise all of our frustration levels may increase.

  11. #11
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    I'm confused because I don't know where to put the code that calls the method loadPics

  12. #12
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    And I need to call it to "assign" the sb variable, but I though I WAS assigning it when I wrote
    Java Code:
    sb = new ImageIcon(getClass().getResource("Start.png")).getImage();
    because I'm assigning the sb variable to that image..

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

    Default

    Quote Originally Posted by nickburris View Post
    I'm confused because I don't know where to put the code that calls the method loadPics
    What is confusing about "put it in the constructor for this class"? If you don't understand this, you shouldn't be trying to do GUI coding just yet, but should first go through a basic tutorial or book. Recommendations don't come any more basic than that.

  14. #14
    nickburris is offline Member
    Join Date
    Jan 2011
    Posts
    30
    Rep Power
    0

    Default

    Okay, I understand that part, but what's confusing now is I though I was assigning the sb variable when I wrote
    Java Code:
    sb = new ImageIcon(getClass().getResource("Start.png")).getImage();

Similar Threads

  1. Replies: 1
    Last Post: 01-27-2011, 08:14 AM
  2. Replies: 0
    Last Post: 07-01-2010, 03:56 PM
  3. Replies: 1
    Last Post: 04-21-2010, 02:28 AM
  4. displaying image into screen !
    By burningflower12 in forum AWT / Swing
    Replies: 14
    Last Post: 11-16-2009, 02:02 AM
  5. ImageSearch in Java (find an image on the screen)
    By ribbs2521 in forum New To Java
    Replies: 7
    Last Post: 02-05-2009, 05:48 AM

Tags for this Thread

Posting Permissions

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