balloons
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
balloons [2018/08/17 08:27] – frchris | balloons [2018/08/17 08:43] – frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | [[https:// | ||
+ | |||
CheckList: | CheckList: | ||
- Add a method in the '' | - Add a method in the '' | ||
Line 5: | Line 7: | ||
- Change '' | - Change '' | ||
- Change '' | - Change '' | ||
- | - Change '' | + | - Change '' |
- Modify the '' | - Modify the '' | ||
Challenges: | Challenges: | ||
Line 12: | Line 14: | ||
- Instead of '' | - Instead of '' | ||
- | <code java Balloon.java> | + | <file java Balloon.java> |
import java.awt.Color; | import java.awt.Color; | ||
import java.awt.Graphics; | import java.awt.Graphics; | ||
Line 47: | Line 49: | ||
} | } | ||
- | </code> | + | </file> |
<code java BalloonDrop.java> | <code java BalloonDrop.java> | ||
+ | import java.awt.Color; | ||
import java.awt.Font; | import java.awt.Font; | ||
import java.awt.Graphics; | import java.awt.Graphics; | ||
Line 68: | Line 71: | ||
public static int HEIGHT=600; | public static int HEIGHT=600; | ||
private Font titleFont, regularFont; | private Font titleFont, regularFont; | ||
- | private ArrayList<Balloon& | + | private ArrayList<Balloon> balloons; |
private Timer timer; | private Timer timer; | ||
+ | /** | ||
+ | * The class constructor will initialize the ArrayList& | ||
+ | * and other object fields (variables), | ||
+ | * object from the javax.swing.* library. | ||
+ | * generate an Action Event, so that something happens | ||
+ | * in regular intervals without the user typing anything. | ||
+ | */ | ||
+ | public BalloonFall() | ||
+ | { | ||
+ | // | ||
+ | titleFont = new Font(" | ||
+ | regularFont = new Font(" | ||
+ | balloons = new ArrayList< | ||
+ | timer = new Timer(10, this); | ||
+ | timer.start(); | ||
+ | } | ||
+ | /** | ||
+ | * the main method makes an instance of our application and puts it in a JFrame | ||
+ | * that will end the application when it is closed. | ||
+ | * | ||
+ | */ | ||
+ | public static void main(String[] args) { | ||
+ | BalloonFall app= new BalloonFall(); | ||
+ | JFrame window = new JFrame(" | ||
+ | window.setSize(WIDTH, | ||
+ | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | window.getContentPane().add(app); | ||
+ | window.addKeyListener(app); | ||
+ | window.setVisible(true); | ||
+ | |||
+ | } | ||
+ | |||
+ | /** | ||
+ | * This is the method to change what is drawn to the screen: | ||
+ | */ | ||
+ | public void paintComponent(Graphics g){ | ||
+ | // | ||
+ | g.setColor(Color.WHITE); | ||
+ | g.fillRect(0, | ||
+ | g.setColor(Color.BLUE); | ||
+ | g.setFont(titleFont); | ||
+ | g.drawString(" | ||
+ | // | ||
+ | g.setColor(Color.BLACK); | ||
+ | g.setFont(regularFont); | ||
+ | g.drawString(" | ||
+ | for (Balloon x: | ||
+ | x.draw(g); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | /** | ||
+ | * These 3 methods need to be declared to implement the KeyListener Interface | ||
+ | * that listens for keys that are typed on the keyboard | ||
+ | */ | ||
+ | @Override | ||
+ | public void keyTyped(KeyEvent e) {} | ||
+ | |||
+ | @Override | ||
+ | public void keyPressed(KeyEvent e) {} | ||
+ | |||
+ | @Override | ||
+ | public void keyReleased(KeyEvent e) { | ||
+ | |||
+ | int size = Balloon.randomInt(50, | ||
+ | int x = Balloon.randomInt(0, | ||
+ | int y = Balloon.randomInt(-size, | ||
+ | balloons.add(new Balloon(x, | ||
+ | repaint(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | /** | ||
+ | * This method is needed to implement the ActionListener that listens for the timer | ||
+ | * e.getKeyChar () and e.getKeyCode () will return the char (or its code) | ||
+ | */ | ||
+ | public void actionPerformed ( ActionEvent e) { | ||
+ | // timer made an action event, so | ||
+ | int i = 0; | ||
+ | while ( i < balloons.size() ) { | ||
+ | balloons.get(i).drop(); | ||
+ | // | ||
+ | if ( balloons.get(i).getBottom() > HEIGHT ) { | ||
+ | balloons.remove (i); | ||
+ | }else{ | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | repaint (); | ||
+ | } | ||
+ | } | ||
</ | </ |
balloons.txt · Last modified: 2018/08/18 20:14 by cthiel