001package util.ingamemenu; 002 003import java.awt.Color; 004import java.awt.Dimension; 005import java.awt.Graphics; 006import java.awt.Image; 007import java.awt.event.MouseAdapter; 008import java.awt.event.MouseEvent; 009import java.awt.event.MouseListener; 010import java.io.File; 011import java.io.IOException; 012import javax.imageio.ImageIO; 013import javax.swing.JComponent; 014 015 016/** 017 * @author Yaqi Zhang 018 * 019 */ 020public class GameButton extends JComponent { 021 /** 022 * String locates at the bottom of the button 023 */ 024 public static final int BOTTOM = 1; 025 026 /** 027 * String locates at the center of the button 028 */ 029 public static final int CENTER = 0; 030 /** 031 * String locates at the top of the button 032 */ 033 public static final int TOP = 2; 034 private static final long serialVersionUID = 1L; 035 private static final int DEFAULT_BUTTON_WIDTH = 130; 036 private static final int DEFAULT_BUTTON_HEIGHT = 40; 037 038 private Image myImg; 039 private String myImgName; 040 private String myCommand = ""; 041 private MouseListener myMouseListener; 042 private Dimension mySize; 043 private int myStringLocation = CENTER; 044 045 /** 046 * Create a game button that allows to customize the image of the button. 047 * 048 * @param fileName of the button image 049 */ 050 public GameButton (String fileName) { 051 setName(fileName); 052 setImage(fileName); 053 mySize = new Dimension(DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT); 054 setPreferredSize(mySize); 055 setMouseListener(); 056 } 057 058 /** 059 * Create a game button that allows to customize the image of the button. 060 * 061 * @param fileName of the button image 062 * @param command String appears on the button. 063 */ 064 public GameButton (String fileName, String command) { 065 this(fileName); 066 setString(command); 067 } 068 069 /** 070 * Create a game button that allows to customize the image of the button. 071 * 072 * @param fileName of the button image 073 * @param command String appears on the button. 074 * @param ml MouseListener 075 */ 076 public GameButton (String fileName, String command, MouseListener ml) { 077 this(fileName, command); 078 addMouseListener(ml); 079 } 080 081 /** 082 * Create a game button that allows to customize the image of the button. 083 * 084 * @param fileName of the button image 085 * @param command String appears on the button. 086 * @param ml MouseListener 087 * @param size of the Button 088 */ 089 public GameButton (String fileName, String command, MouseListener ml, 090 Dimension size) { 091 this(fileName, command, ml); 092 setSize(size); 093 } 094 095 /** 096 * Set String appears on the button. 097 * 098 * @param command String appears on the button 099 */ 100 public void setString (String command) { 101 myCommand = command; 102 } 103 104 /** 105 * Set String appears on the button. 106 * 107 * @param command String appears on the button 108 * @param location of the String appear on the button 109 */ 110 public void setString (String command, int location) { 111 myStringLocation = location; 112 myCommand = command; 113 } 114 115 @Override 116 protected void paintComponent (Graphics pen) { 117 Dimension myButtonSize = getSize(); 118 pen.drawImage(myImg, 0, 0, myButtonSize.width, myButtonSize.height, 119 null); 120 pen.setColor(Color.BLACK); 121 if (myStringLocation == CENTER) { 122 pen.drawString(myCommand, (getSize().width / 4), (int) (getSize() 123 .getHeight() / 2)); 124 } 125 else if (myStringLocation == BOTTOM) { 126 pen.drawString(myCommand, (getSize().width / 4), (int) (getSize() 127 .getHeight() - 10)); 128 } 129 else if (myStringLocation == TOP) { 130 pen.drawString(myCommand, (getSize().width / 4), 10); 131 } 132 } 133 134 @Override 135 public void paint (Graphics pen) { 136 paintComponent(pen); 137 } 138 139 /** 140 * @param img image 141 */ 142 public void setImage (Image img) { 143 myImg = img; 144 } 145 146 /** 147 * @param fileName in String 148 */ 149 public void setImage (String fileName) { 150 myImgName = fileName; 151 setImage(fileName, "normal"); 152 } 153 154 private void changeImage (String state) { 155 setImage(myImgName, state); 156 } 157 158 private void setMouseListener () { 159 removeMouseListener(myMouseListener); 160 myMouseListener = new MouseAdapter() { 161 @Override 162 public void mousePressed (MouseEvent arg0) { 163 changeImage("pressed"); 164 } 165 166 @Override 167 public void mouseReleased (MouseEvent arg0) { 168 changeImage("normal"); 169 } 170 }; 171 addMouseListener(myMouseListener); 172 } 173 174 /** 175 * @param width 176 * @param height 177 */ 178 public void setButtonSize (int width, int height) { 179 mySize = new Dimension(width, height); 180 setPreferredSize(mySize); 181 } 182 183 private void setImage (String fileName, String state) { 184 try { 185 myImg = ImageIO.read(new File("src/util/ingamemenu/buttonimg/" 186 + fileName + "." + state + ".png")); 187 } 188 catch (IOException e) { 189 System.out.println("src/vooga/platformer/gui/menu/buttonimg/" 190 + fileName + "." + state + ".png was not found"); 191 e.printStackTrace(); 192 } 193 repaint(); 194 } 195 196 /** 197 * @param size of the menu 198 */ 199 @Override 200 public void setSize (Dimension size) { 201 mySize = size; 202 setPreferredSize(size); 203 } 204 205 @Override 206 public Dimension getSize () { 207 return mySize; 208 } 209}