Closed Thread Icon

Topic awaiting preservation: Java question (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13043" title="Pages that link to Topic awaiting preservation: Java question (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Java question <span class="small">(Page 1 of 1)</span>\

 
norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 01-25-2004 04:13

Yeah, I know- 'This is for server side stuff, not applications programing'. But I didn't know where else to post this one...

So I'm going thru sun's core Java fundamentals vol 1, trying to evolve from a mere scripter into a programmer, but it seems like my compiler hates me....and keeps spitting out this error message:

code:
/Users/normknowles/java/Image.java:9: incompatible types
found : java.awt.Image
required: Image
image=Toolkit.getDefaultToolkit().getImage("me_Icon.gif");




Here is a look at the offending code:

code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ImagePanel extends JPanel
{
public ImagePanel()
{
image=Toolkit.getDefaultToolkit().getImage("me_Icon.gif");
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image,0);
try{tracker.waitForID(0);}
catch(InterruptedException e){}
}

public void painComponent(Graphics g)
{
super.paintComponent(g);
Dimension d=getSize;

int clientWidth=d.width;
int clientHeight=d.Height;
int imageWidth=image.getWidth(this);
int imageHeight= image.getHeight(this);

g.drawImage(image,0,0,this);

for(int i=0; i*imageWidth<=clientWidth;i++)
{
for(int j=0;j*imageHeight<=clientHeight;j++)
{
if (i+j>0)
{
g.copyArea(0,0,imageWidth,imageHeight,i*imageWidth,j*imageHeight);
}

}

}


}
private Image image;
}



The image file is in the same directory as the program I'm trying to build, I've double checked the filename.

Where am I screwing up here?

/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

jdauie
Bipolar (III) Inmate

From: Missoula, MT
Insane since: Jan 2003

posted posted 01-25-2004 06:24

I get the program to compile with 3 changes, but I didn't test your code.

I changed lines 19, 22, and 43.

line 19: Dimension d=getSize();
line 22: int clientHeight=d.height;
line 43: private java.awt.Image image;

also, when I compiled your original code, i recieved the following error message, which might clarify things for you...

code:
F:\>javac Image.java
.\Image.java:9: duplicate class: ImagePanel
class ImagePanel extends JPanel
^
Image.java:47: cannot access Image
bad class file: .\Image.java
file does not contain class Image
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
private Image image;
^
2 errors

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 01-25-2004 06:39

thanks jdauie, I changed those lines and now it compiles!

Now I just have to figure out why the image doesn't show up on the frame when I invoke frame.show()....but those mistakes are probably in the code I didn't post.


/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 01-25-2004 13:23

Try your original code, but add the following:

code:
import java.awt.image.*;



I suspect that the class java.awt.Image relies on classes in the package java.awt.image, and if java.awt.image.* are not loaded, java.awt.Image won't work either. Don't confuse java.awt.image (the package) with java.awt.Image (the class). This is just a guess, but it's an educated one.

Cell 1250 :: alanmacdougall.com :: Illustrator tips

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 01-25-2004 20:35

I added import java.awt.image.* , but still no picture shows up in the frame ( which is a shame because it is a very good picture of me).

this is the rest of my code, I've gone over and over and over this and still can't see what is wrong:

code:
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("Image Test");
setSize(300,200);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

} );

Container contentPane=getContentPane();
contentPane.add(new ImagePanel());

}

}


public class Image
{
public static void main (String [] args)
{
JFrame frame=new ImageFrame();
frame.show();
}
}



/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

jdauie
Bipolar (III) Inmate

From: Missoula, MT
Insane since: Jan 2003

posted posted 01-26-2004 00:02

You mispelled a method:

public void paintComponent(Graphics g)

I checked it with an image and it displays it.
[edit] I assume that it displays correctly, perhaps not [/edit]

[This message has been edited by jdauie (edited 01-26-2004).]

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 01-26-2004 01:14

My thanks to both of you.

I made my JAR and now when I double-click it, up pops a picture of the best looking bald guy I know....This Java stuff might just turn out to be fun!

/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

« BackwardsOnwards »

Show Forum Drop Down Menu