嗨!我有這個問題:我必須在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);
}
}
謝謝我不知道如何使它看起來像那樣。 – MKB
沒問題,只需點擊「{}」按鈕將文本選擇格式設置爲代碼 –