1 36 37 40 41 import javax.swing.*; 42 import javax.swing.border.*; 43 44 import java.awt.*; 45 import java.awt.event.*; 46 import java.util.*; 47 48 49 55 56 public class DirectionPanel extends JPanel { 57 58 private ButtonGroup group; 59 60 public DirectionPanel(boolean enable, String selection, ActionListener l) { 61 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 62 setAlignmentY(TOP_ALIGNMENT); 63 setAlignmentX(LEFT_ALIGNMENT); 64 65 Box firstThree = Box.createHorizontalBox(); 66 Box secondThree = Box.createHorizontalBox(); 67 Box thirdThree = Box.createHorizontalBox(); 68 69 if(!enable) { 70 selection = "None"; 71 } 72 73 group = new ButtonGroup(); 74 DirectionButton b; 75 b = (DirectionButton) firstThree.add(new DirectionButton( tl_dot, tldn_dot, "NW", "Sets the orientation to the North-West", l, group, selection.equals("NW"))); 76 b.setEnabled(enable); 77 b = (DirectionButton) firstThree.add(new DirectionButton( tm_dot, tmdn_dot, "N", "Sets the orientation to the North", l, group, selection.equals("N"))); 78 b.setEnabled(enable); 79 b = (DirectionButton) firstThree.add(new DirectionButton( tr_dot, trdn_dot, "NE", "Sets the orientation to the North-East", l, group, selection.equals("NE"))); 80 b.setEnabled(enable); 81 b = (DirectionButton) secondThree.add(new DirectionButton( ml_dot, mldn_dot, "W", "Sets the orientation to the West", l, group, selection.equals("W"))); 82 b.setEnabled(enable); 83 b = (DirectionButton) secondThree.add(new DirectionButton( c_dot, cdn_dot, "C", "Sets the orientation to the Center", l, group, selection.equals("C"))); 84 b.setEnabled(enable); 85 b = (DirectionButton) secondThree.add(new DirectionButton( mr_dot, mrdn_dot, "E", "Sets the orientation to the East", l, group, selection.equals("E"))); 86 b.setEnabled(enable); 87 b = (DirectionButton) thirdThree.add(new DirectionButton( bl_dot, bldn_dot, "SW", "Sets the orientation to the South-West", l, group, selection.equals("SW"))); 88 b.setEnabled(enable); 89 b = (DirectionButton) thirdThree.add(new DirectionButton( bm_dot, bmdn_dot, "S", "Sets the orientation to the South", l, group, selection.equals("S"))); 90 b.setEnabled(enable); 91 b = (DirectionButton) thirdThree.add(new DirectionButton( br_dot, brdn_dot, "SE", "Sets the orientation to the South-East", l, group, selection.equals("SE"))); 92 b.setEnabled(enable); 93 94 add(firstThree); 95 add(secondThree); 96 add(thirdThree); 97 } 98 99 public String getSelection() { 100 return group.getSelection().getActionCommand(); 101 } 102 103 public void setSelection( String selection ) { 104 Enumeration e = group.getElements(); 105 while( e.hasMoreElements() ) { 106 JRadioButton b = (JRadioButton)e.nextElement(); 107 if( b.getActionCommand().equals(selection) ) { 108 b.setSelected(true); 109 } 110 } 111 } 112 113 public ImageIcon bl_dot = loadImageIcon("bl.gif","bottom left layout button"); 115 public ImageIcon bldn_dot = loadImageIcon("bldn.gif","selected bottom left layout button"); 116 public ImageIcon bm_dot = loadImageIcon("bm.gif","bottom middle layout button"); 117 public ImageIcon bmdn_dot = loadImageIcon("bmdn.gif","selected bottom middle layout button"); 118 public ImageIcon br_dot = loadImageIcon("br.gif","bottom right layout button"); 119 public ImageIcon brdn_dot = loadImageIcon("brdn.gif","selected bottom right layout button"); 120 public ImageIcon c_dot = loadImageIcon("c.gif","center layout button"); 121 public ImageIcon cdn_dot = loadImageIcon("cdn.gif","selected center layout button"); 122 public ImageIcon ml_dot = loadImageIcon("ml.gif","middle left layout button"); 123 public ImageIcon mldn_dot = loadImageIcon("mldn.gif","selected middle left layout button"); 124 public ImageIcon mr_dot = loadImageIcon("mr.gif","middle right layout button"); 125 public ImageIcon mrdn_dot = loadImageIcon("mrdn.gif","selected middle right layout button"); 126 public ImageIcon tl_dot = loadImageIcon("tl.gif","top left layout button"); 127 public ImageIcon tldn_dot = loadImageIcon("tldn.gif","selected top left layout button"); 128 public ImageIcon tm_dot = loadImageIcon("tm.gif","top middle layout button"); 129 public ImageIcon tmdn_dot = loadImageIcon("tmdn.gif","selected top middle layout button"); 130 public ImageIcon tr_dot = loadImageIcon("tr.gif","top right layout button"); 131 public ImageIcon trdn_dot = loadImageIcon("trdn.gif","selected top right layout button"); 132 133 public ImageIcon loadImageIcon(String filename, String description) { 134 String path = "/resources/images/buttons/" + filename; 135 return new ImageIcon(getClass().getResource(path), description); 136 } 137 138 139 public class DirectionButton extends JRadioButton { 140 141 144 public DirectionButton(Icon icon, Icon downIcon, String direction, 145 String description, ActionListener l, 146 ButtonGroup group, boolean selected) 147 { 148 super(); 149 this.addActionListener(l); 150 setFocusPainted(false); 151 setHorizontalTextPosition(CENTER); 152 group.add(this); 153 setIcon(icon); 154 setSelectedIcon(downIcon); 155 setActionCommand(direction); 156 getAccessibleContext().setAccessibleName(direction); 157 getAccessibleContext().setAccessibleDescription(description); 158 setSelected(selected); 159 } 160 161 public boolean isFocusTraversable() { 162 return false; 163 } 164 165 public void setBorder(Border b) { 166 } 167 } 168 } 169 | Popular Tags |