CS 145 Lecture 23 – GUIs, Non-zero Gravity
Agenda
- TIOBE flip
- Swing GUI framework
- a tease of inheritance
- event-driven programming
- painting dropping circles
- composition
- collaboration graphs in SMC
- law #31: try local first
Code
DroppingCircles.java
package prefinal; import javax.swing.JFrame; public class DroppingCircles { public static void main(String[] args) { JFrame frame = new JFrame("Drop Circles"); // frame.add(new JButton("Click me!")); // frame.add(new JLabel("Read me!")); frame.add(new DroppingCirclesCanvas()); frame.setSize(512, 512); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
DroppingCirclesCanvas.java
package prefinal; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JPanel; public class DroppingCirclesCanvas extends JPanel implements MouseListener { private Sphere sphere; public DroppingCirclesCanvas() { addMouseListener(this); } protected void paintComponent(Graphics g) { // g.fillOval(50, 50, 100, 25); if (sphere != null) { sphere.draw(g); } } public void mouseClicked(MouseEvent e) { System.out.println(e.getX()); System.out.println(e.getY()); sphere = new Sphere(e.getX(), e.getY()); repaint(); } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } }
Sphere.java
package prefinal; import java.awt.Color; import java.awt.Graphics; public class Sphere { public static final int RADIUS = 25; private int centerX; private int centerY; public Sphere(int x, int y) { centerX = x; centerY = y; } public void draw(Graphics g) { g.setColor(Color.RED); g.fillOval(centerX - RADIUS, centerY - RADIUS, 2 * RADIUS, 2 * RADIUS); } }
Haiku
Math was just okay.
Then I made my own worlds.
And they needed laws.
Then I made my own worlds.
And they needed laws.