Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. public void mouseClicked(MouseEvent e) {//This is a differn't event. This is not executed by clicking on the same button. It's a differn't one.
  2. click(new Point(e.getX() + gridScrollPaneViewport.getViewPosition().x, e.getY() + gridScrollPaneViewport.getViewPosition().y));
  3. }
  4.  
  5. public void click(Point point) {
  6. map.selectPlot((PlotGUI) map.getComponentAt(point));
  7. }
  8. public void selectPlot(PlotGUI plot) {
  9. ChickenTycoon.getInstance().getScreen().getPlayingScreen().sideBar
  10. .setPlot(plot);
  11. selectedPlot = plot;
  12. }
  13.  
  14. public void setPlot(PlotGUI plot) {
  15. title.setText(plot.getName() + plot.getX() + plot.getY());
  16. for (Component comp : stuffPanel.getComponents()) {
  17. stuffPanel.remove(comp);
  18. }
  19. for (Component comp : plot.getComponentList()) {
  20. stuffPanel.add(comp);
  21. }
  22. update();
  23. }
  24.  
  25. public void update() {
  26. if (ChickenTycoon.getInstance().getScreen().getPlayingScreen().map
  27. .getSelectedPlot() != null) {
  28. ChickenTycoon.getInstance().getScreen().getPlayingScreen().map
  29. .getSelectedPlot().update();
  30. }
  31. }
  32.  
  33. public void update() {
  34. Log.m("Updating LandGUI");
  35. ((CButton) componentList[0]).setText("Buy for $ "//I know I am casting this to a CButton, it's my custom version of JButton. I tried it with a JButton and the same thing happens.
  36. + ((Land) parentPlot).getPurchasePrice());
  37. }
  38.  
  39. package gui;
  40.  
  41. import game.GameConfiguration;
  42.  
  43. import java.awt.BasicStroke;
  44. import java.awt.Color;
  45. import java.awt.Dimension;
  46. import java.awt.Font;
  47. import java.awt.FontMetrics;
  48. import java.awt.Frame;
  49. import java.awt.Graphics;
  50. import java.awt.Graphics2D;
  51. import java.awt.Insets;
  52. import java.awt.RenderingHints;
  53. import java.awt.Shape;
  54. import java.awt.Stroke;
  55. import java.awt.event.ComponentEvent;
  56. import java.awt.event.ComponentListener;
  57. import java.awt.geom.Area;
  58. import java.awt.geom.Rectangle2D;
  59. import java.awt.geom.RoundRectangle2D;
  60.  
  61. import javax.swing.JButton;
  62. import javax.swing.JOptionPane;
  63. import javax.swing.SwingUtilities;
  64.  
  65. import chicken.GlobalConfiguration;
  66.  
  67. public class CButton extends JButton implements ComponentListener {
  68. protected static final int BORDER_WIDTH = 5;
  69. private static final Font font = GlobalConfiguration.font;
  70. private static final Insets INSETS_MARGIN = new Insets(2, 5, 2, 5);
  71.  
  72. private static final long serialVersionUID = 1L;
  73. protected Area m_areaDraw = null;
  74. private Area m_areaFill = null;
  75.  
  76. private double m_dHeightDraw = 0d;
  77. private double m_dHeightFill = 0d;
  78. private double m_dWidthDraw = 0d;
  79.  
  80. private double m_dWidthFill = 0d;
  81. private int m_nMinHeight = 0;
  82. private int m_nMinWidth = 0;
  83.  
  84. private int m_nStringHeightMax = 0;
  85. private int m_nStringWidthMax = 0;
  86. private RoundRectangle2D m_rrect2dDraw = null;
  87. private RoundRectangle2D m_rrect2dFill = null;
  88. private Shape m_shape = null;
  89.  
  90. public CButton(String strLabel) {
  91. setContentAreaFilled(false);
  92. setMargin(CButton.INSETS_MARGIN);
  93. setFocusPainted(false);
  94. addComponentListener(this);
  95. setFont(CButton.font);
  96. setText(strLabel);
  97. }
  98.  
  99. @Override
  100. public void componentHidden(ComponentEvent e) {
  101. }
  102.  
  103. @Override
  104. public void componentMoved(ComponentEvent e) {
  105. }
  106.  
  107. // Needed if we want this button to resize
  108. @Override
  109. public void componentResized(ComponentEvent e) {
  110. m_shape = new Rectangle2D.Float(0, 0, getBounds().width,
  111. getBounds().height);
  112. m_dWidthFill = (double) getBounds().width - 1;
  113. m_dHeightFill = (double) getBounds().height - 1;
  114. m_dWidthDraw = ((double) getBounds().width - 1)
  115. - (CButton.BORDER_WIDTH - 1);
  116. m_dHeightDraw = ((double) getBounds().height - 1)
  117. - (CButton.BORDER_WIDTH - 1);
  118. setShape();
  119. repaint();
  120. }
  121.  
  122. @Override
  123. public void componentShown(ComponentEvent e) {
  124. }
  125.  
  126. @Override
  127. public boolean contains(int nX, int nY) {
  128. if ((null == m_shape) || m_shape.getBounds().equals(getBounds())) {
  129. m_shape = new Rectangle2D.Float(0, 0, this.getBounds().width,
  130. this.getBounds().height);
  131. }
  132. return m_shape.contains(nX, nY);
  133. }
  134.  
  135. @Override
  136. public void paintBorder(Graphics g) {
  137. Graphics2D g2 = (Graphics2D) g;
  138. RenderingHints hints = new RenderingHints(
  139. RenderingHints.KEY_ANTIALIASING,
  140. RenderingHints.VALUE_ANTIALIAS_ON);
  141. g2.setRenderingHints(hints);
  142. g2.setColor(Color.black);
  143. Stroke strokeOld = g2.getStroke();
  144. g2.setStroke(new BasicStroke(CButton.BORDER_WIDTH,
  145. BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
  146. g2.draw(m_areaDraw);
  147.  
  148. if (getModel().isRollover()) {
  149. g2.setColor(Color.GRAY);
  150. g2.draw(m_areaDraw);
  151. }
  152. g2.setStroke(strokeOld);
  153. };
  154.  
  155. @Override
  156. public void paintComponent(Graphics g) {
  157. Graphics2D g2 = (Graphics2D) g;
  158. RenderingHints hints = new RenderingHints(
  159. RenderingHints.KEY_ANTIALIASING,
  160. RenderingHints.VALUE_ANTIALIAS_ON);
  161. g2.setRenderingHints(hints);
  162. if (getModel().isArmed()) {
  163. g2.setColor(Color.CYAN.darker());
  164. } else {
  165. g2.setColor(Color.CYAN);
  166. }
  167. g2.fill(m_areaFill);
  168. super.paintComponent(g2);
  169. }
  170.  
  171. private void setShape() {
  172. // Area
  173. double dArcLengthFill = Math.min(m_dWidthFill, m_dHeightFill);
  174. m_rrect2dFill = new RoundRectangle2D.Double(0d, 0d, m_dWidthFill,
  175. m_dHeightFill, dArcLengthFill, dArcLengthFill);
  176. // WARNING: arclength and archeight are divided by 2
  177. // when they get into the roundedrectangle shape
  178. m_areaFill = new Area(m_rrect2dFill);
  179. // Border
  180. double dArcLengthDraw = Math.min(m_dWidthDraw, m_dHeightDraw);
  181. m_rrect2dDraw = new RoundRectangle2D.Double(
  182. (CButton.BORDER_WIDTH - 1) / 2, (CButton.BORDER_WIDTH - 1) / 2,
  183. m_dWidthDraw, m_dHeightDraw, dArcLengthDraw, dArcLengthDraw);
  184. m_areaDraw = new Area(m_rrect2dDraw);
  185. }
  186.  
  187. @Override
  188. public void setText(final String strText) {
  189. CButton.super.setText(strText);
  190. Frame frame = JOptionPane.getRootFrame();
  191. FontMetrics fm = frame.getFontMetrics(font);
  192. m_nStringWidthMax = fm.stringWidth(getText());
  193. m_nStringWidthMax = Math.max(m_nStringWidthMax,
  194. fm.stringWidth(getText()));
  195. // WARNING: use getMargin. it refers to dist btwn text and border.
  196. // Also use getInsets. it refers to the width of the border
  197. int nWidth = Math.max(m_nMinWidth, m_nStringWidthMax + getMargin().left
  198. + getInsets().left + getMargin().right + getInsets().right);
  199. m_nStringHeightMax = fm.getHeight();
  200. // WARNING: use getMargin. it refers to dist btwn text and border.
  201. // Also use getInsets. it refers to the width of the border
  202. int nHeight = Math.max(m_nMinHeight, m_nStringHeightMax
  203. + getMargin().left + getInsets().left + getMargin().right
  204. + getInsets().right);
  205. setPreferredSize(new Dimension(
  206. nWidth + ((2 * getFont().getSize()) / 5), nHeight
  207. + ((2 * getFont().getSize()) / 5)));
  208. // Set the initial draw and fill dimensions
  209. setShape();
  210. }
  211. }
  212.  
  213. button.setText("Some Text");
  214. buttonContainer.repaint();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement