Results 1 to 15 of 15
Thread: Why isn't this working?
- 02-02-2011, 04:20 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
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:
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 JPanelJava 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); } }
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); } } }
- 02-02-2011, 05:16 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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
- 02-02-2011, 09:44 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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.
-
- 02-03-2011, 01:08 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-03-2011, 01:16 AM #6
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?
- 02-03-2011, 01:19 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-03-2011, 01:24 AM #8
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"); }
- 02-03-2011, 01:29 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-03-2011, 01:33 AM #10
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 }
- 02-03-2011, 01:34 AM #11
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.
- 02-03-2011, 01:42 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-03-2011, 01:46 AM #13
Sweet mother of God!
do that somewhere before you try to display the image. How friggin hard was that?Java Code:sb = loadPics();
- 02-03-2011, 01:55 AM #14
Member
- Join Date
- Jan 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-03-2011, 02:00 AM #15
Similar Threads
-
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
Why is my Do/While Not working?
By Meta in forum New To JavaReplies: 1Last Post: 05-11-2010, 06:05 PM -
working with JC
By yuhobebbho in forum New To JavaReplies: 0Last Post: 02-10-2010, 11:22 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Working With ANT
By JavaForums in forum EclipseReplies: 0Last Post: 04-26-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks