📄 bufferedlayer.java
字号:
public void removeLayer(Layer layer) { mapBean.remove(layer); resetPalette(); } /** * Return if there is at least one layer assigned to the group. */ public boolean hasLayers() { return (mapBean.getComponentCount() > 0); } /** * Get the layers assigned to the internal MapBean. * * @return a Component[]. */ public Component[] getLayers() { return mapBean.getComponents(); } public void firePaletteEvent(ComponentEvent event) { super.firePaletteEvent(event); hasActiveLayers = (event.getID() == ComponentEvent.COMPONENT_SHOWN); } /** * You can change what kind of MapBean is used to hold onto the * layers. This method just sets the new MapBean into the layer, * as is. If there was a previous MapBean with layers, they're * gone and replaces with whatever is attached to the new MapBean. * * @param mb new MapBean */ public void setMapBean(MapBean mb) { if (mapBean != null) { remove(mapBean); } mapBean = mb; add(mapBean, BorderLayout.CENTER); } /** * Get the current MapBean used in the BufferedLayer. * * @return MapBean */ public MapBean getMapBean() { return mapBean; } /** * Set the background color of the group. Actually sets the * background color of the projection used by the internal * MapBean, and which then forces a repaint() on it. * * @param color java.awt.Color. */ public void setBackground(Color color) { setBckgrnd(color); } /** * Set the background paint of the group. Actually sets the * background paint of the projection used by the internal * MapBean. * * @param paint java.awt.Paint */ public void setBckgrnd(Paint paint) { mapBean.setBckgrnd(paint); if (paint instanceof Color) { setHasTransparentBackground(((Color) paint).getAlpha() < 255); } else { // then we don't know, assume it is. setHasTransparentBackground(true); } } /** * Get the background color of the image. Actually returns the * background color of the projection of the internal MapBean. * * @return color java.awt.Color */ public Color getBackground() { return mapBean.getBackground(); } /** * Get the background Paint object used for the internal MapBean. * * @return java.awt.Paint */ public Paint getBckgrnd(Paint paint) { return mapBean.getBckgrnd(); } /** * Part of the Layer/ProjectionListener thing. The internal * MapBean's projection gets set here, which forces the group * layers to receive it as well. */ public void projectionChanged(com.bbn.openmap.event.ProjectionEvent pe) { Projection proj = setProjection(pe); if (proj != null && mapBean instanceof BLMapBean && hasTransparentBackground) { ((BLMapBean) mapBean).wipeImage(); } mapBean.setProjection(proj); } /** * The GUI panel. */ JPanel panel = null; /** * Should be called if layers are added or removed from the * buffer. */ public void resetPalette() { panel = null; super.resetPalette(); } /** * Get the GUI (palettes) for the layers. The BufferedLayer * actually creates a JTabbedPane holding the palettes for all of * its layers, and also has a pane for itself that provides * visibility control for the group layers. */ public Component getGUI() { if (panel == null) { Component[] layerComps = getLayers(); panel = new JPanel(); JTabbedPane tabs = new JTabbedPane(); JPanel bfPanel = new JPanel(); bfPanel.setLayout(new BoxLayout(bfPanel, BoxLayout.Y_AXIS)); bfPanel.setAlignmentX(Component.CENTER_ALIGNMENT); // LEFT bfPanel.setAlignmentY(Component.CENTER_ALIGNMENT); // BOTTOM tabs.addTab("Layer Visibility", bfPanel); for (int i = 0; i < layerComps.length; i++) { Layer layer = (Layer) layerComps[i]; Component layerGUI = layer.getGUI(); if (layerGUI != null) { tabs.addTab(layer.getName(), layerGUI); } VisHelper layerVisibility = new VisHelper(layer); bfPanel.add(layerVisibility); } panel.add(tabs); } return panel; } public void paint(Graphics g) { if (hasLayers()) { super.paint(g); } } /** * Class that helps track turning on/off layers in the buffered * layer. */ protected class VisHelper extends JCheckBox implements ActionListener { Layer layer; public VisHelper(Layer l) { super(l.getName(), l.isVisible()); super.addActionListener(this); layer = l; } public void actionPerformed(ActionEvent ae) { layer.setVisible(((JCheckBox) ae.getSource()).isSelected()); if (Debug.debugging("bufferedlayer")) { Debug.output("Turning " + layer.getName() + (((JCheckBox) ae.getSource()).isSelected() ? " on" : " off")); } if (mapBean instanceof BLMapBean && hasTransparentBackground) { ((BLMapBean) mapBean).wipeImage(); } layer.repaint(); } } /** * Part of the ProjectionPainter interface. The group layers are * given the projection and the graphics to paint into. */ public void renderDataForProjection(Projection proj, Graphics g) { Component[] layersComps = mapBean.getComponents(); for (int i = layersComps.length - 1; i >= 0; i--) { Layer layer = (Layer) layersComps[i]; layer.renderDataForProjection(proj, g); } } /** * PropertyChangeListener method, to listen for the source map's * background changes. Act on if necessary. */ public void propertyChange(PropertyChangeEvent pce) { if (pce.getPropertyName() == MapBean.BackgroundProperty) { mapBean.setBckgrnd((Paint) pce.getNewValue()); } } /** * An simple extension of the BufferedMapBean that calls a layer, * presumably its parent, to call repaint(). This is necessary in * order to make sure Swing calls paint properly. Only repaint() * is overridden in this class over a standard BufferedMapBean. */ public class BLMapBean extends BufferedMapBean { /** The layer to call back. */ Layer layer; /** * @param parent the parent layer of this MapBean. */ public BLMapBean(Layer parent) { super(); background = OMColor.clear; layer = parent; } /** * Set the buffer dirty, and call repaint on the layer. */ public void repaint() { setBufferDirty(true); if (Debug.debugging("bufferedlayer")) { Debug.output("BLMapBean.repaint() has active layers = " + hasActiveLayers); } if (hasActiveLayers && hasTransparentBackground) { wipeImage(); } if (layer != null) { layer.repaint(); } } /** * We need this because if the background to the BufferedLayer * is clear, we need to clear out anything that was previously * there. This seems to be the only way to do it. */ public void wipeImage() { setBufferDirty(true); // Need to do some optimization for this to figure out // which is really faster, recreating a new image, or // cycling though the pixels. Plus, the image should be // reset if the background is slighly transparent if any // layer can change between overall cleansings. if (this.getBackground() == OMColor.clear) { drawingBuffer = createImage(this.getWidth(), this.getHeight()); } } /** * We need the buffer to be able to be transparent. */ public Image createImage(int width, int height) { if (Debug.debugging("bufferedlayer")) { Debug.output("BLMapBean.createImage()"); } if (width <= 0) width = 1; if (height <= 0) height = 1; return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -