1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import javax.swing.border.*;
5
6 public class ScrollMap extends JApplet {
7
8 private JLabel lblIndianaMap = new JLabel(
9 new ImageIcon(getClass().getResource("image/indianaMap.gif")));
10 private JLabel lblOhioMap = new JLabel(
11 new ImageIcon(getClass().getResource("/image/ohioMap.gif")));
12
13
14 private JScrollPane jspMap = new JScrollPane(lblIndianaMap);
15
16 public ScrollMap() {
17
18 JComboBox jcboMap = new JComboBox(new String[]{"Indiana",
19 "Ohio"});
20
21
22 JPanel p = new JPanel();
23 p.setLayout(new BorderLayout());
24 p.add(jcboMap);
25 p.setBorder(new TitledBorder("Select a map to display"));
26
27
28 jspMap.setColumnHeaderView(new JLabel(new ImageIcon(getClass().
29 getResource("/image/horizontalRuler.gif"))));
30 jspMap.setRowHeaderView(new JLabel(new ImageIcon(getClass().
31 getResource("/image/verticalRuler.gif"))));
32 jspMap.setCorner(JScrollPane.UPPER_LEFT_CORNER,
33 new CornerPanel(JScrollPane.UPPER_LEFT_CORNER));
34 jspMap.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER,
35 new CornerPanel(JScrollPane.UPPER_RIGHT_CORNER));
36 jspMap.setCorner(JScrollPane.LOWER_RIGHT_CORNER,
37 new CornerPanel(JScrollPane.LOWER_RIGHT_CORNER));
38 jspMap.setCorner(JScrollPane.LOWER_LEFT_CORNER,
39 new CornerPanel(JScrollPane.LOWER_LEFT_CORNER));
40
41
42 add(jspMap, BorderLayout.CENTER);
43 add(p, BorderLayout.NORTH);
44
45
46 jcboMap.addItemListener(new ItemListener() {
47
48 public void itemStateChanged(ItemEvent e) {
49 String selectedItem = (String)e.getItem();
50 if (selectedItem.equals("Indiana")) {
51
52 jspMap.setViewportView(lblIndianaMap);
53 }
54 else if (selectedItem.equals("Ohio")) {
55
56 jspMap.setViewportView(lblOhioMap);
57 }
58
59
60 jspMap.revalidate();
61 }
62 });
63 }
64
65
66 class CornerPanel extends JPanel {
67
68 private String location;
69
70 public CornerPanel(String location) {
71 this.location = location;
72 }
73
74 @Override
75 protected void paintComponent(Graphics g) {
76 super.paintComponents(g);
77
78 if (location == "UPPER_LEFT_CORNER")
79 g.drawLine(0, getHeight(), getWidth(), 0);
80 else if (location == "UPPER_RIGHT_CORNER")
81 g.drawLine(0, 0, getWidth(), getHeight());
82 else if (location == "LOWER_RIGHT_CORNER")
83 g.drawLine(0, getHeight(), getWidth(), 0);
84 else if (location == "LOWER_LEFT_CORNER")
85 g.drawLine(0, 0, getWidth(), getHeight());
86 }
87 }
88
89 public static void main(String[] args) {
90 ScrollMap applet = new ScrollMap();
91 JFrame frame = new JFrame();
92 //EXIT_ON_CLOSE == 3
93 frame.setDefaultCloseOperation(3);
94 frame.setTitle("ScrollMap");
95 frame.getContentPane().add(applet, BorderLayout.CENTER);
96 applet.init();
97 applet.start();
98 frame.setSize(400,320);
99 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
100 frame.setLocation((d.width - frame.getSize().width) / 2,
101 (d.height - frame.getSize().height) / 2);
102 frame.setVisible(true);
103 }
104 }