📄 mylabel.java
字号:
/*
* MyLabel.java
*
* 僀儀儞僩傪庢摼偱偒傞儔儀儖
*
* @see http://java-house.etl.go.jp/ml/archive/j-h-b/003638.html#body
*
* Modified by Takahiro Katoji(mailto:katoco@mars.elcom.nitech.ac.jp)
*/
package dicomviewer;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class MyLabel extends Canvas {
public static final int LEFT = Label.LEFT;
public static final int CENTER = Label.CENTER;
public static final int RIGHT = Label.RIGHT;
private String label;
private int alignment;
private static final int hgap = 5;
private static final int vgap = 3;
// Viewer偺AppletContext傪奿擺偡傞
AppletContext appletContext;
// 暥帤偺怓(default: black)
Color color = Color.black;
public MyLabel() {
this("", LEFT);
}
public MyLabel(String label) {
this(label, LEFT);
}
public MyLabel(String label, int alignment) {
this.label = label;
this.alignment = alignment;
// 儅僂僗僀儀儞僩傪庢摼偡傞傛偆夵曄
this.addMouseListener(new MyMouseListener());
}
public void setText(String text) {
label = text;
repaint();
}
public String getText() {
return label;
}
public void setAlignment(int alignment) {
this.alignment = alignment;
repaint();
}
public int getAlignment() {
return alignment;
}
public Dimension preferredSize() {
Font f = getFont();
int height = getFont().getSize();
int width = getFontMetrics(getFont()).stringWidth(label);
return new Dimension(width + hgap * 2, height + vgap * 2);
}
public Dimension minimumSize() {
Font f = getFont();
int height = getFont().getSize();
int width = getFontMetrics(getFont()).stringWidth(label);
return new Dimension(width, height);
}
public void paint(Graphics g) {
Dimension area = getSize();
Font f = g.getFont();
int w = g.getFontMetrics().stringWidth(label);
int h = f.getSize();
int x;
switch (alignment) {
// case LEFT:
default:
x = area.width - w > hgap ? hgap : 0;
break;
case CENTER:
x = (area.width - w) / 2;
break;
case RIGHT:
x = area.width - w - (area.width - w > hgap ? hgap : 0);
break;
}
int y = (area.height + h) / 2 - g.getFontMetrics(f).getMaxDescent();
// 暥帤偺怓傪寛掕偡傞
g.setColor(color);
g.drawString(label, x, y);
}
// AppletContext傪庴偗庢傞
public void setAppletContext(AppletContext appletContext) {
this.appletContext = appletContext;
}
// 儅僂僗憖嶌儕僗僫乕
class MyMouseListener extends MouseAdapter {
// 儅僂僗偑Canvas撪偵擖偭偨偲偒
public void mouseEntered(MouseEvent e) {
// 僇乕僜儖偺宍傪庤偵偡傞
setCursor(new Cursor(Cursor.HAND_CURSOR));
// 暥帤偺怓傪惵偔偡傞
color = Color.blue;
repaint();
}
// 儅僂僗偑Canvas奜偵偱偨偲偒
public void mouseExited(MouseEvent e) {
// 僇乕僜儖偺宍傪尦偵栠偡
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
// 暥帤偺怓傪崟偔偡傞
color = Color.black;
repaint();
}
// 儅僂僗傪墴偟偨偲偒偺張棟
public void mousePressed(MouseEvent e) {
try {
URL url = new URL("http://mars.elcom.nitech.ac.jp/dicom/index-e.html");
// DICOM Viewer (Java Applet)丂偺儁乕僕傪怴偟偄僂傿儞僪僂偵昞帵偡傞
appletContext.showDocument(url, "_blank");
}
catch(Exception exception){
System.out.println("Exception: " + exception.getMessage() );
}
}
// 儅僂僗傪棧偟偨偲偒偺張棟
public void mouseReleased(MouseEvent e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -