简体   繁体   English

如何在 Java 中为笑脸画嘴巴

[英]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);

    }
}

Maybe a drawArc(...)也许是一个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();
            }
        });
    }
}

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

    `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM