2D Graphics. Shape Models, Drawing, Selection using Java. CS d Graphics 1

Similar documents
public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

Using A Frame for Output

Java Mouse and Keyboard Methods

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Event-Driven Programming

core 2 Handling Mouse and Keyboard Events

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

CS 335 Lecture 06 Java Programming GUI and Swing

Construction of classes with classes

DIA Creating Charts and Diagrams

The following four software tools are needed to learn to program in Java:

Event processing in Java: what happens when you click?

Remote Method Invocation

Providing Information (Accessors)

WPF Shapes. WPF Shapes, Canvas, Dialogs 1

Working With Animation: Introduction to Flash

Extending Desktop Applications to the Web

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

GUI Components: Part 2

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Animazione in Java. Animazione in Java. Problemi che possono nascere, e loro soluzione. Marco Ronchetti Lezione 1

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

There are some important differences between an applet and a standalone Java application, including the following:

QUICK REFERENCE: ADOBE ILLUSTRATOR CS2 AND CS3 SECTION 1: CS3 TOOL BOX: PAGE 2 SECTION 2: CS2 TOOL BOX: PAGE 11

11. Applets, normal window applications, packaging and sharing your work

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

How to Convert an Application into an Applet.

CPIT-285 Computer Graphics

Interactive Programs and Graphics in Java

Tutorial Reference Manual. Java WireFusion 4.1

Programming with Java GUI components

ART 170: Web Design 1

Mouse Event Handling (cont.)

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

MiniDraw Introducing a framework... and a few patterns

Adobe Illustrator CS5

Canterbury Maps Quick Start - Drawing and Printing Tools

Click on various options: Publications by Wizard Publications by Design Blank Publication

Graphical User Interfaces

The Virtual Pest as a Java Applet Shannon McGinnis Tosolt, Nov. 2002

Understand the Sketcher workbench of CATIA V5.

What s New V 11. Preferences: Parameters: Layout/ Modifications: Reverse mouse scroll wheel zoom direction

Simple Line Drawing. Description of the Simple Line Drawing program

Swing. A Quick Tutorial on Programming Swing Applications

m ac romed ia Fi r e wo r k s Curriculum Guide

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

LAB4 Making Classes and Objects

Tutorial Creating Vector Graphics

Java is commonly used for deploying applications across a network. Compiled Java code

Graphic Design Studio Guide

Instructions for Creating a Poster for Arts and Humanities Research Day Using PowerPoint

Analysis Of Source Lines Of Code(SLOC) Metric

Homework/Program #5 Solutions

Doña Ana County, NM Map Help

History OOP languages Year Language 1967 Simula Smalltalk

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

Image Processing and Computer Graphics. Rendering Pipeline. Matthias Teschner. Computer Science Department University of Freiburg

Recipes4Success. Animate a Rocket Ship. Frames 6 - Drawing Tools

Overview of the Adobe Flash Professional CS6 workspace

Graphic Design with Gimp

A Short Introduction to Computer Graphics

Introduction to Computer Graphics

1 Hour, Closed Notes, Browser open to Java API docs is OK

Lession: 2 Animation Tool: Synfig Card or Page based Icon and Event based Time based Pencil: Synfig Studio: Getting Started: Toolbox Canvas Panels

Two-Dimensional Arrays Java Programming 2 Lesson 12

Introduction to Java Applets (Deitel chapter 3)

MovieClip, Button, Graphic, Motion Tween, Classic Motion Tween, Shape Tween, Motion Guide, Masking, Bone Tool, 3D Tool

WFP Liberia Country Office

Logo Design Studio Pro Guide

Window's Paint Tools

Software Design: Figures

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

10. THERM DRAWING TIPS

Essentials of the Java(TM) Programming Language, Part 1

TOON BOOM HARMONY Essentials Edition - Paperless Animation Guide

Fireworks CS4 Tutorial Part 1: Intro

Section 6 Spring 2013

Graphic Design. Background: The part of an artwork that appears to be farthest from the viewer, or in the distance of the scene.

Flash Tutorial Part I

GUI Development: Goals. User Interface Programming in C#: Basics and Events. C# Materials

Object Oriented Software Design

How Scala Improved Our Java

Java SE 6 Update 10. la piattaforma Java per le RIA. Corrado De Bari. Sun Microsystems Italia Spa. Software & Java Ambassador

Tutorial for Tracker and Supporting Software By David Chandler

Geocortex HTML 5 Viewer Manual

Sample Turtle Programs

Introducing Variance into the Java Programming Language DRAFT

Gerrit Stols

Lecture Notes, CEng 477

Image Processing. In this chapter: ImageObserver ColorModel ImageProducer ImageConsumer ImageFilter

Create a Poster Using Publisher

Object Oriented Software Design

Essentials of the Java Programming Language

Using C# for Graphics and GUIs Handout #2

Autodesk Fusion 360 Badge Guide: Design an F1 in Schools Trophy

3D Viewer. user's manual _2

Transcription:

2D Graphics Shape Models, Drawing, Selection using Java 1

Graphic Models vs. Images Computer Graphics: the creation, storage, and manipulation of images and their models Model: a mathematical representation of an image containing the important properties of an object (location, size, orientation, color, texture, etc.) in data structures Rendering: Using the properties of the model to create an image to display on the screen Image: the rendered model Model Rendering Image 2

