Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.

Slides:



Advertisements
Similar presentations
Drawing in a frame – Java GUI
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
Unit 101 Java GUI Components and Events  GUI Components and Containers  Adding Components to Containers  GUI Events  GUI Events Classes  Learning.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
Graphical User Interfaces
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Interfaces & Polymorphism part 2:
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
GUI programming Graphical user interface-based programming.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Even-Driven Programming and basic Graphical User Interface.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Layout Managers Arranges and lays out the GUI components on a container.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Session 22 Chapter 11: Implications of Inheritance.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Computer Science 209 GUIs Model/View/Controller Layouts.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Lesson 33: Layout management and drawing – Java GUI.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Introducing, the JFrame Gives us a work area beside System.out.println.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Multiple buttons and action calls
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Welcome To java
Graphical User Interfaces
Computer Science 209 Graphics and GUIs.
Chapter 4 Interface Types and Polymorphism Part 2
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Graphical user interface-based programming
Steps to Creating a GUI Interface
The calculator – Java GUI
A Quick Java Swing Tutorial
CiS 260: App Dev I Chapter 6: GUI and OOD.
Lecture 4: Standard Java Graphics
Presentation transcript:

Lesson 34: Layering Images with Java GUI

The FlowLayout RECAP

The LEFT and CENTER layout // FlowLayoutTest.java - demo flowlayout import java.awt.*; import javax.swing.*; class FlowLayoutTest{ public static void main (String[] args){ JFrame frame = new JFrame("FlowLayoutTest.LEFT"); Container pane = frame.getContentPane(); pane.setLayout(new FlowLayout(FlowLayout.LEFT)); pane.add(new JButton("Button 1")); pane.add(new JLabel("Label 2")); pane.add(new JButton("Button 3")); pane.add(new JLabel("Label 4")); pane.add(new JButton("Button 5")); frame.pack(); frame.show(); } CENTER LEFT RECAP

Adding buttons to actions and frames -with a FlowLayout RECAP

Adding a button to StartTest // StarTestQuit.java - add a quit button to StartTest import java.awt.*; import javax.swing.*; class StarTestQuit{ public static void main (String[] args){ JFrame frame = new JFrame("StartTest"); Container pane = frame.getContentPane(); Star star = new Star(); JButton Quit = new JButton("Quit"); pane.setLayout(new FlowLayout()); Quit.addActionListener(new GoodBye()); pane.add(Quit); pane.add(star); frame.pack(); frame.show(); } RECAP

A simple Drawing program RECAP

// SimplePaint.java - drawing with a mouse import java.awt.*; import javax.swing.*; class SimplePaint{ public static void main (String[] args){ JFrame frame = new JFrame("SimplePaint"); Container pane = frame.getContentPane(); DrawingCanvas canvas = new DrawingCanvas(); PaintListener listener=new PaintListener(); canvas.addMouseMotionListener(listener); pane.add(canvas); frame.pack(); frame.show(); } RECAP

// DrawingCanvas.java - a blank canvas import javax.swing.*; import java.awt.*; class DrawingCanvas extends JComponent{ public Dimension getMinimumSize(){ return new Dimension(SIZE, SIZE); } public Dimension getPreferredSize(){ return new Dimension(SIZE, SIZE); } private static final int SIZE=500; } RECAP

// PaintListener.java import java.awt.*; import java.awt.event.*; class PaintListener implements MouseMotionListener{ public void mouseDragged(MouseEvent e) { DrawingCanvas canvas = (DrawingCanvas)e.getSource(); Graphics g = canvas.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); } public void mouseMoved(MouseEvent e){} private int radius =3; private int diameter = radius*2; } Each time the mouse is moved – while the position of the over the component that is being listened to, the mouseDragged() method is called. Each change in position detected by the system generates another call to mouseDragged(). This results in many calls by a single dragging of the mouse. Each call is passed a a MouseEvent object that contains, among other things, a reference to the component that generated the event and the coordinates of the mouse at that time the event was generated. The calls e.getSource() returns a generic reference to the component that generated the event. In general the event could have come from any component not just DrawingCanvas. Here it is explicitly cast to the reference a DrawingCanvas object RECAP

