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

    Default Why isn't this working?

    I've been searching and searching for 2 days to find out why this doesn't work, I really need help.

    This is my main class, I don't believe this is the problem, though:
    Java Code:
    public class Nick {
    	public static void main(String[] args){
    		
    		TheWindow w = new TheWindow();
    		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		w.setSize(700,500);
    		w.setVisible(true);
    	}
    	
    	
    }
    This is the class that I'm pretty sure is where something is going wrong. My problem is, when I run it, it's not putting the image on the JPanel

    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 Image loadPics(){
    	sb = new ImageIcon(getClass().getResource("Start.png")).getImage();
    	return sb;
    }
    
    	  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);
          if(sb != null) {
                g.drawImage(sb, 0, 0, this);
            }
    }
    
    }

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

    Default

    Your question was answered in your other thread here. Just writing code does not mean it will be called, you need to call the method loadPics. I provided a link in your previous post about calling methods, I recommend you read it thoroughly, as well as re-reading all the posts in your previous threads you started asking the identical question

  3. #3
    bigka79 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Took me a minute looking at this, but you never tell the program to draw. You need use the loadPics method by putting it in the constructor.

  4. #4
    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 bigka79 View Post
    Took me a minute looking at this, but you never tell the program to draw. You need use the loadPics method by putting it in the constructor.
    He's been told this again, and again, and again. Best to just save your breath and wait for this poster to study some basic Java, as that's what he needs to do before he can understand our recommendations.

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

    Default

    I'm trying. My last post in my thread wasn't answered, most likely because you guys were getting annoyed with me, and I understand that.
    I've read all of the posts over and over again, I've done many things, I just can't figure it out.
    I was hoping to get a straight up answer on what I need to write and where, so I can understand why.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Seriously.

    Despite being told exactly what the problem is you still cannot resolve it. Once again, you have a method called loadPics which "loads" the image file ready for the rest of the code to use. BUT you never call that method. If it never gets called the image is never loaded so how can you display it?

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

    Default

    if(sb != null) {
    g.drawImage(sb, 0, 0, this);
    }

    I wrote that, at the bottom, and in the loadPics(), it loads the image.
    Then, with that at the bottom, should that not draw the image that was loaded and assigned to the image variable sb?
    That's what I understand right now, which is why I'm confused.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Change your code to this
    Java Code:
    if(sb != null) {
        g.drawImage(sb, 0, 0, this);
    } else {
        JOptionPane.showMessageDialog(null, "Im a doofus because I never called the loadPics method");
    }

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

    Default

    But I still don't know what it means to call the loadPics method, nor does it make sense that you need to also "call" a method, when it's clearly written what the method does, assigns the sb variable to that image, then at the bottom of the code, what I'm doing is using the image variable sb. This makes sense in my mind, I really just need someone to tell me what to put and where.
    I do understand that I need to call the loadPics method, but I do not know how to do that. Care to give me a link to a tutorial for that? The link I received in the other thread does not teach anything about calling methods.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Oh come on, exercise your grey matter a tiny bit. Your code:
    Java Code:
    public TheWindow(){
    	super("ProjectN (Un-named game)");
    	myPanel = new StockMarket();
    	myPanel.setBackground(customColor);  // calls a method
    	add(myPanel, BorderLayout.CENTER); // calls a method
    }
    public Image loadPics(){
    	sb = new ImageIcon(getClass().getResource("Start.png")).getImage(); //calls 3 methods chained together
    	return sb;
    }
    
    	  public void paint(Graphics g) {
    		  if(g instanceof Graphics2D){
    				Graphics2D g2 = (Graphics2D)g;
    				g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // calls a method
    			}
    		  
          super.paint(g); // calls a method
          g.drawString("This is the current state of the game", 60, 100); // calls a method
          g.drawString("Things will be added soon, I'm currently working on the basic code", 60, 130); // calls a method
          if(sb != null) {
                g.drawImage(sb, 0, 0, this); // calls a method
            }

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your code is littered with method calls. If you still maintain that you do not know what a method call is then the only conclusion I can make is that the code posted is stolen.

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

    Default

    So I need to call the loadPics method, I now know what calling a method is, but what am I supposed to do to call the method? loadPics.WHAT?
    I just don't understand what I'm supposed to do TO call this method.

    Edit: Sorry, I did know what calling methods is, just didn't know that that was what I was doing.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Sweet mother of God!
    Java Code:
    sb = loadPics();
    do that somewhere before you try to display the image. How friggin hard was that?

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

    Default

    Thank you, so much.
    Sorry for being such a nuisance, I understand now and it was a dumb mistake that I knew all along, just wasn't seeing it.
    Thanks again.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Exactly and that is why people were getting frustrated with you. Do try and think for yourself in future. You won't always be able to rely on handholding.

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  2. Why is my Do/While Not working?
    By Meta in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 06:05 PM
  3. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

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
  •