2012-11-14 42 views
1

嗨!我有這個問題:我必須在Java中創建一個程序,設計一個人的身影,我必須畫它。我寫了設計人的代碼,但我不知道如何用顏色填充形狀。我知道我必須使用「java.awt.Color」,但我不知道如何。如何在java中設計顏色?

顏色必須是:圖像背景(黃色),頭部(藍色),手臂&雙腿(綠色),身體(紅色)。

這是到目前爲止我的代碼:

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

public class DrawPanelTest { 
    //creates a window to display the drawing 
    public static void main(String[] args) { 
     // create a new frame to hold the panel 
     JFrame application = new JFrame(); 
     Container pane=application.getContentPane(); 
     // create a panel that contains our drawing 
     DrawPanel panel = new DrawPanel(); 
     // set the frame to exit when it is closed 
     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // add the panel to the frame 
     pane.add(panel); 
     application.setContentPane(pane); 
     // set the size of the frame 
     application.setSize(550, 450); 
     // make the frame visible 
     application.setVisible(true); 
    } 
} 

這裏就是圖中得出:

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JPanel; 

public class DrawPanel extends JPanel { 

    public void paintComponent(Graphics g) { 
     //draw the human 
     g.drawOval(300, 100, 100, 100); 
     g.drawRect(300, 200, 100, 100); 
     g.drawRect(400,200, 100, 10); 
     g.drawRect(200,200, 100, 10); 
     g.drawRect(300,300, 10, 100); 
     g.drawRect(390,300, 10, 100); 
    } 
} 
+0

謝謝我不知道如何使它看起來像那樣。 – MKB

+2

沒問題,只需點擊「{}」按鈕將文本選擇格式設置爲代碼 –

回答

5

使用g.fillOval()到位g.drawOval()的

設置顏色由g.setColor()

關於背景顏色,請單擊o上述N一個鏈接,搜索術語「背景」和吊杆:Graphics.clearRect()

文檔說:

通過用當前繪圖表面的背景色進行填充來清除指定的矩形。

+0

感謝它的工作,但我仍然不知道如何繪製背景。您能給我一些幫助嗎? N – MKB

+0

你必須學會​​幫助你自己... – Aubin

+0

我的評論是在你編輯你的之前:P但是,我發現它 – MKB