2015-10-27 78 views
0

如何獲得笑臉的嘴巴部分?我是否使用多邊形或橢圓形...橢圓形似乎沒有意義,但我不知道? 這裏是我的代碼:如何在Java中爲笑臉描繪嘴巴

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

    } 
} 
+1

有什麼錯['圖形#drawArc'(https://docs.oracle.com/javase/8/docs/ api/java/awt/Graphics.html#drawArc-int-int-int-int-int-)在你以前的問題中鏈接?我看到你也避免了通過[2D圖形](http://docs.oracle.com/javase/tutorial/2d/)線索 – MadProgrammer

+0

閱讀的建議不,沒有錯,沒有什麼是錯誤的我發現圖形# drawArc有幫助!我根本不知道如何製作一個半圓。對不起,你真的有幫助,我非常感謝鏈接! – stack101

回答

1

也許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(); 
      } 
     }); 
    } 
} 
+0

非常感謝! – stack101

+0

@ stack101,很高興幫助。不要忘記「接受」答案,以便人們知道問題已經解決。 – camickr