如何用Java绘制一个笑脸的嘴

时间:2015-10-27 00:16:23

标签: java graphics awt

如何获得笑脸的嘴部?我使用多边形还是椭圆形...椭圆形似乎没有意义,但我不知道? 这是我的代码:

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

    }
}

2 个答案:

答案 0 :(得分:3)

也许是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();
            }
        });
    }
}

答案 1 :(得分:0)

您将要画一个倒圆弧,否则您会皱着眉头,所以想添加

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

    `