April 23rd, 2003, 11:22 PM
|
#1 (permalink)
| | Best To Avoid Me
Join Date: Mar 2002 Location: Under Your Bed
Posts: 8,596
| JAVA help needed badly!!!
Hi guys!
Ok, here's what I got going on...I'm doing a Java project that is a simple text based story/game with a few .gif images. My problem is that once my program gets to the part where the image is displayed, it won't go any further. My teacher mentioned something about having either an "OK" button to proceed or a timer, but I can't find out how to do that anywhere and it's really starting to frustrate me!
Here's my code so far.
NOTE: The program DOES NOT get past the image being displayed (the code in red, the image works just fine), so nothing beyond the Martoch.gif file is shown. The image just sits there with nothing to click.
public class Martoch extends JApplet {
private Image Martoch;
private Image Paladin;
private Image Sorcerer;
public void init() //begins method
{
//Explain purpose of this application
JOptionPane.showMessageDialog( null,
"Blah blah blah"
"Want to Play?",
JOptionPane.INFORMATION_MESSAGE );
//Show Martoch.gif Martoch = getImage( getDocumentBase (), "Martoch.gif" );
}
public void paint( Graphics g )
{
g.drawImage( Martoch, 0, 0, this );
}
String fighterName;
String fighterCity;
fighterName = JOptionPane.showInputDialog( "Please enter your name" );
fighterCity = JOptionPane.showInputDialog( "Please enter the city you hail from" );
JOptionPane.showMessageDialog( null,
"Blah blah blah",
"It's time to choose",
JOptionPane.INFORMATION_MESSAGE );
So any thoughts on this? I'm so lost...
Thanks!
Mike
__________________
~ Camp Crystal Lake counselor positions opening daily ~
|
| |
April 24th, 2003, 12:44 AM
|
#2 (permalink)
| | Best To Avoid Me
Join Date: Mar 2002 Location: Under Your Bed
Posts: 8,596
|
First bump...  |
| |
April 24th, 2003, 07:28 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 21,020
|
maybe PM strangerstill I do believe he's a Java guy  |
| |
April 24th, 2003, 08:46 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 3,421
| Quote:
public void paint( Graphics g )
{
g.drawImage( Martoch, 0, 0, this );
}
| maybe a "repaint()" w/o the quotes could do it, if you want a new picture to be repainted. Also, if you have a button, you'd need implements ActionListener [where you have public class Martoch extends JApplet (you just type in implements ActionListener right after JApplet)] then
public void actionListener(ActionEvent e)
{
}
In school, we don't use private for the Images, we just use Image=whatever; |
| |
April 24th, 2003, 05:33 PM
|
#5 (permalink)
| | Best To Avoid Me
Join Date: Mar 2002 Location: Under Your Bed
Posts: 8,596
| Quote: Originally posted by poopeyhed2
maybe a "repaint()" w/o the quotes could do it, if you want a new picture to be repainted. Also, if you have a button, you'd need implements ActionListener | I'm not exactly sure what you mean by having a new picture "repainted". Basically, my program works fine without the images...but when I have an image load anywhere in the program, the program just stops right at the image. The image has no "OK" button to click to proceed or a 5-second timer until it goes away.
I do have buttons labeled Sorceror and Paladin that the user can click to select which fighter they want...the buttons point to methods of course. I did have to add the "implements ActionListener" to the extends JApplet of course.
Thanks for your help though, I learned about the buttons last night...you were right on the money there. |
| |
April 24th, 2003, 05:54 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 3,421
|
Sorry, I must have mis read that  the repaint() method refreshes everything that you see, which was what I was originally thinking, where your first picture comes up and then you don't see any other picture after that.
Sorry I ca'nt be that much help, as I too am a newbie in Java.. In school, we use BlueJ, seems like a pretty good piece of software, and especially that it's free  |
| |
April 24th, 2003, 07:15 PM
|
#7 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
(I reckon this ought to be a sticky on this forum, but) USE THE CODE TAG!
You're indenting your code (I'm hoping) in the Allman style (I'm a 1TBS man, but I learnt C from K&R) - so post using the CODE tag so it shows up in your post! - it took me ages to realise what's wrong with your code precisely because it's not indented!
Oh, you wanna know what *is* wrong with it? Everything after the paint() method is in the class block instead of a method block (next indent level) so it doesn't get regarded as executable code, so of course it doesn't get run.
You're running the applet from the init thread - this is A Bad Thing but it could work for now. To get the image to show and then continue with your code you need to keep the code in one uninterrupted method and place a repaint() or similar call at the relevant point in the code: Code: Martoch = getImage(getDocumentBase (), "Martoch.gif");
repaint();
String fighterName; Do they actually teach you the philosophy of object oriented programming before throwing you in and making you write code? Your code above would make sense in a run-time, dynamically typed language like JavaScript, but Java is a lot less sloppy... |
| |
April 24th, 2003, 10:40 PM
|
#8 (permalink)
| | Member
Join Date: Feb 2003 Location: France
Posts: 55
|
Hey Martoch,
I got your code working by changing it to the following Code: import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class Martoch extends JApplet
{
private Image Martoch;
private Image Paladin;
private Image Sorcerer;
public void init() //begins method
{
//Show Martoch.gif
Martoch = getImage( getDocumentBase (), "Martoch.gif" );
}
public void start()
{
//Explain purpose of this application JOptionPane.showMessageDialog( null, "Blah blah blah",
"Want to Play?", JOptionPane.INFORMATION_MESSAGE );
String fighterName = JOptionPane.showInputDialog( "Please enter your name" );
String fighterCity = JOptionPane.showInputDialog( "Please enter the city you hail from" );
JOptionPane.showMessageDialog( null, "Blah blah blah",
"It's time to choose", JOptionPane.INFORMATION_MESSAGE );
}
public void paint( Graphics g )
{
g.drawImage( Martoch, 0, 0, this );
}
} Is that any help / wot u wanted? |
| |
April 24th, 2003, 10:43 PM
|
#9 (permalink)
| | Best To Avoid Me
Join Date: Mar 2002 Location: Under Your Bed
Posts: 8,596
|
Ok I'm sort of following you and I'm very appreciative of your help!
However, this is my current code: Code:
public void init() //begins method
{
//Explain purpose of this application
JOptionPane.showMessageDialog( null,
"Think you have what it takes" +
"\n to survive in the dark realm of Sylox?" +
"\nMuhahahaha...think again my young friend." +
"\nMany a warrior has stepped forward with" +
"\nheavy hand and brave heart...combining" +
"\ncourage and strength in an attempt to" +
"\nrelease the world from HIS mighty grip" +
"\nAll have failed...HE is an unmatched force," +
"\nseemingly immortal...can you defeat HIM?" +
"\nWho is HE, you ask...why, you shall soon see.",
"Want to Play?",
JOptionPane.INFORMATION_MESSAGE );
Martoch = getImage( getDocumentbase(), "Martoch.gif");
repaint();
String fighterName; Ignore the indentations in the showMessageDialog code, it all lines up in notepad. Well now when I try to compile it I get a "Cannot resolve symbol" error with the little arrow pointing to the g in getDocumentbase.
I'm terribly sorry if I'm being very "Java stupid" right now, but this is actually more advanced than we got in the class. I just really want my image to show up, then somehow be able to continue on with my code.
So, what am I doing wrong now?
EDIT: trying your code now!!!
THANKS!
Last edited by Martoch : April 24th, 2003 at 10:50 PM.
|
| |
April 24th, 2003, 10:56 PM
|
#10 (permalink)
| | Best To Avoid Me
Join Date: Mar 2002 Location: Under Your Bed
Posts: 8,596
| Quote: Originally posted by DJDaveMark Hey Martoch,
I got your code working by changing it to the following[code}[/code]Is that any help / wot u wanted? | Well the only "issue" with that code is that the image isn't displayed until the end of the applet...I need it to be displayed in the middle of the applet, then go away so the rest of the text can be shown and the code can continue.
Clear as mud? |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |