neville
neville

Reputation: 35

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

Upvotes: 0

Views: 1405

Answers (2)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181410

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.

Upvotes: 3

Ash
Ash

Reputation: 9446

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

Upvotes: 1

Related Questions