Programming graphics

Similar documents
Graphical User Interfaces. Comp 152

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

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

Chapter 12 Advanced GUIs and Graphics

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!

G51PRG: Introduction to Programming Second semester Applets and graphics

Window Interfaces Using Swing Objects

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

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

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Graphic User Interfaces. - GUI concepts - Swing - AWT

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Graphical User Interface (GUI)

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Introduction to the JAVA UI classes Advanced HCI IAT351

Window Interfaces Using Swing Objects

PIC 20A GUI with swing

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Systems Programming Graphical User Interfaces

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

Java Programming Lecture 6

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

User interfaces and Swing

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Programming Graphics (P1 2006/2007)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Chapter 6: Graphical User Interfaces

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Lecture 5: Java Graphics

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

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

Unit 7: Event driven programming

Java: Graphical User Interfaces (GUI)

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

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

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

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

CS 251 Intermediate Programming GUIs: Components and Layout

Chapter 18. Advanced graphics programming

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Contents Chapter 1 Introduction to Programming and the Java Language

Layouts and Components Exam

Packages: Putting Classes Together

Part I: Learn Common Graphics Components

Summary Chapter 25 GUI Components: Part 2

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Building Graphical User Interfaces. GUI Principles

SELF-STUDY. Glossary

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Unit 6: Graphical User Interface

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

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

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

Building Graphical User Interfaces. Overview

Chapter 17 Creating User Interfaces

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

CS111: PROGRAMMING LANGUAGE II

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

AP CS Unit 11: Graphics and Events

Sri Vidya College of Engineering & Technology

Graphical User Interface (GUI)

Lecture 18 Java Graphics and GUIs

Java Applet Basics. Life cycle of an applet:

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

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

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

JAVA. A. Zakiuddin Ahmed. M.Sc.,M.Phil M.S.(Edn), PGDCSM, Professor in Computer Science, P. Rizwan Ahmed. MCA., M.Sc.,M.A., M.Phil.

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Swing/GUI Cheat Sheet

GUI in Java TalentHome Solutions

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Agenda. Container and Component

To gain experience using GUI components and listeners.

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

Chapter 12 GUI Basics

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

Java Applets This is not a Java course! (You re supposed to know about Java.)

Chapter 2 Using Objects. Types. Number Literals. A type defines a set of values and the operations that can be carried out on the values Examples:

Graphical User Interface (GUI)

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

Lecture 3: Java Graphics & Events

CSE 331 Software Design & Implementation

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphical User Interfaces. Swing. Jose Jesus García Rueda

CSC207H: Software Design Lecture 11

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Programmierpraktikum

Swing from A to Z Some Simple Components. Preface

Transcription:

Programming graphics Need a window javax.swing.jframe Several essential steps to use (necessary plumbing ): Set the size width and height in pixels Set a title (optional), and a close operation Make it visible See EmptyFrameViewer.java (p. 59) Add javax.swing.jcomponents to window Draw shapes, colors, on these components That s all there is to it! Except for the painstaking labor, of course

Drawing rectangles for example Define class that extends JComponent Or a subclass like JPanel for additional features Implement paintcomponent method Use Graphics object passed to this method Actually a Graphics2D object since Java 1.2 Then let that object draw Rectangle objects See RectangleComponent.java (p. 61) Add the component to a frame for viewing e.g., RectangleViewer.java

java.awt.graphics2d Is a subclass of java.awt.graphics So cast is allowed; and Graphics methods inherited If don t cast, must use primitive drawing methods: e.g., drawrect(int, int, int, int), filloval(int, int, int, int), i.e., not object-oriented so lots of work to use/reuse But Graphics2D can do a lot more stuff e.g., draw(java.awt.shape) draws any Shape, including Rectangle, Ellipse2D, Polygon, fill(shape) draws and fills Shape with current color

Drawing more complex shapes Text example (p. 114-116) Car.java Acts like a Car that can draw itself Car constructor sets x and y locations Includes draw(graphics2d g2) method Lets Graphics2D object draw lines, ellipses, rectangles A class like CarComponent.java just uses it: Car mycar = new Car(x, y); mycar.draw(g2); // passes reference to graphics object Still need a view window, like CarViewer.java

C o l o r Current color applies to text, lines, and fills: g2.setcolor(color.red); g2.draw( ); // draws in red g2.setcolor(color.blue); g2.fill( ); // fills with blue Custom colors available: Can set by float values in range 0.0F to 1.0F: Color gb = new Color(0.0F, 0.7F, 1.0F); g2.setcolor(gb); Or by int values in range 0 to 255: Color bg = new Color(0, 255, 175);

Rendering text with Graphics2D Actually necessary to draw the text at a specified location on the Graphics object g.drawstring(astring, x, y) uses current rendering context (e.g., color), and current text attributes (e.g., font) Font: a face name, a style, and a point size Font f = new Font( Serif, Font.BOLD, 24); g2.setfont(f); // sets font for g2

