how to draw simple eyes in java
How can I draw simplest eyes in Java's swing ? I would like to get something like that :
http://img710.imageshack.us/img710/70/eyesp.jpg
Use Graphics class methods:
- fillRect
- fillOval
And similar methods to accomplish what you are trying to on a JPanel.
Example:
public class Eyes extends JPanel
{
// override paint
@Override
protected void paintComponent(Graphics g)
{
super(g);
// use fillRect, fillOval and color methods
// on "g" to draw what you want
}
}
Then, of course, you will place Eyes object inside a JInternalFrame, other JPanel or container as you need.
To draw a filled circle with a outline in a different color, you can use drawOval
in addition to fillOval
(don't forget to change the color on the Graphics
context before drawing the outline).
You should also investigate the Grahpics2D
class, which has a lot more functionality than the regular Graphics
object. (You can simply cast a Graphics
instance as a Graphics2D
).
In particular, to make the circles look "nice", you may want to set the anti-aliasing rendering hint. You can do this as follows:
Graphics2D g2d = (Graphics2D)g;
// Turn anti-aliasing on.
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Draw everything you want...
// Turn anti-aliasing off again.
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
精彩评论