加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ColorSquaresComponent.java 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2020-04-10 14:59 . 作业
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
/**
Draws colored squares.
*/
public class ColorSquaresComponent extends JComponent
{
private String colors;
private int wide;
private int high;
private int size;
/**
Constructs a component for a drawing made up of colored squares.
@param col the color codes of all squares in the drawing.
@param w the width of the drawing
@param h the height of the drawing
@param sideLength the side length of each square
*/
public ColorSquaresComponent(String col, int w, int h, int boxSize)
{
colors = col;
wide = w;
high = h;
size = boxSize;
}
private Color getSquareColor(char code)
{
if (code == 'R') return Color.RED;
if (code == 'G') return Color.GREEN;
if (code == 'B') return Color.BLUE;
if (code == 'C') return Color.CYAN;
if (code == 'M') return Color.MAGENTA;
if (code == 'Y') return Color.YELLOW;
if (code == 'K') return Color.BLACK;
return Color.WHITE;
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Rectangle box = new Rectangle (0,0, size, size);
// your work here - draw all of the boxes in the specified color
for(int i = 0;i < high;i++){
for(int j = 0;j < wide;j++){
char c=colors.charAt(i*20+j);
Color color = getSquareColor(c);
g2.setColor(color);
g2.fillRect(j*20,i*20,20,20);
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化