,
Introductions and the Enigma machine: 8:00 -> 16:35 (9:48-13:00)
Christopher: 46:24
Christopher’s ineffectiveness: 51:06 (50:00-52:00)
The game and frustration: 1:06:43 (1:09-1:13)
C-I-L-L-Y: 1:11:30
Category Archives: Lesson
GUI: JFrame
Classwork:
Use a JFrame to create an application, YI_ManyShapes.java and YI_ManyShapesJFrame.java that draws rectangles, ellipses and lines. Use different colors and fill.
Here is a good source for your application:
Here is a simple example:
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; /** * Write a description of class RectangleObject here. */ public class RectangleObject extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Construct a rectangle object Rectangle box = new Rectangle(5, 10, 20, 30); // draw the rectangle g2.draw(box); } }
The JFrame class:
import javax.swing.JFrame; /** * Draw an empty frame using JFrame. * */ public class RectangleJFrame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300,400); frame.setTitle("A Rectanlge Object in a JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleObject aRectangle = new RectangleObject(); frame.add(aRectangle); frame.setVisible(true); } }
Homework:
Write an application, YI_HappyFace.java using what you learned from Manyshapes to draw a happy face. Make sure to include the JFrame class also.
GUI – DialogDemo.java
Check edmodo.com for questions on content.
import javax.swing.JOptionPane; /* 1.4 example used by DialogDemo.java. */ class DialogViewer1{ public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hello, World!"); System.exit(0); } }
import javax.swing.JOptionPane; public class DialogViewer2 { public static void main(String[] args) { String name = JOptionPane.showInputDialog("What is your name?"); System.out.println(name); System.exit(0); } }
Homework: Prepare for more questions including JOptionPane.
GUI: Action Listener
ActionEvent, ActionListener, and MouseListener
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** An action listener that prints a message. */ public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("You clicked me!!!!"); } }
import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; /** This program demonstrates how to install an action listener. */ public class ButtonViewer { private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click here if you dare!"); frame.add(button); ActionListener listener = new ButtonListener(); button.addActionListener(listener); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; /** This component displays a circle that can be moved. */ public class ShapeComponent extends JComponent { private static final int SQUARE_X = 100; private static final int SQUARE_Y = 100; private static final int SQUARE_WIDTH = 50; private static final int SQUARE_HEIGHT = 50; private Ellipse2D.Double circle; public ShapeComponent() { // The circle that the paintComponent method draws circle = new Ellipse2D.Double(SQUARE_X, SQUARE_Y, SQUARE_WIDTH, SQUARE_HEIGHT); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw(circle); } /** Moves the rectangle to the given location. @param x the x-position of the new location @param y the y-position of the new location */ public void moveTo(int x, int y) { circle = new Ellipse2D.Double(x, y, SQUARE_WIDTH, SQUARE_HEIGHT); repaint(); } }
import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import javax.swing.JFrame; import java.awt.Color; /** This program displays a ShapeComponent. */ public class ShapeComponentViewer { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 400; public static void main(String[] args) { final ShapeComponent component = new ShapeComponent(); // Add mouse press listener class MousePressListener implements MouseListener { public void mousePressed(MouseEvent event) { int x = event.getX(); int y = event.getY(); component.moveTo(x, y); } // Do-nothing methods public void mouseReleased(MouseEvent event) {} public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} } MouseListener listener = new MousePressListener(); component.addMouseListener(listener); JFrame frame = new JFrame(); frame.getContentPane().setBackground(new Color(255, 0, 0)); frame.add(component); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Classwork:
Use the resources given to create a new GUI application, YI_CrazyCircle.java and its JFrame to generate random colors background and random filled circles wherever you click the mouse on the window. PLEASE INCLUDE SCREEN SHOT OF YOUR WORK.
Hint: To be able to also change the background at random create a rectangle of size equivalent to the JFrame. Instead of creating two public classes, create one inner class, the JComponnet inside main so it can have access to the JFrame size.
Thirtieth Day – Applets
October 21st, 2015
Classwork:
Big Java – Chapter 4: Applets (Find it on the menu)
Applets are an easy way to get started with GUI applications. However, I recommend to use JFrames.
RectangleApplet is on page 143
FontApplet is on page 153
RectangleApplet.java
SolidEllipseApplet.java
FontApplet.java