Applets an alternate approach A way to run a program but not an application No main method necessary Need a subclass of Applet (or JApplet) So: class extends Applet (or extends JApplet) Most web browsers know how to create a new applet, and how to use certain Applet methods So, applets must be embedded in an html page And, to be useful, they must include at least one of the methods the browser invokes (e.g., paint)

Running an Applet The applet is started by the web browser as soon as the web page (html file) is visited The html file (stands for hypertext markup language) must have an applet tag in it: <applet code=appletclassname.class width=### height=###> </applet> <!-- needs a closing tag too --> The browser works in a certain order: Creates a new applet object includes a window, a Graphics object, lots of class Applet methods Invokes (1) init once, (2) start first & return visits, (3) paint first & every need to paint (also stop, destroy)

Implementing a simple applet import javax.swing.japplet; // mandatory Also usually Graphics and Graphics2D and others Declare a class that extends JApplet: public class RectangleApplet extends JApplet Implement paint method (at least) Same procedures as paintcomponent for components Create an html file to load the applet in a web browser or the appletviewer (provided with JDK) See RectangleApplet.java (p. 63) and related html files (p. 64)

Images Images are not drawings You don t draw them, you show them But Graphics method is called drawimage anyway Image is an abstract class Generally, create instance by loading from file or URL Can also create/edit by classes in java.awt.image Applets know how to get images (ImageApplet) Applications use Toolkit to get (demo) Toolkit.getDefaultToolkit().getImage("javacup.gif");

Events Signals from the outside Usually user initiates: Mouse click, button push, menu-select, enter text, Not always: TimerTester.java Each event has a type a class in java.awt.event So each event is an object Created almost constantly (by Java window manager)

Event sources Technically: can be any external signal Including from a different program or system e.g., a client applet contacting a web server In a GUI, focus is on user signals Java Components generate the events Each component generates different event types, e.g.: Panels (inc. Applets) MouseEvents Textfields and buttons ActionEvents Windows WindowEvents Keyboard KeyEvents Components inform a list of Listeners about events

Listeners Objects that components tell about events Must have methods a component can invoke i.e., implements one of the Listener interfaces Must be added to a component s listener list Different listeners apply to different messages ActionListener actionperformed e.g., ClickListener (with ButtonTester.java) MouseListener mouseclicked, mouseentered, e.g., MouseSpy demo (from 1 st edition of textbook) Note: also can extend Adapter class instead of implement listeners directly saves busywork

Inner class listeners Can access private instance variables of outer class Stores implicit reference to outer class object So can often handle events more easily e.g., RectangleComponentViewer.java Notice they can access final local variables too Another example: TimerTester2.java Must be final so no ambiguity about value For example, no opportunity for variable to go out of scope while object exists

Laying out GUI components Depends on layout manager Defaults: JFrame: BorderLayout JPanel: FlowLayout Others: GridLayout GridBagLayout Can set new, and even make custom

Choosing a layout manager e.g., a grid layout for calculator buttons: panel.setlayout( new GridLayout(4,3) ); panel.add(button7); panel.add(button8); panel.add(button9); panel.add(button4);... e.g., CS10Display.java

Text components JLabel not for user input, just display JTextField for one row of text new JTextField(), or (int columns) gettext(), settext(string), setfont(font), seteditable(boolean), (mostly inherited) ActionEvent on <enter> JTextArea for multiple rows of text new JTextArea(), or (int rows, int columns) Same methods inherited from JTextComponent Generates no events, so usually use with a button

Choices Choice objects generate ActionEvents Handlers for JCheckBox and JRadioButton: use boolean method isselected() Note: put radio buttons in a ButtonGroup so just one can be selected at a time For same reason should visually group them in a panel with a border, like EtchedBorder For JComboBox: use getselecteditem() Note: returns Object usually cast to String e.g., ChoiceFrame.java (see FontViewer.java, pp. 794-798)

Menus Steps to implement swing menus: 1. Add a menu bar to a JFrame JMenuBar bar = new JMenuBar(); setjmenubar(bar); 2. Add menus to the menu bar JMenu filemenu = new JMenu( File ); bar.add(filemenu); 3. Add menu items to the menus, & listeners to items JMenuItem openitem = new JMenuItem( Open ); filemenu.add(openitem); openitem.addactionlistener(listener); e.g., MenuFrame.java (see FontViewer2.java, pp. 803-7)

Sliders, and more swinging Note - good text section 18.4, pp. 808-814: should read while browsing API on web About how to discover swing features/usage Focuses on JSlider generates ChangeEvents, so addchangelistener(listener), where listener implements statechanged method Requires javax.swing.event for change events e.g., SliderFrame.java (see ColorViewerFrame.java, pp. 812-814) Explore swing as needed, or even just for fun Buy a book, or look at API classes starting with J Or just run the SwingSet demo from the JDK