Implementing Direct Manipulation Objectives: draw a shape on the screen at specified position, size, orientation perhaps draw many copies, each different from the others test when a rendered shape is selected could be a filled or outlined polygon or a polyline selections that just miss the shape should snap to shape Tasks: create a model of the shape Now: what we did in X, in Java draw it Later: 2D transformations choose a selection paradigm implement shape hit tests and/or inside tests (with snapping) respond to events 3

Example Shape Models An array of points: {P 1, P 2,, P n Can be open, closed, filled Java has primitive shapes built in: Lines, Ellipses, Rectangles Tools for building others: CubicCurves, Paths, drawpolygon, drawpolyline, fillpolygon 4

SimpleDraw: Drawing in Java package twod_graphics; import javax.swing.*; import java.awt.*; import java.awt.geom.path2d; public class SimpleDraw { public static void main(string[] args) { JFrame f = new JFrame("SimpleDraw"); // jframe is the app window f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(400, 400); // window size f.setcontentpane(new Canvas()); // add canvas to jframe f.setvisible(true); // show the window // JComponent is a base class for custom components class Canvas extends JComponent { // custom graphics drawing public void paintcomponent(graphics g) { 5

SimpleDraw: Drawing in Java package twod_graphics; import javax.swing.*; import java.awt.*; import java.awt.geom.path2d; public class SimpleDraw { // JComponent is a base class for custom components class Canvas extends JComponent { // custom graphics drawing public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D) g; g2.setstroke(new BasicStroke(32)); g2.setcolor(color.blue); g2.drawline(0, 0, getwidth(), getheight()); // draw line g2.setcolor(color.red); g2.drawline(getwidth(), 0, 0, getheight()); g2.setcolor(color.yellow); g2.setstroke(new BasicStroke(1)); g2.draw(new Star()); // cast to get 2D drawing methods // 32 pixel thick stroke // make it blue 6

SimpleDraw: Drawing in Java package twod_graphics; import javax.swing.*; import java.awt.*; import java.awt.geom.path2d; public class SimpleDraw { class Canvas extends JComponent { class Star extends Path2D.Double { public Star() { super(wind_even_odd); this.moveto(0, 0); this.lineto(-1.5, 5); this.lineto(-7, 5); this.lineto(-2.5, 8); this.lineto(-4.2, 13); this.lineto(0, 10); this.lineto(4.2, 13); this.lineto(2.5, 8); this.lineto(7, 5); this.lineto(1.5, 5); this.lineto(0, 0); How do you get the star where you want it and the size you want? 7

Selection Paradigms Click selection different for filled and outlined shapes Rubberband rectangle Lasso (see Ch 14 of text) 8

Closest Shape to Mouse Test check distance from every line segment of every shape to mouse position (can be optimized ) check distance from mouse to line segment using vector projection ClosestPointDemo.java 9 http://en.wikipedia.org/wiki/distance_from_a_point_to_a_line

Mouse Inside Filled-Shape Test Is a point inside or outside a polygon? P1 P3 P4 P2 Java Shapes have a contains(point2d p) method 10

MouseEventDemo (1/4) import javax.swing.jframe; import javax.swing.jcomponent; import java.awt.event.*; import java.awt.*; public class MouseEventDemo { public static void main(string[] args) { JFrame f = new JFrame("MouseEventDemo"); // jframe is the app window f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(400, 400); // window size f.setcontentpane(new Canvas()); // add canvas to jframe f.setvisible(true); // show the window Same as before, except for imports. 11

MouseEventDemo (2/4) class Canvas extends JComponent { 12 private Color colour = Color.GRAY; private int x; private int y; private int size = 50; Canvas() { // add listeners this.addmouselistener(new MListener()); this.addmousemotionlistener(new MMListener()); // custom graphics drawing public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D) g; g2.setcolor(colour); g2.filloval(this.x - this.size / 2, this.y - this.size / 2, this.size, this.size); private class MListener implements MouseListener { private class MMListener implements MouseMotionListener { Updated when appropriate events are received. Each listener defines a group of events that we care about. Same function as XSelectInput. What do we do when we receive an event?

MouseEventDemo (3/4) private class MMListener implements MouseMotionListener { @Override public void mousedragged(mouseevent arg0) { System.out.format("drag %d,%d\n", arg0.getx(), arg0.gety()); x = arg0.getx(); y = arg0.gety(); repaint(); A mouse motion listener is interested in two kinds of events: mouse moved and mouse dragged. @Override public void mousemoved(mouseevent arg0) { System.out.format("move %d,%d\n", arg0.getx(), arg0.gety()); x = arg0.getx(); y = arg0.gety(); repaint(); 13

MouseEventDemo (4/4) private class MListener implements MouseListener { @Override public void mouseclicked(mouseevent arg0) { System.out.format("click %d,%d count %d\n", arg0.getx(), arg0.gety(), arg0.getclickcount()); if (arg0.getclickcount() == 2) // double click Canvas.this.colour = Color.PINK; else Canvas.this.colour = Color.GREEN; Canvas.this.repaint(); @Override public void mouseentered(mouseevent arg0) { System.out.format("enter %d,%d\n", arg0.getx(), arg0.gety()); Canvas.this.colour = Color.BLACK; Canvas.this.repaint(); @Override public void mouseexited(mouseevent arg0) { @Override public void mousepressed(mouseevent arg0) { 14 @Override public void mousereleased(mouseevent arg0) { System.out.format("release %d,%d\n", arg0.getx(), arg0.gety()); colour = Color.BLUE; repaint();