// Processed by NMI's Java Code Viewer 4.8.2 © 1997-2000 B. Lemaire // Website: http://njcv.htmlplanet.com E-mail: info@njcv.htmlplanet.com // Copy registered to Evaluation Copy // Source File Name: ScrollingPanel.java import java.awt.*; public class ScrollingPanel extends Panel { Scrollbar vert; Panel panel1; Scrollbar horiz; Panel scrolledPanel; int offset; int fill; boolean usingHoriz; boolean usingVert; Rectangle bounds; public static final String NORTH_WEST[] = { "North", "West" }; public static final String NORTH_EAST[] = { "North", "East" }; public static final String SOUTH_WEST[] = { "South", "West" }; public static final String SOUTH_EAST[] = { "South", "East" }; public static final int CENTER = 0; public static final int FILL = 1; Dimension preferredSize; public ScrollingPanel(Panel panel, String as[], int i) { this(panel, Color.lightGray, as, i); } public ScrollingPanel(Panel panel, Color color, String as[], int i) { preferredSize = new Dimension(0, 0); scrolledPanel = panel; fill = i; setBackground(Color.lightGray); setLayout(new BorderLayout()); vert = new Scrollbar(1, 0, 0, 0, 10); add(as[1], vert); horiz = new Scrollbar(0, 0, 0, 0, 10); add(as[0], horiz); panel1 = new Panel(); panel1.setBackground(color); panel1.setLayout(null); add("Center", panel1); scrolledPanel.move(0, 0); panel1.add(scrolledPanel); useScrolls(800, 600); } public boolean handleEvent(Event event) { if(event.target == vert) { int i = panel1.size().height; int k = scrolledPanel.size().height; if(k > i && vert.getMaximum() != 0) offset = ((k - i) * vert.getValue()) / vert.getMaximum(); else offset = 0; scrolledPanel.move(scrolledPanel.bounds().x, -offset); return true; } if(event.target == horiz) { int j = panel1.size().width; int l = scrolledPanel.size().width; if(l > j && horiz.getMaximum() != 0) offset = ((l - j) * horiz.getValue()) / horiz.getMaximum(); else offset = 0; scrolledPanel.move(-offset, scrolledPanel.bounds().y); return true; } else { return false; } } public void bringDown() { if(usingVert && fill == 1) { vert.setValue(vert.getMaximum()); handleEvent(new Event(vert, 0, null)); } } private void useScrolls(int i, int j) { bounds = scrolledPanel.bounds(); if(i != 0) { int k = bounds.width / i + 1; horiz.setValues(0, 0, 0, k); } if(j != 0) { int l = bounds.height / j + 1; vert.setValues(0, 0, 0, l); } if(fill == 1) { if(i >= preferredSize.width) { horiz.hide(); usingHoriz = false; scrolledPanel.resize(i, preferredSize.height); } else { horiz.show(); horiz.setValue(0); usingHoriz = true; } if(j >= preferredSize.height) { vert.hide(); usingVert = false; scrolledPanel.resize(scrolledPanel.size().width, j); return; } else { vert.show(); vert.setValue(vert.getMaximum()); usingVert = true; return; } } if(fill == 0) { if(i >= bounds.width) { horiz.hide(); usingHoriz = false; } else { horiz.show(); horiz.setValue(0); usingHoriz = true; } if(j >= bounds.height) { vert.hide(); usingVert = false; return; } vert.show(); vert.setValue(0); usingVert = true; } } private void placeScrolledPanel(int i, int j) { if(fill == 1) { scrolledPanel.move(0, 0); boolean _tmp = !usingVert && !usingHoriz; if(!usingVert && usingHoriz) handleEvent(new Event(horiz, 0, null)); if(usingVert && !usingHoriz) handleEvent(new Event(vert, 0, null)); if(usingVert && usingHoriz) { handleEvent(new Event(horiz, 0, null)); handleEvent(new Event(vert, 0, null)); } } if(fill == 0) { if(!usingVert && !usingHoriz) scrolledPanel.move((i - bounds.width) / 2, (j - bounds.height) / 2); if(!usingVert && usingHoriz) scrolledPanel.move(0, (j - bounds.height - horiz.size().height) / 2); if(usingVert && !usingHoriz) scrolledPanel.move((i - bounds.width - vert.size().width) / 2, 0); if(usingVert && usingHoriz) scrolledPanel.move(0, 0); } } public void updatePanel() { useScrolls(size().width, size().height); layout(); placeScrolledPanel(size().width, size().height); } public synchronized void resize(int i, int j) { useScrolls(i, j); super.resize(i, j); layout(); placeScrolledPanel(i, j); } public synchronized void reshape(int i, int j, int k, int l) { useScrolls(k, l); super.reshape(i, j, k, l); layout(); placeScrolledPanel(k, l); } }