Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.

Slides:



Advertisements
Similar presentations
Chapter 14 Advanced GUI ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Advertisements

Custom Painting Gestione della Grafica customizzata Vedi anche:
Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
User Interfaces II GUI – Awt, Swing, Web
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Fun with Swing Chapter 9. Overview Swing vs. AWT Creating windows and panels. Displaying formatted text in panels. Drawing graphics (lines, circles, etc.)
Made with love, by Zachary Langley Applets The Graphics Presentation.
Web Design & Development Lecture 19. Java Graphics 2.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
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.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSE 341, S. Tanimoto Java-PolyDraw- 1 A Java Case Study: An Applet for Drawing Polygons Motivation -- See a number of AWT features integrated. Get ideas.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Graphical User Interfaces, 2D Graphics & Game Programming.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
© The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces.
Event Handling Events and Listeners Timers and Animation.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
© A+ Computer Science - Chicken yeller = new Chicken();
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Even-Driven Programming and basic Graphical User Interface.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
CIS 3.5 Lecture 2.2 More programming with "Processing"
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.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
Java Swing One of the most important features of Java is its ability to draw graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
A First Look at GUI Applications
Java Swing.
More programming with "Processing"
A+ Computer Science METHODS.
Presentation transcript:

Rectangles moving and responding to the mouse

We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled or back again –Starting unfilled We can drag rectangles too

Classes Needed Rectangle –Particular requirement: needs to know if its filled or not –Needs to know how to draw itself

import java.awt.*; public class Rect extends Rectangle { private boolean fillRect = false; public Rect() { this.x = 100; this.y = 100; this.height = 40; this.width = 30; }

public void setfillRect(boolean fillRect) { this.fillRect = fillRect; } public boolean getfillRect() { return fillRect; }

public void drawMyRect (Graphics g) { Graphics2D g2 = (Graphics2D) g; if (this.getfillRect()) g2.fill(this); else g2.draw(this); } }

Pile of Rectangles Need to be able to keep track of your place and cycle through In Java, you do that with Iterators (see Laszlo Design Patterm Chapter)

Iterators Cf Chapter 6 in the subject guide

Classes Needed 2 Panel (we will call it View) –Needs to Listen for Mouse clicking and Mouse dragging –So needs to implement Mouse Listeners Clicking, Entering, dragging, Pressing, Releasing…

Data for View Various Booleans to keep track of what you are doing: –Maybe just dragging Rectangle Pile –ArrayList We will need two variables for Rectangles

Method 1: Constructor public View(java.util.ArrayList d) { rectPile = d; this.addMouseListener(this); this.addMouseMotionListener(this); this.setFocusable(true); this.setEnabled(true); }

MouseClicking 1.Change your mind about filling 2.Repaint

public void mouseClicked(MouseEvent e) { { n = findRectAtPoint(e.getPoint()); if (n != null) { if (n.getfillRect()) n.setfillRect(false); else n.setfillRect(true);} } repaint(); }

Mouse Pressing You are getting ready to drag 1.Work out what rectangle you have pressed 2.Work out how far you are from the UL corner 3.Set dragging to true

Mouse Pressing public void mousePressed(MouseEvent e) { n = findRectAtPoint(e.getPoint()); if (n != null) { dragging = true; offxs = (int) n.getX() - e.getX(); offys = (int) n.getY() - e.getY(); }

Mouse Dragging public void mouseDragged(MouseEvent e) { if (dragging) { n.setLocation((int) (e.getX() + offxs), e.getY() + offys); repaint(); }

Moue Released Stop dragging

public void mouseReleased(MouseEvent e) { if (dragging) { dragging = false; n = null; repaint(); }

Main Application public static void main(String[] args) { parseArgs(args); Lab3Application pa = new Lab3Application("LAB3"); }

Arguments What we will do with the arguments here is to set the number of rectangles Therefore the number of arguments we will need is really one and it is an int But for main we have to make the input an array of strings –parseArgs is to get from the String Array to what we rea,,y need

public static void parseArgs(String[] args) { if (args.length == 1) { nbrRectangles = Integer.parseInt(args[0]); }

The Application is a kind of Frame public class Lab3Application extends JFrame { private final static int HEIGHT = 300; private final static int WIDTH = 400;

Data The Size of the Frame The number of Rectangles The RectanglePile itself

Constructor It will take the title as a parameter Make the pile: –Make a pile object –Call a method that will create the pile Make a View and add it as the Content Pane And it will set a few attributes of the frame

public Lab3Application(String title) { super(title); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE); d = new ArrayList(); createInitialPile(); View pv = new View(d); this.getContentPane().add(pv); this.setSize(HEIGHT, WIDTH); this.setVisible(true); }

Creating the Pile private void createInitialPile() { for (int i = 0; i < nbrRectangles; i++) { d.add(new Rect(20, 20)); }