📄 graphicshapeicon.java
字号:
import javax.swing.*;
import java.awt.*;
public class GraphicShapeIcon implements Icon {
public GraphicShapeIcon(int shape) {
this.shape = shape;
}
public GraphicShapeIcon(int shape, int fullWidth, int fullHeight) {
this.shape = shape;
this.fullWidth = fullWidth;
this.fullHeight = fullHeight;
this.width = fullWidth - 2 * PAD;
this.height = fullHeight - 2 * PAD;
this.arcSize = fullWidth/2;
}
// Icon interface
public int getIconWidth() {
return fullWidth;
}
public int getIconHeight() {
return fullHeight;
}
public void paintIcon(Component comp, Graphics g, int x, int y) {
Color c = g.getColor();
g.translate(x + PAD, y + PAD);
g.setColor(Color.black);
switch(shape) {
case GraphicOps.DRAWN_LINE:
g.drawLine(0, 0, width - 1, height - 1);
break;
case GraphicOps.DRAWN_RECT:
g.drawRect(0, 0, width - 2, height - 2);
break;
case GraphicOps.DRAWN_ROUND_RECT:
g.drawRoundRect(0, 0, width - 2, height - 2, arcSize, arcSize);
break;
case GraphicOps.FILLED_RECT:
g.fillRect(0, 0, width - 1, height - 1);
break;
case GraphicOps.FILLED_ROUND_RECT:
g.fillRoundRect(0, 0, width - 1, height - 1, arcSize, arcSize);
break;
case GraphicOps.DRAWN_OVAL:
g.drawOval(0, height/4, width, height/2);
break;
case GraphicOps.FILLED_OVAL:
g.fillOval(0, height/4, width, height/2);
break;
}
g.setColor(c);
g.translate(-(x + PAD), -(y + PAD));
}
private int shape;
// Constants
public static final int FULLWIDTH = 32;
public static final int FULLHEIGHT = 32;
public static final int PAD = 2;
public static final int WIDTH = FULLWIDTH - 2 * PAD;
public static final int HEIGHT = FULLHEIGHT - 2 * PAD;
public static final int ARC_SIZE = FULLWIDTH/2;
// Sizes for this instance
private int fullWidth = FULLWIDTH;
private int fullHeight = FULLHEIGHT;
private int width = WIDTH;
private int height = HEIGHT;
private int arcSize = ARC_SIZE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -