stack101
stack101

Reputation: 13

How to draw a mouth for a Smiley Face in Java

How do I get the mouth part of the smiley face? Do I use a polygon or oval...oval doesn't seem to make sense but I don't know? here is my code:

import java.awt.Color;
import java.awt.Canvas;
import java.awt.Graphics;

public class HappyFace extends Canvas {

    public HappyFace() {

        setBackground(Color.BLACK);
    }

    public void paint(Graphics window) {

        window.setColor(Color.YELLOW);
        window.fillOval(250, 150, 350, 320);

        window.setColor(Color.MAGENTA);
        window.fillOval(300, 220, 90, 100);
        window.fillOval(450, 220, 90, 100);

        window.setColor(Color.WHITE);
        window.drawOval(380, 320, 90, 100);

        window.setColor(Color.GREEN);

    }
}

Upvotes: 1

Views: 20667

Answers (2)

Natthan
Natthan

Reputation: 13

you are going to want to put an inverted arc or you will have a frowning face so you want to add

  g.drawArc.invert(110, 55, 60, 60, 0, -180)

   ` import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class FaceComponent extends JPanel
    {
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            g.drawArc(100, 45, 80, 80, 0, 360);

            g.setColor( Color.blue );
            g.drawArc( 120, 70, 10, 10, 0, 360);
            g.drawArc( 150, 70, 10, 10, 0, 360);

            g.setColor( Color.magenta );
            g.drawLine ( 140, 85, 140, 100 );

            g.setColor( Color.red );
            g.drawArc ( 110, 55, 60, 60, 0, -180 );
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(250, 250);
        }

        private static void createAndShowGUI()
        {
            JComponent face = new FaceComponent();
            face.setForeground(Color.GREEN);
    //      face.setBackground(Color.YELLOW);

            JPanel contentPane = new JPanel( new BorderLayout() );
            contentPane.setBackground( Color.CYAN );
            contentPane.add( face );

            JFrame frame = new JFrame("SSCCE");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane( contentPane );
            frame.setLocationByPlatform( true );
            frame.pack();
            frame.setVisible( true );
        }

        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                    public void run()
                    {
                       createAndShowGUI();
                    }
               });
            }
        }

    `

Upvotes: 0

camickr
camickr

Reputation: 324157

Maybe a drawArc(...)

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class FaceComponent extends JPanel
{
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.drawArc(100, 45, 80, 80, 0, 360);

        g.setColor( Color.blue );
        g.drawArc( 120, 70, 10, 10, 0, 360);
        g.drawArc( 150, 70, 10, 10, 0, 360);

        g.setColor( Color.magenta );
        g.drawLine ( 140, 85, 140, 100 );

        g.setColor( Color.red );
        g.drawArc ( 110, 55, 60, 60, 0, -180 );
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(250, 250);
    }

    private static void createAndShowGUI()
    {
        JComponent face = new FaceComponent();
        face.setForeground(Color.GREEN);
//      face.setBackground(Color.YELLOW);

        JPanel contentPane = new JPanel( new BorderLayout() );
        contentPane.setBackground( Color.CYAN );
        contentPane.add( face );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane( contentPane );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

Upvotes: 2

Related Questions