やあ!私はこの問題を抱えています: 人間の姿をデザインするプログラムを Java で作成し、それをペイントする必要があります。人間をデザインするコードを書きましたが、形状を色で塗りつぶす方法がわかりません。「java.awt.Color」を使用する必要があることはわかっていますが、方法がわかりません。
色は、画像の背景(黄色)、頭(青)、腕と脚(緑)、体(赤)でなければなりません。
これまでの私のコードは次のとおりです。
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);
}
}