📄 swingmapview.java
字号:
/** * This class is used by SwingMapTest to load and update the map **/import java.awt.*;import javax.swing.*;public class SwingMapView extends JComponent implements Scrollable { private Mappy map; private String mapname; private int xoff, yoff; public SwingMapView() { mapname = "TEST.FMP"; map = null; //can't load map yet as createImage won't work until after setup xoff = 0; yoff = 0; setAutoscrolls(true); //enable synthetic drag events } public void setPreferredHeight(int ph) { setPreferredSize(new Dimension(512, ph)); } public void setPreferredWidth(int pw) { setPreferredSize(new Dimension(pw, 420)); } public void paintComponent(Graphics g) { Rectangle drawHere = g.getClipBounds(); if (map == null && mapname != null) { map = new Mappy (); map.loadMap (mapname, this); setPreferredSize(new Dimension (map.getMapWidth(), map.getMapHeight())); xoff = 0; yoff = 0; revalidate(); return; } if (map != null) { map.setScreenDimensions(drawHere.x, drawHere.y, drawHere.width, drawHere.height); map.setX (xoff+drawHere.x); map.setY (yoff+drawHere.y); map.drawBackground (g, false); map.drawForeground (g, 1); map.drawForeground (g, 2); map.drawForeground (g, 3); } } public Dimension getPreferredSize() { if (map != null) { return new Dimension (map.getMapWidth(), map.getMapHeight()); } return new Dimension(512, 420); //super.getPreferredSize(); } public Dimension getPreferredScrollableViewportSize() { return new Dimension(512, 420); } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { //Get the current position. //System.out.println("SUI: o="+orientation+" d="+direction+"\n"); int currentPosition = 0; if (orientation == SwingConstants.HORIZONTAL) { currentPosition = visibleRect.x; if (direction < 0) { currentPosition = visibleRect.x%map.getBlockWidth(); if (currentPosition == 0) return map.getBlockWidth(); return currentPosition; } if (direction > 0) return map.getBlockWidth()-(currentPosition%map.getBlockWidth()); } else { currentPosition = visibleRect.y; if (direction < 0) { currentPosition = visibleRect.y%map.getBlockHeight(); if (currentPosition == 0) return map.getBlockHeight(); return currentPosition; } if (direction > 0) return map.getBlockHeight()-(currentPosition%map.getBlockHeight()); } return currentPosition; } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { if (orientation == SwingConstants.HORIZONTAL) { return visibleRect.width - 16; } else { return visibleRect.height - 16; } } public boolean getScrollableTracksViewportWidth() { return false; } public boolean getScrollableTracksViewportHeight() { return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -