Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

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.
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.
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.
Graphical User Interfaces
Made with love, by Zachary Langley Applets The Graphics Presentation.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
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.
Page w16.1 – Spring 2010Steffen Vissing Andersen SDJ I1 subjects, Spring 2010 Agenda – Week 16, 2010 GUI.
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.
Graphical User Interfaces Java’s AWT and Swing APIs.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Drawing in a frame – Java GUI
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.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
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.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
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.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
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.
Övning 5. Repetition klasser class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
Event Handling Events and Listeners Timers and Animation.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
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.
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.
– 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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Alice in Action with Java Chapter 14 Events and GUIs.
Layout Managers Arranges and lays out the GUI components on a container.
EVENTS Wiring together objects, code, and actions.
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.
Review_6 AWT, Swing, ActionListener, and Graphics.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
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.
CompSci 230 S Programming Techniques
Java Swing.
Graphical User Interface (pronounced "gooey")
Presentation transcript:

Bar Graph Design

Left-side/Right-side mechanical processing creative, abstract reasoning

Why Is He Doing This?

The Problem: Two frames (Views) and a data structure (Model). Observer Pattern:

Observer Pattern Times Four: JTextField: ActionListener receives event after you hit. The ActionListener updates the Data (Model) object. JTextField anonymous object ActionListener addActionListener actionPerformed

Observer Pattern Times Four: Data: Is observed by the BarGraph (ChangeListener) for any changes to the model. Data BarGraph ChangeListener attach stateChanged

Observer Pattern Times Four: BarGraph: MouseListener receives event when you click on the bar graph. mousePressed() moves the bar and updates Data object. BarGraph Mouse Adapter MouseListener addMouseListener mousePressed

Observer Pattern Times Four: Data: Is observed by TextFrame (ChangeListener) for any changes to data. Data TextFrame ChangeListener attach stateChanged

Class Diagram:

Sequence Diagram Enter new number

Sequence Diagram: Click on BarGraph:

Data.java public class Data { public Data(ArrayList d) { data = d; listeners = new ArrayList (); } public ArrayList getData() { return (ArrayList ) (data.clone()); } public void attach(ChangeListener c) { listeners.add(c); }

Data.java public class Data { public void update(int loc, double val) { if (val > MAX) val = MAX; if (val < 0) val = 0; data.set(loc, new Double(val)); // for each listener, let the listener know a change has occurred for (ChangeListener l : listeners) { l.stateChanged(new ChangeEvent(this)); } public Double get(int i) { return data.get(i); } private ArrayList data; private ArrayList listeners; public static final double MAX = 100.0; }

TextFrame: public class TextFrame extends JFrame implements ChangeListener { public TextFrame (Data md) { d = md; ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent e) { JTextField c = (JTextField) e.getSource(); int ndx = findBox(c); if (ndx >= 0) d.update(ndx,Double.parseDouble(c.getText().trim())); } }; JPanel tPanel = new JPanel(); tPanel.setLayout(new BoxLayout(tPanel,BoxLayout.PAGE_AXIS)); tf = new JTextField[5]; for (int i = 0; i < 5; i++) { tf[i] = new JTextField(10); tf[i].addActionListener(l); tPanel.add(tf[i]); } add(tPanel, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); }... }

public void stateChanged(ChangeEvent e) { // how I notify the text frame if the bar graph changes for (int i = 0; i < 5; i++){ double d1 = d.get(i).doubleValue(); String s = tf[i].getText().trim(); double d2; try { d2 = Double.parseDouble(s); } catch(NumberFormatException nfe ) { d2 = 0.0; } if (Math.abs(d1 - d2) > 0.01) tf[i].setText(new Double(d1).toString()); } public int findBox(JTextField t) { int i = 0; while (i < 5) { if (t == tf[i]) return i; i++; } return -1; } private Data d; private JtextField[ ] tf; }

BarGraph: public class BarGraph extends JFrame implements ChangeListener { public BarGraph(Data md) { d = md; setLocation(0,300); Icon barIcon = new Icon() { public int getIconWidth() { return WIDTH; } public int getIconHeight() { return HEIGHT; } public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g; Color cl = g2.getColor(); for (int i = 0; i < 5; i++) { Rectangle2D.Double rectangle = new Rectangle2D.Double(0, (HEIGHT/6)*i, WIDTH*(d.get(i).doubleValue()/Data.MAX), (HEIGHT/6)); g2.setPaint(Color.cyan); g2.fill(rectangle); g2.setColor(Color.black); g2.draw(rectangle); } Rectangle2D.Double rectangle = new Rectangle2D.Double(0,375,WIDTH,(HEIGHT/6)); g2.setPaint(cl); g2.fill(rectangle);g2.setColor(cl);g2.draw(rectangle); } }; // end of barIcon panel... }

BarGraph: public class BarGraph extends JFrame implements ChangeListener { public BarGraph(Data md) {... add(new JLabel(barIcon)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MouseListener m = new MouseAdapter() { public void mousePressed(MouseEvent e){ Point2D p = e.getPoint(); double dy = p.getY(); int barToMove = (int)(dy / BARHEIGHT); if (barToMove 4) return; double dx = p.getX(); double newV = (dx/WIDTH)* Data.MAX; d.update(barToMove, newV); } }; addMouseListener(m); pack(); setVisible(true); } // end constructor } // end class