Re: newbie:this program gives error on compiling
Here is how to make it easier for others to help:
Display the listing like this with the error messages embedded:
public class OvalButton extends JButton
{
public OvalButton(String text)
{
super(text);
setOpaque(false);
}
public Shape getShape()
{
return new Ellipse2D.Float(0,0,getWidth()/2,getHeight());
-----------------------------------------------------------------------------------
ERROR
OvalButton.java:16: package Ellipse2D does not exist
return new Ellipse2D.Float(0,0,getWidth()/2,getHeight());
^
-----------------------------------------------------------------------------------
COMMENTARY
For some reason Javac thinks Ellipse is a package that Float lives.
Packages always start with a lower case letter so a red flag goes up.
Float is an inner class of Ellipse2D. It has a constructor
Ellipse2D.Float(float x, float y, float w, float h)
I like to import classes explicitly, rather than using * notation,
e.g.
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Float;
When I did that he error went away.
You could also do just:
import java.awt.geom.Ellipse2D;
or
import java.awt.geom.*;
Since inner classes are imported automatically.
I like to use IntelliJ or other IDE to tidy the imports. It does a
much better job than you can manually.
The easy way to solve this is to look the error message up at
http://mindprod.com/jgloss/compileerrormessages.html#PACKAGEDOESNTEXIST
-----------------------------------------------------------------------------------
}
public void paint(Graphics g)
{
Graphics2D g2=(Graphics2D) g;
g2.getClip(getShape());
-----------------------------------------------------------------------------------
ERROR
OvalButton.java:21: getClip() in java.awt.Graphics cannot be applied
to (java.awt.Shape)
g2.getClip(getShape());
^
-----------------------------------------------------------------------------------
COMMENTARY
I could not find a method Graphics2.getClip( Shape), just inherited
Graphics.getClip(). One of the first things to do is check the
Javadoc to make sure there is a method with the signature you think
SHOULD be there.
The easy way to solve this is to look the error message up at
http://mindprod.com/jgloss/compileerrormessages.html#CANTBEAPPLIED
-----------------------------------------------------------------------------------
super.paint(g2);
}
public boolean contains(int x,int y)
{
return getShape().contains(x,y);
}
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com