📄 canvaselement.java
字号:
protected void paintElement(Graphics g) {};
public void repaint()
{
parent.repaint(px,py, rwidth, rheight);
}
public void serviceRepaints()
{
parent.serviceRepaints();
}
public void keyPressed(int keyCode)
{
wantKeyRepeat = false;
wantRepaint = false;
keyPressedDeep(keyCode);
if (wantKeyRepeat)
{
keytimer = parent.newFixedRateTimer(keytimer, TIMER_KEYREPEAT,
REPEAT_DELAY, REPEAT_RATE, this);
last_rep_key = keyCode;
keyrepeat = 1;
}
}
public void keyPressedDeep(int keyCode)
{
keyPressedElement(keyCode);
if (wantRepaint)
{
repaint();
serviceRepaints();
}
}
protected void keyPressedElement(int keyCode)
{
}
public void keyReleased(int keyCode)
{
keytimer = parent.killTimer(keytimer);
keyrepeat = 0;
}
protected void wantKeyRepeat()
{
wantKeyRepeat = true;
}
protected void wantRepaint()
{
wantRepaint = true;
clearAutoScrollCounter();
}
public boolean isFocused()
{
return parent.isFocused(this);
}
public void setVisible(boolean vvis)
{
visible = vvis;
}
public boolean isVisible()
{
return visible;
}
public int getAccelerator()
{
return accelKey;
}
public void setAccelerator(int keyCode)
{
accelKey = keyCode;
}
public int getContentWidth()
{
int tmp = getContentWidthImpl();
if (tmp==0) tmp = minwidth;
if (drawBorder()) tmp += 4;
return tmp;
}
public int getContentHeight()
{
int tmp = getContentHeightImpl();
if (tmp==0) tmp = minheight;
if (drawBorder()) tmp += 4;
return tmp;
}
protected int getContentWidthImpl()
{
return 0;
}
protected int getContentHeightImpl()
{
return 0;
}
public void layout(int anchor)
{
layanchor = anchor;
int maxw = getContentWidth();
int maxh = getContentHeight();
int mx = 0;
int my = 0;
if ((anchor&Graphics.HCENTER)!=0)
{
mx = (parent.getWidth()-maxw) >> 1;
}
else if ((anchor&Graphics.RIGHT)!=0)
{
mx = parent.getWidth()-maxw;
}
if ((anchor&Graphics.VCENTER)!=0)
{
my = (parent.getHeight()-maxh) >> 1;
}
else if ((anchor&Graphics.BOTTOM)!=0)
{
my = parent.getHeight() - maxh;
}
setPositionSize(mx, my, maxw, maxh);
layouted = true;
}
public void sizeChanged(int w, int h)
{
if (layouted)
{
layout(layanchor);
}
}
public void setPositionSize(int mx, int my, int mw, int mh)
{
px = mx;
py = my;
rwidth = mw;
rheight = mh;
calcSpaces();
OnSizeChanged();
}
public void onTimer(int tag, long delta)
{
if (delta < 300 && tag == TIMER_AUTO_SCROLL && autoscroll)
{
if (keyrepeat==0) // don't scroll on keyrepeat
{
onAutoScroll();
}
}
if (tag == TIMER_KEYREPEAT)
{
keyPressedDeep(last_rep_key);
// flashing backlight takes time, do it only every 4th time
if ((keyrepeat&3)==0) parent.manageBacklight();
keyrepeat++;
}
}
public void setAutoScroll(boolean flag)
{
autoscroll = flag;
if (autoscroll)
{
scrollertimer = parent.newFixedRateTimer(scrollertimer, TIMER_AUTO_SCROLL,
SCROLL_INTERVAL, SCROLL_INTERVAL,
this);
}
}
public boolean getAutoScroll()
{
return autoscroll;
}
protected void onAutoScroll()
{
autoscroller++;
repaint();
}
public int getAutoScrollCounter()
{
return autoscroller;
}
public void clearAutoScrollCounter()
{
autoscroller = 0;
}
public Font getFont()
{
return selectedFont;
}
public static void paintStringASC(CanvasElement ce, Graphics g, String text, int x,int y,int anc,int dx, int asc)
{
if (text.length()==0) return;
Font fnt = ce.getFont();
int tw = fnt.stringWidth(text);
int dw = ce.getWidth() - dx;
int STEP = 4;
if (asc<=0 || tw<dw)
{
/*int ofs = 0;
int len = text.length();
while(tw>=dw)
{
len--;
tw = fnt.substringWidth(text, ofs, len);
}
g.drawSubstring(text, ofs, len, x, y, anc);*/
g.drawString(text, x, y, anc);
}
else
{
// asc is always > 0 here
int idx = STEP*(asc-1);
if (idx<0) idx = 0;
if (idx>=text.length()) idx = text.length()-1;
String stext = text.substring(idx);
tw = fnt.stringWidth(stext);
if (tw<dw)
{
ce.clearAutoScrollCounter();
asc = 0;
}
idx = STEP*asc;
if (idx<0) idx = 0;
if (idx>=text.length()) idx = text.length()-1;
stext = text.substring(idx);
g.drawString(stext, x, y, anc);
/* this code takes too long time to complete
int ofs = 0;
int len = stext.length();
while(true)
{
tw = fnt.substringWidth(text, ofs, len);
if (tw<dw) break;
len--;
}
g.drawSubstring(stext, ofs, len, x, y, anc);*/
}
}
public void setMinimumSize(int wwidth, int hheight)
{
minwidth = wwidth;
minheight = hheight;
}
public int getMinimumWidth()
{
int tmp = minwidth;
if (tmp>0 && drawBorder()) tmp += 4;
return tmp;
}
public int getMinimumHeight()
{
int tmp = minheight;
if (tmp>0 && drawBorder()) tmp += 4;
return tmp;
}
public void OnSizeChanged()
{
}
public void showNotify()
{
}
public void hideNotify()
{
}
public void execMetaCommands(Graphics g, int figureid, int bx, int by, int bw, int bh)
{
if (figures!=null)
{
int pos = figidxs[figureid];
int posl = figidxs[figureid+1]-1;
while(pos<posl)
{
int sz = execMetaCommand(g, pos, bx, by, bw, bh);
pos += sz + 1;
}
}
}
public void execMetaCommands(Graphics g, int figureid)
{
execMetaCommands(g, figureid, 0, 0, getWidth(), getHeight());
}
private int execMetaCommand(Graphics g, int pos, int bx, int by, int width, int height)
{
int cmd = figures[pos];
int lsz = 0;
if (cmd == COMMAND_FILL_RECT)
{
lsz = 4;
g.fillRect(bx+width*figures[pos+1]/100,
by+height*figures[pos+2]/100,
(width*figures[pos+3]/100)+1,
(height*figures[pos+4]/100)+1);
}
else if (cmd == COMMAND_FILL_RECT_BOUND)
{
lsz = 4;
int vx1 = MathPosRound(width*figures[pos+1]);
int vx2 = MathPosRound(width*figures[pos+3]);
int vy1 = MathPosRound(height*figures[pos+2]);
int vy2 = MathPosRound(height*figures[pos+4]);
g.fillRect(bx + vx1,
by + vy1,
vx2 - vx1,
vy2 - vy1 );
}
else if (cmd == COMMAND_FILL_TRI)
{
lsz = 6;
g.fillTriangle(bx+width*figures[pos+1]/100,
by+height*figures[pos+2]/100,
bx+width*figures[pos+3]/100,
by+height*figures[pos+4]/100,
bx+width*figures[pos+5]/100,
by+height*figures[pos+6]/100);
}
else if (cmd == COMMAND_FILL_ARC)
{
lsz = 6;
int vx1 = MathPosRound(width*figures[pos+1]);
int vx2 = MathPosRound(width*figures[pos+3]);
int vy1 = MathPosRound(height*figures[pos+2]);
int vy2 = MathPosRound(height*figures[pos+4]);
g.fillArc(bx + vx1,
by + vy1,
vx2 - vx1,
vy2 - vy1,
36*figures[pos+5]/10,
36*figures[pos+6]/10);
}
else if (cmd == COMMAND_COLOR)
{
lsz = 1;
int lcolor = figures[pos+1];
if (lcolor==COLOR_FG) lcolor = getForegroundColor();
if (lcolor==COLOR_BG) lcolor = getBackgroundColor();
g.setColor(lcolor);
}
else if (cmd == COMMAND_COLOR_RAMP)
{
lsz = 1;
int lramp = figures[pos+1];
g.setColor(getRampColor(lramp));
}
else
{
System.err.println("Invalid command"+cmd);
}
return lsz;
}
private int MathPosRound(int dint)
{
if ((dint % 100) >= 50) dint+=100;
dint = dint / 100;
return dint;
}
public HostCanvas getParent()
{
return parent;
}
public void setBackgroundColor(int bgc)
{
BACKGROUND_COLOR = bgc;
decomponentColors();
}
public void setForegroundColor(int fgc)
{
FOREGROUND_COLOR = fgc;
decomponentColors();
}
public void setMiddleRampColor(int mgc)
{
MIDDLERAMP_COLOR = mgc;
decomponentColors();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -