📄 imagedisplay1.java
字号:
import java.applet.*;
import java.awt.*;
import java.util.Vector;
public class ImageDisplay extends Canvas{
Image currentImage; /* the last current selected image */
String currentName; /* the name of the current selected symbol */
int grids, unit;
int imageNum, maxImageNum;
Image[] images;
String[] names;
boolean[] selected;
public Vector selectedList; /* use this Vector to get the selected images*/
MediaTracker tracker;
Color bg1 = Color.white, bg2 = Color.magenta;
int row = 0; /* current row number, for scrolling */
public ImageDisplay(int gridnum, int u, int n) {
grids = gridnum;
unit = u;
maxImageNum = n;
imageNum = 0;
images = new Image[ maxImageNum ];
names = new String[ maxImageNum ];
selected = new boolean[ maxImageNum ];
selectedList = new Vector(maxImageNum);
tracker = new MediaTracker(this);
setBackground(bg1);
}
public void addImage(String name, Image new_image) {
if (imageNum < maxImageNum) {
imageNum++;
images[ imageNum - 1 ] = new_image;
names[ imageNum - 1 ] = name;
selected[ imageNum - 1 ] = false;
/* wait till image is loaded */
tracker.addImage(images[ imageNum - 1 ], imageNum - 1);
try {tracker.waitForID(imageNum - 1); } catch (InterruptedException e) {} ;
if (tracker.isErrorID(imageNum - 1))
System.out.println("Error loading image " + (imageNum - 1));
}
else System.out.println("Error:ImageDisplay Overflow");
}
/* Warning:the stuff in draw() causes this.repaint() being called */
public void paint(Graphics g) {
draw();
}
public void scroll(int r) {
row = r;
repaint();
}
public void draw() {
Graphics g = getGraphics();
int count = row * grids;
int x = 0, y = 0; /* the grid x , y */
int imageWidth, imageHeight, spacex = 5, spacey = 5;
g.clearRect(0, 0, size().width, size().height);
while (count < imageNum) {
/* compute the space padding */
imageWidth = images[ count ].getWidth(this);
imageHeight = images[ count ].getHeight(this);
if (imageWidth < unit) spacex = (unit - imageWidth) / 2;
if (imageHeight < unit) spacey = (unit - imageHeight) / 2;
/* this causes the hightlight effect */
if (selected[ count ]) g.setXORMode(bg2);
else g.setPaintMode();
g.drawImage(images[ count ], x * unit + spacex, y * unit + spacey, this);
g.setFont(new Font("Courier", Font.BOLD, 12));
int i = names[ count ].indexOf("_");
/* draw in two lines if the string is two long */
if (i < 0) g.drawString(names[ count ], x * unit + spacex, y * unit + unit);
else {
g.drawString(names[count].substring(0, i + 1), x * unit + spacex, y * unit + unit - 10);
g.drawString(names[count].substring(i + 1), x * unit + spacex, y * unit + unit);
}
count++;
if ((++x) >= grids) { y++; x = 0; }
}
}
public boolean mouseUp(Event evt, int mousex, int mousey) {
int x = mousex / unit, y = mousey / unit;
int n = y * grids + x + row * grids;
/* if ctrl key is not pressed, unselect all */
if ((evt.modifiers & Event.CTRL_MASK) == 0) unselectAll();
if (x < grids && n < imageNum) {
selected[ n ] =! selected[ n ];
currentImage = images[ n ];
currentName = names[ n ];
if (selected[ n ]) selectedList.addElement(names[ n ]);
else selectedList.removeElement(names[ n ]);
draw();
}
draw();
return false;
}
public void unselectAll() {
for (int i = 0; i < imageNum; i++) selected[ i ] = false;
selectedList.removeAllElements();
currentImage = null;
currentName = null;
}
}
/*
class ReverseFilter extends RGBImageFilter {
public ReverseFilter() {
// this means the filter is position independent
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
return (rgb ^ 0xFFFFFF); // bitwise xor, has the effect of complement
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -