代码拉取完成,页面将自动刷新
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);
int sum=0;
// your work here - draw all of the boxes in the specified color
for (int i = 1;i <= 20;i++)
{
for (int j = 0;j < wide; j++){
char c = colors.charAt(sum);
sum++;
Color color =getSquareColor(c);
g2.setColor(color);
g2.fillRect(j*size,(i-1)*size,size,size);
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。