MKB*_*MKB 1 java colors draw background-color
嗨!我有这个问题:我必须在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 );
}
}
Run Code Online (Sandbox Code Playgroud)
这是绘制图形的地方:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
使用g.fillOval()代替g.drawOval()
通过g.setColor()设置颜色
关于背景颜色,单击上面的一个链接,搜索术语"背景"和繁荣:Graphics.clearRect()
文件说:
通过使用当前绘图表面的背景颜色填充指定的矩形来清除它.
| 归档时间: |
|
| 查看次数: |
160 次 |
| 最近记录: |