Computer Science 210: Data Structures. Intro to Java Graphics

Similar documents
L4,5: Java Overview II

Graphical User Interfaces 2

G51PRG: Introduction to Programming Second semester Applets and graphics

Introduction to the JAVA UI classes Advanced HCI IAT351

Lecture 3: Java Graphics & Events

User interfaces and Swing

Windows and Events. created originally by Brian Bailey

Graphical User Interfaces 2

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Lecture 5: Java Graphics

Graphical User Interfaces 2

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Graphic User Interfaces. - GUI concepts - Swing - AWT

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Programming Languages and Techniques (CIS120)

Chapter 14: Applets and More

IT101. Graphical User Interface

Chapter 12 Advanced GUIs and Graphics

Graphical User Interface (GUI)

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

CS2110. GUIS: Listening to Events

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Packages: Putting Classes Together

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Programming Languages and Techniques (CIS120)

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

CS 251 Intermediate Programming GUIs: Components and Layout

Graphical User Interface (GUI)

H212 Introduction to Software Systems Honors

(Incomplete) History of GUIs

2IS45 Programming

Summary Chapter 25 GUI Components: Part 2

GUI in Java TalentHome Solutions

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Chapter 1 GUI Applications

Swing. By Iqtidar Ali

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphics and Painting

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Lecture 18 Java Graphics and GUIs

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Unit 7: Event driven programming

AP CS Unit 12: Drawing and Mouse Events

J.73 J.74 THE I/O PACKAGE. Java I/O is defined in terms of streams. Streams are ordered sequences of data that have a source and a destination.

CSE 331 Software Design & Implementation

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Handling Mouse and Keyboard Events

javax.swing Swing Timer

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Graphical User Interfaces (GUIs)

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Part 3: Graphical User Interface (GUI) & Java Applets

Object-oriented programming in Java (2)

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Programming Language Concepts: Lecture 8

Advanced Java Programming

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

UCLA PIC 20A Java Programming

Graphical User Interface Programming

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Programming Languages and Techniques (CIS120)

Lecture 9. Lecture

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Java IDE Programming-I

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Programmierpraktikum

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

GUI Event Handlers (Part II)

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Programming Languages and Techniques (CIS120)

DM503 Programming B. Peter Schneider-Kamp.

Swing from A to Z Using Focus in Swing, Part 2. Preface

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

CSIS 10A Practice Final Exam Solutions

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Graphics User Defined Forms, Part I

Swing from A to Z Some Simple Components. Preface

TYPES OF INTERACTORS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

FirstSwingFrame.java Page 1 of 1


Transcription:

Computer Science 210: Data Structures Intro to Java Graphics

Summary Today GUIs in Java using Swing in-class: a Scribbler program READING: browse Java online Docs, Swing tutorials

GUIs in Java Java comes with two libraries for creating GUIs Swing and Awt Swing is implemented on top of AWT All your programs (that have a GUI) will start by importing Swing and Awt classes import javax.swing.* import java.awt.* Swing/Awt provide definition of standard classes used in GUIs panels, labels, frames, buttons, scroll bars, text labels etc all classes in Swing start with J JButton, JComboBox,JDesktopIcon,JSeparator,JSlider,JScrollPane,JLabel, JProgressBar, JTable etc called components

GUIs in Java Components JButton, JComboBox, JDesktopIcon, JSeparator, JSlider, JScrollPane, JLabel, JProgressBar, JTable etc Components are organized in a hierarchy at the top level, a component that handles windows top-level containers: JFrame, JDialog, JApplet we ll use JFrame the window may contain panels that contain buttons and labels and so on components that are not top-level containers must be attached to some other component

Example import javax.swing.*; import java.awt.*; //a class that handles a window public class MyClass extends JFrame { // instance variables... public MyClass() { super("my window"); setsize(400, 400);! //exit when closing the window! setdefaultcloseoperation(jframe.exit_on_close); } setvisible(true); };

Handling the mouse Mouse events are sent to the objects that registered to listen to the mouse The mouse events are sent through a set of methods with predefined names To handle the mouse 1. the class must implement one or both of these interfaces MouseMotionListener MouseListener 2. the object must register itself as a mouse listener the mouse events will be sent to all objects that are registered as listeners by calling the methods defined by the interface Note: if the registration is in the constructor of the class, then every instance of the class will listen to the mouse In general mouse events --> register as a mouse listener, etc timer events --> register as a time listener for each type of event, there exists a corresp method to register as listener

import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; //a class that handles the mouse public class MyclassWithMouse extends JFrame implements MouseInputListener { public MyclassWithMouse() { super("my window"); setsize(400, 400); //exit on close! setdefaultcloseoperation(jframe.exit_on_close); setvisible(true);! addmousemotionlistener(this);! addmouselistener(this); } }; public void mousepressed(mouseevent e) {} public void mousedragged(mouseevent e) {} public void mousereleased(mouseevent e) {} public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousemoved(mouseevent e) {}

Drawing in a window To draw you need a canvas Graphics g ; Need to grab the canvas of the JFrame Graphics g = this.getgraphics(); Methods supported by class Graphics drawline(point p1, Point p2) drawimage(..) drawoval.. drawpolygon.. drawrect.. getcolor, setcolor.. getfont, setfont.. Java coordinate system: (0,0) upper left corner

In-class work Test mouse functionality write code in the various mouse methods and check when they get called Develop a program that lets the user scribble on the window record the mouse clicks when pressing the mouse you want to start drawing; if you keep the mouse pressed and drag it around, you want the movement to be shown on screen, until the mouse is released. in addition to the skeleton above, you need some instance variables to record position you can use integers, or class Point provided by Java

The painting mechanism in Swing Problem: render/paint the right things at the right time Swing: any component has (inherits) a method called paint public void paint(graphics g) the component should place the rendering code inside paint() paint() is invoked every time itʼs time to paint A call to paint() can be triggered: by the system the component is made visible the component is resized the component needs to be repaired (i.e. some other window that was previously obscuring this component has moved away) by the the application when the program decides it needs to re-paint the component When the system invokes paint() on a component, it pre-configures a Graphics object with the current Graphics context and passes it as argument to paint()

The painting mechanism in Swing Programs should place the rendering code inside paint() override paint() Programs should avoid placing rendering code at any point where it might be invoked outside paint Why? Because such code may be invoked at times when it is not appropriate to paint -- for instance, before the component is visible or has access to a valid Graphics object. Programs should NOT invoke paint() directly. Instead, use public void repaint() In fact, Swing components should override public void paintcomponent(graphics g) Paint mechanism is complicated Weʼll keep GUIs simple GUIs are a tool for the class, not the focus

Here is an example of a paint() method which renders a filled circle in the bounds of a component: public void paint(graphics g) { //clear the screen super.paint(); // Dynamically calculate size information of the component Dimension size = getsize(); // diameter int d = Math.min(size.width, size.height); int x = (size.width - d)/2; int y = (size.height - d)/2; } // draw circle (color already set to foreground) g.filloval(x, y, d, d); g.setcolor(color.black); g.drawoval(x, y, d, d);

re-write Scribbler place all render code in paint() call repaint() when appropriate Class work