📄 layermenu.java
字号:
//////////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 1996 L. Patocchi & W.Gander//// This program is free software; you can redistribute it and/or modify it// under the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 of the License, or (at your option)// any later version.//// This program is distributed in the hope that it will be useful, but WITHOUT// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for// more details.//// You should have received a copy of the GNU General Public License along with// this program; if not, write to the Free Software Foundation, Inc., 675 Mass// Ave, Cambridge, MA 02139, USA.//// Contacts:// // Project Supervisor// W.Hett hew@info.isbiel.ch// // Authors// W.Gander gandw@info.isbiel.ch// L.Patocchi patol@info.isbiel.ch//// Documentation can be found at://// http://www.isbiel.ch/Projects/janet/index.html////////////////////////////////////////////////////////////////////////////////////////// File : layerMenu.java//package jaNet.backprop.gui;import java.awt.*;import java.util.*;class layerMenu extends Window{ private Frame dearParentFrame; private Component dearParentComponent; public int myX, myY; public int width, height; public int horizontalInset; public int verticalInset; public int xPositionWithInc, yPositionWithInc; // components private String menuLabel; private int menuLabelWidth, menuLabelHeight; private Vector menuItems; private int currentSelection; private boolean iHadMove = false; public layerMenu(Frame frame, Component component, String label){ super(frame); dearParentFrame = frame; dearParentComponent = component; menuLabel = label; menuLabelWidth = getStringWidth(dearParentComponent.getGraphics(),menuLabel); menuLabelHeight = getStringHeight(dearParentComponent.getGraphics()); horizontalInset = 5; verticalInset = 2; width = menuLabelWidth + 2*horizontalInset +2; if(width < 75) width = 75; height = menuLabelHeight + 2*verticalInset +2; if(height < 20) height = 20; currentSelection = -1; menuItems = new Vector(); } public void show(int x, int y, String label){ Graphics g = dearParentFrame.getGraphics(); menuLabel = label; menuLabelWidth = getStringWidth(g,menuLabel); menuLabelHeight = getStringHeight(g); xPositionWithInc = x; yPositionWithInc = y; // recompute position myX = x; myY = y ; Component temp = dearParentComponent; while(temp != null){ myX += temp.location().x; myY += temp.location().y; temp = temp.getParent(); } myX -= dearParentFrame.insets().left; myY -= + menuLabelHeight + 2*verticalInset +2; move(myX, myY); // recompute size for(int i=0; i<menuItems.size(); i++) if(getItem(i).getLabelWidth(g)+ 2*horizontalInset +2 > width) width = getItem(i).getLabelWidth(g)+ 2*horizontalInset +2; height = menuLabelHeight + 2*verticalInset +2; if(height < 20) height = 20; for(int i=0; i<menuItems.size(); i++) height += getItem(i).getHeight(); resize(width, height); super.show(); } public void update(Graphics g){ paint(g); move(myX, myY); } public void paint(Graphics g){ int w = size().width; int h = size().height; Font tempFont = g.getFont(); move(myX, myY); int y=0; // create an image to avoid flicks Image image = createImage(w, h); Graphics g2 = image.getGraphics(); g2.setColor(Color.lightGray); g2.fill3DRect(0,0, width , height ,true); g2.setColor(Color.black); g2.setFont(new Font("Helvetica",Font.BOLD , 12)); g2.drawString(menuLabel, (width-menuLabelWidth)/2, menuLabelHeight ); g2.setFont(tempFont); y = menuLabelHeight+ 2*verticalInset +2; for (int i=0; i<menuItems.size(); i++){ y += getItem(i).drawMenuItem(g2,y, width); } g.drawImage(image, 0,0, this); } public boolean handleEvent(Event event) { // what a terrible hack for WindowShit if(event.id == Event.MOUSE_UP){ if(iHadMove) return dearParentComponent.handleEvent(event); return true; } if(event.id == Event.MOUSE_DRAG) iHadMove = true; // pass only UP or DRAG if(event.id == Event.MOUSE_UP || event.id == Event.MOUSE_DRAG) return dearParentComponent.handleEvent(event); return super.handleEvent(event); } public boolean handlePrivateEvent(Event event) { move(myX, myY); if (event.id == Event.MOUSE_DRAG) { int x = event.x - xPositionWithInc; int y = event.y - yPositionWithInc; //System.out.println(""+x+" , "+y); if (x<0 || x > width){ if (currentSelection != -1){ getItem(currentSelection).deselect(); currentSelection = -1; repaint(); } return super.handleEvent(event);; } for (int i=0; i<menuItems.size(); i++){ if (getItem(i).isSelectable(y)){ if (i != currentSelection){ if (currentSelection != -1) getItem(currentSelection).deselect(); currentSelection = i; getItem(currentSelection).select(); repaint(); } return super.handleEvent(event);; } } if (currentSelection != -1){ getItem(currentSelection).deselect(); currentSelection = -1; repaint(); } } return true; } public int countItems(){ return menuItems.size(); } public layerMenuItem getItem(int index){ return (layerMenuItem)menuItems.elementAt(index); } public int getSelection(){ return currentSelection; } public void add(layerMenuItem menuItem){ menuItems.addElement(menuItem); } public layerMenuItem add(String label){ layerMenuItem menuItem = new layerMenuItem(label); add(menuItem); return menuItem; } public void addSeparator(){ layerMenuItem menuItem = new layerMenuItem(); add(menuItem); } public int getStringWidth(Graphics g, String str){ FontMetrics fm = g.getFontMetrics(g.getFont()); return fm.stringWidth(str); } public int getStringHeight(Graphics g){ FontMetrics fm = g.getFontMetrics(g.getFont()); return fm.getHeight(); } public synchronized void remove(MenuComponent item){ int index = menuItems.indexOf(item); if (index >= 0) remove(index); } public synchronized void remove(int index){ layerMenuItem dummy = getItem(index); menuItems.removeElementAt(index); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -