gann-cdf /
graphics
| 1 | package example.swing; |
||
| 2 | |||
| 3 | import org.gannacademy.cdf.graphics.ui.AppWindow; |
||
| 4 | |||
| 5 | import javax.swing.*; |
||
| 6 | import java.awt.event.ActionEvent; |
||
| 7 | import java.awt.event.ActionListener; |
||
| 8 | import java.awt.event.KeyEvent; |
||
| 9 | import java.util.ArrayList; |
||
| 10 | import java.util.List; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * This class implements ActionListener so that it can receive button action events |
||
| 14 | */ |
||
| 15 | public class SwingButton extends AppWindow implements ActionListener { |
||
| 16 | |||
| 17 | private List<Ball> balls; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 18 | |||
| 19 | // Swing is the "modern" Java UI system (as opposed to AWT) and all its components are prefixed "J" |
||
| 20 | private JButton addButton; |
||
|
0 ignored issues
–
show
Comprehensibility
Code Smell
introduced
by
|
|||
| 21 | |||
| 22 | // best practice is to avoid retyping literals (to avoid fat-fingering them) |
||
| 23 | private static final String ADD_COMMAND = "add"; |
||
| 24 | |||
| 25 | @Override |
||
| 26 | public void actionPerformed(ActionEvent e) { |
||
| 27 | if (e.getActionCommand() == ADD_COMMAND) { |
||
| 28 | balls.add(new Ball(getDrawingPanel())); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | @Override |
||
| 33 | protected void setup() { |
||
| 34 | balls = new ArrayList<>(); |
||
| 35 | balls.add(new Ball(getDrawingPanel())); |
||
| 36 | |||
| 37 | // create a new button with the ext "Add Ball" |
||
| 38 | addButton = new JButton("Add Ball"); |
||
| 39 | |||
| 40 | // associate a keyboard shortcut with the button (Alt+B) |
||
| 41 | addButton.setMnemonic(KeyEvent.VK_B); |
||
| 42 | |||
| 43 | // assign a "command" keyword to the button (in case there are many buttons being handled) |
||
| 44 | addButton.setActionCommand(ADD_COMMAND); |
||
| 45 | |||
| 46 | // assign ourselves as the actionlistener for this button |
||
| 47 | addButton.addActionListener(this); |
||
| 48 | |||
| 49 | /* |
||
| 50 | * Position the button on the screen. |
||
| 51 | * |
||
| 52 | * Note that we use the button's preferred size (which is calculated based on its contents), rather than its |
||
| 53 | * width, since it has _no width_, having not yet been added to the layout! |
||
| 54 | * |
||
| 55 | * Note also that we are turning off the layout manager (setLayout(null)) to allow for absolute positioning |
||
| 56 | */ |
||
| 57 | addButton.setBounds( |
||
| 58 | (getDrawingPanel().getWidth() - addButton.getPreferredSize().width) / 2, |
||
| 59 | (getDrawingPanel().getHeight() - addButton.getPreferredSize().height) / 2, |
||
| 60 | addButton.getPreferredSize().width, |
||
| 61 | addButton.getPreferredSize().height |
||
| 62 | ); |
||
| 63 | getDrawingPanel().setLayout(null); |
||
| 64 | |||
| 65 | // actually add the button to the Swing UI hierarchy on top of the drawing panel |
||
| 66 | getDrawingPanel().add(addButton); |
||
| 67 | } |
||
| 68 | |||
| 69 | @Override |
||
| 70 | protected void loop() { |
||
| 71 | for (Ball ball : balls) { |
||
| 72 | ball.move(); |
||
| 73 | } |
||
| 74 | sleep(10); |
||
| 75 | } |
||
| 76 | |||
| 77 | public static void main(String[] args) { |
||
| 78 | new SwingButton(); |
||
| 79 | } |
||
| 80 | } |
||
| 81 |