📄 paletteitem.java
字号:
package joeh.asciidraw;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.io.*;
//Something that you can click on and select.
//Something that controls an AsciiDrawPanel (sets its color)
//(HINT) Note: I thought about this some more and there is a better way to implement this
//'control' another class (keep a common state) by using "interfaces".
//You can read about it in the Java in a Nutshell book.
public class paletteItem extends Panel {
boolean isSelected = false;
Color paletteColor;
String paletteChar;
int selectionBorder = 2;
Color selectionColor = Color.yellow;
AsciiDrawPanel adp;
public paletteItem(Color c, String s, AsciiDrawPanel whatToControl) {
paletteColor = c;
paletteChar = s;
adp = whatToControl;
}
public void deselect() {
isSelected = false;
this.repaint();
}
private void drawNormalBox(Graphics gg){
int w = this.size().width;
int h = this.size().height;
gg.setColor(Color.black);
gg.fillRect(0,0,w,selectionBorder); //top
gg.fillRect(0,h-selectionBorder,w,selectionBorder); //bottom
gg.fillRect(0,0,selectionBorder,h); //left
gg.fillRect(w-selectionBorder,0,selectionBorder,h); //right
}
private void drawSelectionBox(Graphics gg){
int w = this.size().width;
int h = this.size().height;
gg.setColor(selectionColor);
gg.fillRect(0,0,w,selectionBorder); //top
gg.fillRect(0,h-selectionBorder,w,selectionBorder); //bottom
gg.fillRect(0,0,selectionBorder,h); //left
gg.fillRect(w-selectionBorder,0,selectionBorder,h); //right
}
public void select() {
isSelected = true;
adp.setColorAndChar(paletteColor, paletteChar); //set it in the draw window
adp.setColorPaletteSelection(this);
Graphics g = this.getGraphics();
drawSelectionBox(g);
}
public void paint(Graphics g) {
int w = this.size().width;
int h = this.size().height;
g.setColor(Color.white);
g.fillRect(0,0,w,h);
g.setColor(paletteColor);
g.fillRect(0,0,(w*2),(h*2));
if (isSelected) drawSelectionBox(g);
else drawNormalBox(g);
}
public Dimension minimumSize() {
//i must be big enough to see and click
int squareEdge = selectionBorder*8; //borders = one half total size...
return new Dimension(squareEdge, squareEdge);
}
public Dimension preferredSize() {
//I'de like to be this big
int w = selectionBorder*8; //borders = one half total size...
int h = selectionBorder*10;
return new Dimension(w, h);
}
public boolean handleEvent(Event evt)
{
switch(evt.id)
{
case Event.MOUSE_DOWN:
{
select();
adp.requestFocus(); //give the focus back to the panel so that
//it can be typed to without first having to click the mouse there.
return true;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -