Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

Cosc 5/4730 Blackberry Drawing. Screen size With Blackberry devices, they have a known screen size in pixels. If you are programming for specific device.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
Draw Shapes Introduction to simple graphics. What is a graphics context? An instance of the Graphics class Graphics is ABSTRACT! You can extend Graphics.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Georgia Institute of Technology Drawing in Java part 1 Barb Ericson Georgia Institute of Technology September 2006.
Vector Graphics Making custom images. Raster vs. Vector Graphics In computer graphics, a raster graphics image, or bitmap, is a dot matrix data structure.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Drawing-Turtle-and-AWT1 Drawing in Java Barb Ericson Georgia Institute of Technology Sept 21, 2009.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
General Purpose Packages GRAPHICS Chapter 5. General Purpose Packages Features of Graphics Packages Entering text Entering text Common tools Common tools.
Creating Your Own Widgets CompSci 230 S Software Construction.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
Aim: What is Adobe Illustrator?. Adobe Illustrator is a vector based drawing program.
Georgia Institute of Technology Drawing in Java Barb Ericson Georgia Institute of Technology August 2005.
Paint Application in Java Clark Jayson Cacal Aveline Germar Jose Sison Christopher Valera.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
Java Graphics Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zOffset between class.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
CSC1401 Drawing in Java - 1. Goals Understand at a conceptual and practical level How to use the Turtle class to draw on a picture How to use the java.awt.Graphics.
Advanced Java Screen Update Techniques SD’98 - Session 4406 Ted Faison Faison Computing Inc.
Georgia Institute of Technology Drawing in Java – part 1 Barb Ericson Georgia Institute of Technology September 2005.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Barb Ericson Georgia Institute of Technology September 2005
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
MS Paint A simple drawing tool that can be used to create simple or elaborate drawings. These drawings can be either black-and-white or color, and can.
Java Graphics CS 2511.
Georgia Institute of Technology
Building Java Programs
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology September 2005
Building Java Programs
Georgia Institute of Technology
Basic Graphics Drawing Shapes 1.
Workshop for Programming And Systems Management Teachers
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
Media Computation Questions
Building Java Programs
Two-Dimensional Arrays and Nested Loops – part 5
Chapter Lessons Use the Macromedia Flash drawing tools
Barb Ericson Georgia Institute of Technology April 2006
Workshop for Programming And Systems Management Teachers
Chapter 49 Java 2D.
Building Java Programs
Handout-14 Applets and Graphics
Building Java Programs
Graphics Reading: Supplement 3G
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level –How to draw more simple shapes with java.awt.Graphics Arcs Rectangles –Bitmapped pictures versus vector graphics –How to use a java.awt.Graphics2D class How to convert the Graphics class to it How to set the color How to set the paint How to set the stroke (paint brush) How to create the object to be painted How to paint the object

Georgia Institute of Technology Drawing Arcs Arcs –Outlined Arc g.drawArc(topLeftX, topLeftY, width, height, startAngle, arcAngle); –Filled Arc g.fillArc((topLeftX, topLeftY, width, height, startAngle, arcAngle);

Georgia Institute of Technology Drawing Rectangles Rectangle –Outlined Rectangle g.drawRect(topLeftX, topLeftY, width, height); –Filled Rectangle g.fillRect(topLeftX,topLeftY,width,height); –Outlined Rounded Rectangle g.drawRoundRect(topLeftX,topLeftY,width,height,arcWidth,ar cHeight); –Filled Rounded Rectangle g.fillRoundRect(topLeftX,topLeftY,width,height,arcWidth,arcH eight);

Georgia Institute of Technology Drawing on a Blank Picture You can make pictures from the “blank” files –They will have all white pixels –640x480.jpg –7inX95in.jpg You can also create a “blank” picture with a width and height –They will also have all white pixels –Picture blankPicture = new Picture(width,height);

Georgia Institute of Technology Draw a Picture Exercise Create a method that will draw a simple picture –Use at least one rectangle –Use at least one polygon –Use at least one oval –Use at least one arc

Georgia Institute of Technology Bitmapped Versus Vector Graphics We have been doing bitmapped graphics –Specifying the color of each pixel in the picture We just wrote a method that drew a simple picture –Which is smaller the program or the picture? Some applications use vector graphics which are programs that produce the picture –Used in Postscript, Flash, and AutoCAD –Advantages: smaller, easy to change, can be scaled

Georgia Institute of Technology Precision Drawings How would you draw a stack of filled rectangles starting from the lightest one at the bottom right and the darkest one at the top left. –With 10 pixels between each –Not easy with drawing packages

Georgia Institute of Technology Draw Filled Rectangles Method public void drawFilledRectangles() { Graphics g = this.getGraphics(); Color color = null; // loop 25 times for (int i = 25; i > 0; i--) { color = new Color(i * 10, i * 5, i); g.setColor(color); g.fillRect(0,0,i*10,i*10); }

Georgia Institute of Technology Java 2D Graphics – java.awt Newer drawing classes –More object-oriented –Instead of drawOval() or fillOval() you create a Ellipse2D object and ask a 2d graphics object to draw or fill it –Geometric shapes are in the java.awt.geom package Advanced Drawing –Support for different types of brushes Line thickness, dashed lines, etc –Supports cubic curves and general paths –Drawing of gradients and textures –Move, rotate, scale and shear text and graphics –Create composite images

Georgia Institute of Technology How To Use Java 2D Cast the Graphics class to Graphics2D –Graphics2D g2 = (Graphics2D) gObj; Set up the stroke if desired (type of pen) –g2.setStroke(new BasicStroke(widthAsFloat)); Set up a Color, GradientPaint, or TexturePaint –g2.setPaint(Color.blue); –g2.setPaint(blueToPurpleGradient); –g2.setPaint(texture); Create a geometric shape –Line2D line2D = new Line2D.Double(0.0,0.0,100.0,100.0); Draw the outline of a geometric shape –g2.draw(line2d); Fill a geometric shape –g2.fill(rectangle2d);

Georgia Institute of Technology Graphics2D inherits from Graphics Inherits basic drawing ability from Graphics Adds more advanced drawing ability Graphics Graphics2D

Georgia Institute of Technology Drawing Lines Exercise Create a new method (drawWideX) for adding two wide crossed lines to a picture –Using a passed color –Using a passed line width Set up the stroke to make the lines thicker –g2.setStroke(new BasicStroke(width)); –Draw the lines You can use redMotorcycle.jpg to test.

public void drawWideX(Color color, float width) { // get the Graphics2D object Graphics graphics = this.getGraphics(); Graphics2D g2 = (Graphics2D) graphics; // set the color and brush width g2.setPaint(color); g2.setStroke(new BasicStroke(width)); // get the max x and y values int maxX = getWidth() - 1; int maxY = getHeight() - 1; // draw the lines g2.draw(new Line2D.Double(0,0,maxX,maxY)); g2.draw(new Line2D.Double(0,maxY,maxX,0));}

Georgia Institute of Technology Using a Graphics Object to Copy an Image public void copy(Picture source, int x, int y) { // get the graphics object Graphics g = this.getGraphics(); // copy the image g.drawImage(source.getImage(),x,y,null); }

Georgia Institute of Technology Using Graphics2D to Copy an Image public void copy2D(Picture source, int x, int y) { // get the graphics object Graphics g = this.getGraphics(); Graphics g2 = (Graphics2D) g; // copy the image g2.drawImage(source.getImage(),x,y,null); }

Georgia Institute of Technology Testing the Copy Method Picture p1 = new Picture(FileChooser.getMediaPath("beach.jpg")); Picture p2 = new Picture(FileChooser.getMediaPath("turtle.jpg")); p1.copy2D(p2,194,304); p1.show();

Georgia Institute of Technology Summary You can use the original Graphics class for simple 2d drawing –Lines, rectangles, ovals, polygons, strings, images You can use the Graphics2D class for more advanced drawing –Width of paint brush, paint geometric objects Graphics2D inherits from Graphics –Means it can do the same methods –But also adds others