-
paintIcon
Hey there, I have this code below.
I seem to keep getting this error.
Code:
paintIcon(java.awt.Component, java.awt.Graphics,int,int) in javax.swing.ImageIcon cannot be applied to (Smurf, java.awt.Graphics,int,int)
Code:
public class Smurf extends Cartoons
{ Image backImage;
public Goldfish(ImageIcon image)
{
super(image);
this.backImage = backImage;
try
{
this.backImage = ImageIO.read(new URL(""));
URL url = new URL("http://bluebuddies.com/gallery/Smurf_GIF_Animations/gif/Sleeping_Papa_Smurf.gif");
image = new ImageIcon(url);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public void paintComponent(Graphics g)
{
g.drawImage(backImage,0,0,null);
image.paintIcon(this, g, 100, 100);
}
my main aim is to be able to make object of this class in another class(main method) so that it will create a new image of this.
I already have a background image hence that's why I keep the background image blank.
however I'm not sure this is the right way to do it.
-
The error message is self explanatory. The first parameter to the paintIcon method has to be a Component. Evidently your Smurf which extends Cartoons doesn't inherit from Component.
db
-
I solved the problem. Thanks.
I have one more question.
since my main aim is to be able to make object of this class in another class(main method) so that it will create a new image of this.
I already have a background image hence that's why I keep the background image blank.
is the right way to do it?
I tried a few ways. one of the is
Code:
public class testing extends JPanel
{
public Image backImage;
public ImageIcon pic, pic1;
public testing()
{
Cartoons photo1 = new Smurf();
Cartoons photo2 = new Smurf();
Cartoons photo3 = new Smurff();
try
{
this.backImage = ImageIO.read(new URL("http://......jpg"));
URL url = new URL("http://......gif");
pic = new ImageIcon(url);
URL url1 = new URL("http://.....gif");
pic1 = new ImageIcon(url1);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public static void main(String[] args)throws MalformedURLException
{
JFrame frame = new JFrame("");
frame.setContentPane(new testing());
frame.getContentPane().setLayout(null);
frame.setSize(400,400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
protected void paintComponent(Graphics g)
{
g.drawImage(backImage,0,0,null);
//super.paintComponent(g);
pic.paintIcon(this, g, 100, 100);
pic2.paintIcon(this, g, 300, 300);
}
it compiled successfully but when I tried to run it I get this error
Code:
at testing.main(testing.java:70)
Caused by: java.net.MalformedURLException: no protocol:
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at Smurf.<init>(Smurf.java:25)
at testing.<init>(testing.java:24)
at testing.main(testing.java:70)
at __SHELL13.run(__SHELL13.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
but then again, im not sure whether this is the correct way of doing