summaryrefslogtreecommitdiff
path: root/src/tetris/gui/WrittenButton.java
blob: 2a0e55f44bc833d3aa59255ca721546ef1d84ffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package tetris.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.AbstractButton;
import javax.swing.JLabel;

import tetris.gui.images.ImageRes;

/**
 * WrittenButton is a button with a handwritten font and a pencil roll-over.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 8 December 2009.
 */
@SuppressWarnings("serial")
public class WrittenButton extends AbstractButton {
  /**
   * The pencil icon used in the roll-over.
   */
  private static final Image PENCIL = ImageRes.loadImage(ImageRes.WRITING_PENCIL);
  
  /**
   * Default font size.
   */
  private static final float DEFAULT_FONT_SIZE = 36;
  
  /**
   * How much pencil to show.
   */
  private static final int PENCIL_WIDTH = 50;
  
  /**
   * An adjustment to the Y of the pencil.
   */
  private static final int PENCIL_ADJUSTMENT = 10;
  
  /**
   * The button label.
   */
  private final JLabel my_label; 
  
  /**
   * Is the mouse over the button?
   */
  private boolean my_rolledover;
  
  /**
   * Default constrcutor.
   */  
  public WrittenButton() {
    super();
    
    my_rolledover = false;
    
    // Setup Button
    addMouseListener(new MouseHandler());
    setOpaque(false);
    
    // Setup Label
    my_label = new JLabel();
    add(my_label);
  }

  @Override
  public void setText(final String the_label) {
    super.setText(the_label);

    Font base_font;
    
    try {
      final InputStream fis = getClass().getResourceAsStream("/tetris/gui/handwriting.ttf");
      base_font = Font.createFont(Font.TRUETYPE_FONT, fis);
      fis.close();
      
    } catch (final FontFormatException the_exception) {
      base_font = Font.getFont(Font.SANS_SERIF);
      
    } catch (final IOException the_exception) {
      base_font = Font.getFont(Font.SANS_SERIF);
    }
    
    my_label.setText(the_label);
    my_label.setFont(base_font.deriveFont(DEFAULT_FONT_SIZE));
    
    // Set some hints for the layout manager.
    final Dimension ls = my_label.getPreferredSize();
    ls.width += PENCIL_WIDTH;
    setPreferredSize(ls);
    setMinimumSize(ls);
    setMaximumSize(ls);
  }
  
  @Override
  public String getText() {
    return my_label.getText();
  }
  
  @Override
  protected void paintComponent(final Graphics the_graphics) {
    super.paintComponent(the_graphics);
    
    if (my_rolledover) {
      final int x = my_label.getX() + my_label.getWidth();
      final int y = my_label.getY() + my_label.getHeight() -
                    PENCIL.getHeight(null) - PENCIL_ADJUSTMENT;

      the_graphics.drawImage(PENCIL, x, y, null);
    }
  }
  
  /**
   * Mouse handler for the button.
   * 
   * @author Jesse Morgan <jesterpm@u.washington.edu>
   * @version 1.0 8 Dec 2009
   */
  private class MouseHandler extends MouseAdapter {
    @Override
    public void mouseEntered(final MouseEvent the_event) {
      super.mouseEntered(the_event);
      
      my_rolledover = true;
      repaint();
    }
    
    @Override
    public void mouseExited(final MouseEvent the_event) {
      super.mouseExited(the_event);
      
      my_rolledover = false;
      repaint();
    }
    
    @Override
    public void mouseClicked(final MouseEvent the_event) {
      super.mouseClicked(the_event);
      
      final ActionEvent e = new ActionEvent(WrittenButton.this,
                                      ActionEvent.ACTION_PERFORMED,
                                      getText());
      
      WrittenButton.this.fireActionPerformed(e);
    }
  }
}