The outcome RECAP

Some problems with our simple drawing program…

Everytime a window is placed over the drawing, the text that was overlapped is removed when the window becomes active again (let us take a look)

Everytime a window is placed over the drawing, the text that was overlapped is removed when the window becomes active again

The Problem is that the image is drawn only once and only on the visible window. What we need is a “shadow” offscreenImage we can call when the frame gets back the focus. In this example we will see how we can modify our paint program to accomplish this! The Problem

// PaintListener2.java - paints on a DrwaingCanvas2 // and it is associated with an offscreenImage. import java.awt.*; import java.awt.event.*; class PaintListener2 implements MouseMotionListener{ public void mouseDragged(MouseEvent e) { DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource(); Graphics g = canvas.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); // duplicating the drawing on the offscreenImage Image image= canvas.getOffscreenImage(); g = image.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); } public void mouseMoved(MouseEvent e){} protected int radius =3; protected int diameter = radius*2; } Importing libraries This class implements the interface MouseMotionListener The interface MouseMotionListener requires that two methods are implemented: 1)mouseDragged (button clicked and held down) 2)mouseMoved (no button clicked and held down)

// PaintListener2.java - paints on a DrwaingCanvas2 // and it is associated with an offscreenImage. import java.awt.*; import java.awt.event.*; class PaintListener2 implements MouseMotionListener{ public void mouseDragged(MouseEvent e) { DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource(); Graphics g = canvas.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); // duplicating the drawing on the offscreenImage Image image= canvas.getOffscreenImage(); g = image.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); } public void mouseMoved(MouseEvent e){} protected int radius =3; protected int diameter = radius*2; }

// PaintListener2.java - paints on a DrwaingCanvas2 // and it is associated with an offscreenImage. import java.awt.*; import java.awt.event.*; class PaintListener2 implements MouseMotionListener{ public void mouseDragged(MouseEvent e) { DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource(); Graphics g = canvas.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); // duplicating the drawing on the offscreenImage Image image= canvas.getOffscreenImage(); g = image.getGraphics(); g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter); } public void mouseMoved(MouseEvent e){} protected int radius =3; protected int diameter = radius*2; } Each time the mouse is moved – while the position of the over the component that is being listened to, the mouseDragged() method is called. Each change in position detected by the system generates another call to mouseDragged(). This results in many calls by a single dragging of the mouse. Each call is passed a a MouseEvent object that contains, among other things, a reference to the component that generated the event and the coordinates of the mouse at that time the event was generated. Drawing to both the offscreenimage and the canvas!!

Importing the libraries Creating a class called SimplePaint2 Creating a main section Creating a frame named frame and labeled SimplePaint Creating a container named pane, and associating it with our frame called frame. // SimplePaint2.java - drawing with a mouse import java.awt.*; import javax.swing.*; class SimplePaint2{ public static void main (String[] args){ JFrame frame = new JFrame("SimplePaint"); Container pane = frame.getContentPane(); DrawingCanvas2 canvas = new DrawingCanvas2(); PaintListener2 listener=new PaintListener2(); canvas.addMouseMotionListener(listener); pane.add(canvas); frame.pack(); frame.show(); }

// SimplePaint2.java - drawing with a mouse import java.awt.*; import javax.swing.*; class SimplePaint2{ public static void main (String[] args){ JFrame frame = new JFrame("SimplePaint"); Container pane = frame.getContentPane(); DrawingCanvas2 canvas = new DrawingCanvas2(); PaintListener2 listener=new PaintListener2(); canvas.addMouseMotionListener(listener); pane.add(canvas); frame.pack(); frame.show(); } Creating a new object named canvas based on the DrawingCanvas2 class (we will look at it later) Creating a listener based on the PainListener2 class Associating the listener to the canvas

