如何在 Java 中为笑脸画嘴巴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33357857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:33:04 来源:igfitidea点击:
How to draw a mouth for a Smiley Face in Java
提问by stack101
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);
}
}
回答by camickr
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();
}
});
}
}
回答by Natthan
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();
}
});
}
}
`