Need help with a compilation errors
Here is my code
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
publicclass piratepanelextends JPanelimplements Runnable
{
int appletx = 500;
int applety = 500;
Image blackpearl;
Image ship1p;
Image ship2p;
Image ship3p;
Font font;
Thread animator;
Boolean running =false;
public piratepanel()
{
// Set background colour
setBackground(Color.blue);
setPreferredSize(new Dimension(appletx, applety));
// Get game sprites
blackpearl = getImage (getCodeBase(),"blackpearlf.gif");
ship1p = getImage (getCodeBase(),"ship1.gif");
ship2p = getImage (getCodeBase(),"ship2.gif");
ship3p = getImage (getCodeBase(),"ship3.gif");
// Game objects
blackpearl blackpearl =new blackpearl();
ship1 ship1 =new ship1();
ship2 ship2 =new ship2();
ship3 ship3 =new ship3();
// Set up game font
Font gamefont =new Font("Serif",Font.PLAIN,28 );
// Add action listener
addKeyListener(new KeyAdapter(){
publicvoid keyPressed(KeyEvent e)
{ processKey(e);}
});
// initialise timing elements
running =false;
}
publicvoid run()
{
}
}
And here is the command promt errors
http://img171.imageshack.us/img171/3396/compilingji0.png
What is wrong?
http://java.sun.com/j2se/1.4.2/docs/api/java/applet/Applet.htmlgetCodeBase() is a method of the Applet class. You might be looking for something like Class.getResource(). http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
getCodeBase() is a method in the Applet class. Try using this:getClass().getClassLoader().getResource("blackpearlf.gif");
whats the difference between JApplet and Applet?
> whats the difference between JApplet and Applet?One's Swing, one's not. You are aren't using either one.
I know but in my start game class there is. Which would be best do you think? Or which one is the latest if thats the right way of saying it.
> I know but in my start game class there is. Which> would be best do you think? Or which one is the> latest if thats the right way of saying it.They're two different things. It depends on what you want to do.
What's their positives and negatives?
> What's their positives and negatives?Google it.
The main advantage to using the old AWT and java.applet.Applet is that your code could run in browsers that only have an old, slow, buggy JRE plugin installed.Hmmmm :-)
> The main advantage to using the old AWT and> java.applet.Applet is that your code could run in> browsers that only have an old, slow, buggy JRE> plugin installed.For some reason I thought he was asking for the pros/cons of JPanel and JApplet...
What about the other error apart from the getCodeBase()
> What about the other error apart from the> getCodeBase()You never defined a processKey() method.
how do you do that? I'm quite new to java so I might not know a lot of things.
ReplacegetImage (getCodeBase(), "image_file");with e.g.Toolkit.getDefaultToolkit().createImage("image_file");Jukka
> how do you do that? I'm quite new to java so I might
> not know a lot of things.
I've noticed.
private void processKey(KeyEvent e)
{
//what do you want it to do?
}
I didn't mean that i meant the processKey()
Sorry CaptainMorgan08 I posted that message to the post before yours
> Toolkit.getDefaultToolkit().createImage("image_file");
The original poster is hard to understand, but if he is writing a (J)Applet, this version of createImage, which takes a string filename or path is going to fail. What's needed is a method that takes a URL:
import javax.imageio.*;
...
BufferedImage im = ImageIO.read(url);
ImageIO is better than the toolkit methods in a number of different ways, too.
So duckbill is this right? blackpearl = Toolkit.getDefaultToolkit().createImage("blackpearlf.gif");
> So duckbill is this right? What are you doing with the images? If all you are going to do it display them on a Swing (or AWT) component, then reread reply 2.
That is correct if you're writing a desktop application. Now the question is, are you trying to write an applet? If so, and in any case, see drlaszlos post above.
Yes it is a JApplet and i'm hoping to embed it into a website. So should I add what DrLaszloJamf said?
> Yes it is a JApplet and i'm hoping to embed it into a> website.Then you cannot use the Toolkit method I suggested above. I was under the assumption that you were writing a desktop application.
I'm also going to paint this to the JPanel I showed you earlier (it's not finished yet) to make a game.
So is this right then?import javax.imageio.*;String url = blackpearlf.gif;BufferedImage blackpearlf = ImageIO.read(url);How do I draw this onto the panel? Just the ordinary paintImage?
> How do I draw this onto the panel? Just the ordinary> paintImage?You aren't getting the URL correctly. Read reply 2 again.
How do you do a method that takes a URL? reply 2 doesn't seem to be anything to do with that. I'm probably wrong though.
Look:URL url= getClass().getClassLoader().getResource("blackpearlf.gif");BufferedImage blackPearl = ImageIO.read(url);
> import javax.imageio.*;
I hope this is going to end up at the top of your file, with the other imports.
> BufferedImage blackpearlf = ImageIO.read(url);
> How do I draw this onto the panel? Just the ordinary paintImage?
BufferedImage is a subclass of Image. One could even write:
Image blackpearl = ImageIO.read(url);
I'll add all this to my code and see if it works out. Thanks. Can I ask one more question? How can I make an enemy ship move about randomly. Ive worked out the random bit but i cant time every 2 seconds for it to turn another direction. The only source of time seems to be the animation thread which runs a lot of times per second. Any ideas how I could time it to change direction every 2 seconds?
I'll add all this to my code and see if it works out. Thanks. Can I ask one more question? How can I make an enemy ship move about randomly. Ive worked out the random bit but i cant time every 2 seconds for it to turn another direction. The only source of time seems to be the animation thread which runs a lot of times per second. Any ideas how I could time it to change direction every 2 seconds?
If you are already driving the animation is some way (a javax.swing.Timer, for example) , you can use [url=http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#currentTimeMillis()]System.currentTimeMillis[/url] to get the current time in milliseconds. Then you can compare this with your ""time to deke" time.
How does that differ from the animation driving thread? Also from the previous topic I have more errors :(
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.imageio.*;
public class piratepanel extends JPanel implements Runnable
{
int appletx = 500;
int applety = 500;
Font font;
Thread animator;
Boolean running = false;
public piratepanel()
{
// Set background colour
setBackground(Color.blue);
setPreferredSize( new Dimension(appletx, applety));
// Get game sprites
URL bp= getClass().getClassLoader().getResource("blackpearlf.gif");
BufferedImage blackPearl = ImageIO.read(bp);
URL s1= getClass().getClassLoader().getResource("ship1.gif");
BufferedImage ship1 = ImageIO.read(s1);
URL s2= getClass().getClassLoader().getResource("ship2.gif");
BufferedImage ship2 = ImageIO.read(s2);
URL s3= getClass().getClassLoader().getResource("ship3.gif");
BufferedImage ship3 = ImageIO.read(s3);
// Game objects
blackpearl blackpearl = new blackpearl();
ship1 ship1 = new ship1();
ship2 ship2 = new ship2();
ship3 ship3 = new ship3();
// Set up game font
Font gamefont = new Font( "Serif",Font.PLAIN,28 );
// Add action listener
addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e)
{ processKey(e); }
});
// initialise timing elements
running = false;
}
public void run()
{
}
private void processKey(KeyEvent e)
{
}
}
http://img508.imageshack.us/img508/6548/compilinglk4.png
If what you mainly want to do is interact with the GUI, I prefer javax.swing.Timer over creating your own thread that loops and sleeps because the Timer's ActionListener is called from the event dispatch thread (EDT) and so can safely interact with the GUI without needing to use invokeLater or invokeAndWait. Timer is also simpler to use.
Message was edited by:
DrLaszloJamf
Missing
import java.awt.image.BufferedImage;
You have also redefined variables
BufferedImage ship1 = ImageIO.read(s1);
//...
ship1 ship1 = new ship1();
Also consider using capital initial letters for your class names, e.g. Blackpearl instead of blackpearl.
Cheers
? I didn't understand that can you show me an example also can you help fix me errors before too?
I realize this is a steep learning curve, but here is a general programming tip: if you find yourself writing the same code over and over again, make it a subroutine. You code will be clearer, it will be easier to reuse, and if you need to alter that procedure you only have to change the code in one place:
BufferedImage blackPearl = getImage("blackpearl.gif");
BufferedImage ship1 = getImage("ship1.gif");
...
public BufferedImage getImage(String name) throws IOException {
URL url = getClass().getResource(name);
return ImageIO.read(url);
}
could you edit the above code to show me how that is implemented?
Here's a demo:
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
class Demo extends JComponent {
Image image1, image2;
public Demo() throws IOException {
setPreferredSize(new Dimension(400,300));
image1 = getImage("temp1.jpeg");
image2 = getImage("temp2.jpeg");
}
public Image getImage(String name) throws IOException {
URL url = getClass().getResource(name);
return ImageIO.read(url);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image1 != null)
g.drawImage(image1, 10, 10, null);
if (image2 != null)
g.drawImage(image2, 100, 100, null);
}
}
public class Launcher implements Runnable {
public static void main(String[] args) {
EventQueue.invokeLater(new Launcher());
}
public void run() {
try {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new Demo());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sorry I don't really understand that, why didn't my last code work anyway?
What don't you understand about it?
All of it :( the 4 ships are going to be in 4 different files so should I add the image identifier in each one of those or in the JFrame class?
Start out more simply. Read one image and display it. As to where variables should go, you'll discover that as you write the code.
Couldn't I just do it this way?
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.imageio.*;
public class piratepanel extends JPanel implements Runnable
{
int appletx = 500;
int applety = 500;
Font font;
Thread animator;
Boolean running = false;
public piratepanel()
{
// Set background colour
setBackground(Color.blue);
setPreferredSize( new Dimension(appletx, applety));
// Get game sprites
URL bp= getClass().getClassLoader().getResource("blackpearlf.gif");
BufferedImage blackPearl = ImageIO.read(bp);
URL s1= getClass().getClassLoader().getResource("ship1.gif");
BufferedImage ship1 = ImageIO.read(s1);
URL s2= getClass().getClassLoader().getResource("ship2.gif");
BufferedImage ship2 = ImageIO.read(s2);
URL s3= getClass().getClassLoader().getResource("ship3.gif");
BufferedImage ship3 = ImageIO.read(s3);
// Game objects
blackpearl blackpearl = new blackpearl();
ship1 ship1 = new ship1();
ship2 ship2 = new ship2();
ship3 ship3 = new ship3();
// Set up game font
Font gamefont = new Font( "Serif",Font.PLAIN,28 );
// Add action listener
addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e)
{ processKey(e); }
});
// initialise timing elements
running = false;
}
public void run()
{
}
private void processKey(KeyEvent e)
{
}
}
I just need to fix the bugs. How can I fix them though?
> Is anyone going to help?Dude, you've been getting lots of help for the last couple of hours. People do have things to do besides helping you through every step the instant you post your question. Half an hour is not a long time to wait.
jverda at 2007-7-21 17:10:27 >

> Couldn't I just do it this way?Try it and see. If it works, then yes. If it doesn't, then no.> I just need to fix the bugs. How can I fix them> though?You might get better help if you provide details about how these bugs are manifesting themselves.
jverda at 2007-7-21 17:10:27 >

> manifesting themselves?
You say you have bugs. That means you observed something that is different from the requirements. Maybe it was an error message at compile time. Maybe an error message at runtime. Maybe no error message, but the behavior was different from what you expected.
Provide details about what problems you're having, what undesirable behavior you're observing.
jverda at 2007-7-21 17:10:27 >

> I gave a screenshotThat was way back in the beginning, wasn't it? You haven't made any progress?
jverda at 2007-7-21 17:10:27 >