// SimplePaint2.java - drawing with a mouse import java.awt.*; import javax.swing.*; class SimplePaint2{ public static void main (String[] args){ JFrame frame = new JFrame("SimplePaint"); Container pane = frame.getContentPane(); DrawingCanvas2 canvas = new DrawingCanvas2(); PaintListener2 listener=new PaintListener2(); canvas.addMouseMotionListener(listener); pane.add(canvas); frame.pack(); frame.show(); } Adding the canvas object to the container Showing the frame Packing the frame to smallest size

// DrawingCanvas2.java - a blank canvas that remembers // drawing operations using an offscreen image import javax.swing.*; import java.awt.*; class DrawingCanvas2 extends JComponent{ public void paint(Graphics g){ if (offscreenImage!=null) g.drawImage(offscreenImage,0,0,SIZE,SIZE,null); } public Image getOffscreenImage(){ if (offscreenImage==null) offscreenImage=createImage(SIZE,SIZE); return offscreenImage; } public Dimension getMinimumSize(){ return new Dimension(SIZE, SIZE); } public Dimension getPreferredSize(){ return new Dimension(SIZE, SIZE); } private static final int SIZE=500; private Image offscreenImage; } Importing libraries This class is an extension of the JComponent class from the swing library

// DrawingCanvas2.java - a blank canvas that remembers // drawing operations using an offscreen image import javax.swing.*; import java.awt.*; class DrawingCanvas2 extends JComponent{ public void paint(Graphics g){ if (offscreenImage!=null) g.drawImage(offscreenImage,0,0,SIZE,SIZE,null); } public Image getOffscreenImage(){ if (offscreenImage==null) offscreenImage=createImage(SIZE,SIZE); return offscreenImage; } public Dimension getMinimumSize(){ return new Dimension(SIZE, SIZE); } public Dimension getPreferredSize(){ return new Dimension(SIZE, SIZE); } private static final int SIZE=500; private Image offscreenImage; } The first parameter for drawImage is a reference to the offscreenimage. The next two are the coordinates where the image should be placed on the canvas (we are filling the canvas from 0,0). The next is the width and the height of the image (in our case the size of the canvas). If anything has been drawn, and offscreenImage has been created. The method paint() calls g.drawImage() to transfer the offscreen memory image to the computer screen. Manipulating this we can stretch and move the image!!

// DrawingCanvas2.java - a blank canvas that remembers // drawing operations using an offscreen image import javax.swing.*; import java.awt.*; class DrawingCanvas2 extends JComponent{ public void paint(Graphics g){ if (offscreenImage!=null) g.drawImage(offscreenImage,0,0,SIZE,SIZE,null); } public Image getOffscreenImage(){ if (offscreenImage==null) offscreenImage=createImage(SIZE,SIZE); return offscreenImage; } public Dimension getMinimumSize(){ return new Dimension(SIZE, SIZE); } public Dimension getPreferredSize(){ return new Dimension(SIZE, SIZE); } private static final int SIZE=500; private Image offscreenImage; } This method is used by the listener to get a reference to the image, so we can draw on it. Private instance variable The first time getOffscreenImage () is called, the offscreen image is created by calling createImage(). The method createImage() is implicitly defined for DrawingCanvas2 because it extends Jcomponent and createImage() is defined there. We placed createImage() here instead of in the Drawingcanvas2 constructor because at the time the constructor is called Jcomponent may not be ready to create an offscreen image. Once the Jcomponent has been displayed in show(), we can create the offscreenImage.

// DrawingCanvas2.java - a blank canvas that remembers // drawing operations using an offscreen image import javax.swing.*; import java.awt.*; class DrawingCanvas2 extends JComponent{ public void paint(Graphics g){ if (offscreenImage!=null) g.drawImage(offscreenImage,0,0,SIZE,SIZE,null); } public Image getOffscreenImage(){ if (offscreenImage==null) offscreenImage=createImage(SIZE,SIZE); return offscreenImage; } public Dimension getMinimumSize(){ return new Dimension(SIZE, SIZE); } public Dimension getPreferredSize(){ return new Dimension(SIZE, SIZE); } private static final int SIZE=500; private Image offscreenImage; } Setting our preferred and minimum size to the container manager Private protected variable

The outcome

Lessons Learned