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?

[2822 byte] By [Chris1234554a] at [2007-11-26 17:40:32]
# 1
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
kevjavaa at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 2
getCodeBase() is a method in the Applet class. Try using this:getClass().getClassLoader().getResource("blackpearlf.gif");
CaptainMorgan08a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 3
whats the difference between JApplet and Applet?
Chris1234554a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 4
> whats the difference between JApplet and Applet?One's Swing, one's not. You are aren't using either one.
CaptainMorgan08a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 5
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.
Chris1234554a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 6
> 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.
CaptainMorgan08a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 7
What's their positives and negatives?
Chris1234554a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 8
> What's their positives and negatives?Google it.
CaptainMorgan08a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 9
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 :-)
DrLaszloJamfa at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 10
> 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...
CaptainMorgan08a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 11
What about the other error apart from the getCodeBase()
Chris1234554a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 12
> What about the other error apart from the> getCodeBase()You never defined a processKey() method.
CaptainMorgan08a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 13
how do you do that? I'm quite new to java so I might not know a lot of things.
Chris1234554a at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 14
ReplacegetImage (getCodeBase(), "image_file");with e.g.Toolkit.getDefaultToolkit().createImage("image_file");Jukka
duckbilla at 2007-7-9 0:08:43 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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?

}

CaptainMorgan08a at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 16
I didn't mean that i meant the processKey()
Chris1234554a at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 17
Sorry CaptainMorgan08 I posted that message to the post before yours
Chris1234554a at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 18

> 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.

DrLaszloJamfa at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 19
So duckbill is this right? blackpearl = Toolkit.getDefaultToolkit().createImage("blackpearlf.gif");
Chris1234554a at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 20
> 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.
CaptainMorgan08a at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 21
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.
duckbilla at 2007-7-21 17:10:17 > top of Java-index,Java Essentials,Java Programming...
# 22
Yes it is a JApplet and i'm hoping to embed it into a website. So should I add what DrLaszloJamf said?
Chris1234554a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 23
> 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.
duckbilla at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 24
I'm also going to paint this to the JPanel I showed you earlier (it's not finished yet) to make a game.
Chris1234554a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 25
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?
Chris1234554a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 26
> How do I draw this onto the panel? Just the ordinary> paintImage?You aren't getting the URL correctly. Read reply 2 again.
CaptainMorgan08a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 27
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.
Chris1234554a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 28
Look:URL url= getClass().getClassLoader().getResource("blackpearlf.gif");BufferedImage blackPearl = ImageIO.read(url);
CaptainMorgan08a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 29
Ahh Thank you.
Chris1234554a at 2007-7-21 17:10:18 > top of Java-index,Java Essentials,Java Programming...
# 30

> 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);

DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 31

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?

Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 32

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?

Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 33

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.

DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 34

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

Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 35

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

DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 36

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

duckbilla at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 37
? I didn't understand that can you show me an example also can you help fix me errors before too?
Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 38

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);

}

DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 39
could you edit the above code to show me how that is implemented?
Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 40

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();

}

}

}

DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 41
Sorry I don't really understand that, why didn't my last code work anyway?
Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 42
What don't you understand about it?
DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 43
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?
Chris1234554a at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 44
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.
DrLaszloJamfa at 2007-7-21 17:10:22 > top of Java-index,Java Essentials,Java Programming...
# 45

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?

Chris1234554a at 2007-7-21 17:10:27 > top of Java-index,Java Essentials,Java Programming...
# 46
Is anyone going to help?
Chris1234554a at 2007-7-21 17:10:27 > top of Java-index,Java Essentials,Java Programming...
# 47
> 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 > top of Java-index,Java Essentials,Java Programming...
# 48
> 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 > top of Java-index,Java Essentials,Java Programming...
# 49
manifesting themselves?
Chris1234554a at 2007-7-21 17:10:27 > top of Java-index,Java Essentials,Java Programming...
# 50

> 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 > top of Java-index,Java Essentials,Java Programming...
# 51
I gave a screenshot
Chris1234554a at 2007-7-21 17:10:27 > top of Java-index,Java Essentials,Java Programming...
# 52
I need to go now anyway
Chris1234554a at 2007-7-21 17:10:27 > top of Java-index,Java Essentials,Java Programming...
# 53
> 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 > top of Java-index,Java Essentials,Java Programming...