CS12420 - Lecture 03 Drawing & Displaying Images Lynda Thomas

Slides:



Advertisements
Similar presentations
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Advertisements

Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 14 Graphics.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Graphics.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Simple Applet.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
XHTML Images. Images are important Purpose: to enhance your web site. Add only when they complement or add additional impact to your message.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
9/21/99www.cs.vt.edu/wwtut/1 User Interface Programming In Java Part 1 – Introduction Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
(c) University of Washington07-1 CSC 143 Introduction to Graphical Interfaces in Java: AWT and Swing Reading: Ch. 17.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
2D Graphics Basics Chapter 2. Bird’s Eye View  Overview of Computer Graphics  2D Graphics: Basics –Basic 2D graphics, rendering pipeline, geometry,
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.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
25/2/00SEM107 - © Kamin & ReddyClass 9 - HTML - 1 Class 9 - HTML r Creating a personal web page r Structure of HTML documents r HTML tags r The APPLET.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Image File Formats Which one is right for me?. The Only Three Image Formats Your Will Ever Need: Names Jpg “Joint Photographic Experts Group” Png “Portable.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
User Interface Programming In Java
Building Java Programs
Building Java Programs
Building Java Programs
Today’s topics Java Applications Upcoming Reading Graphics
CMSC 202 Swing.
עקרונות תכנות מונחה עצמים
Building Java Programs
Introduction to Graphical Interfaces in Java: AWT and Swing
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

CS Lecture 03 Drawing & Displaying Images Lynda Thomas

Drawing Use a panel as a canvas JPanel.paintComponent(Graphics g) Override (but first call super) to add own commands like g.drawLine(10,10,100,100); Never called directly, instead call repaint()

paintComponent method public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(10,10,100,100); //add own commands here } Just like with actionPerformed – this parameter is supplied If you need to call it (see later) you call repaint()

Other Graphics methods drawLine(int xStart,int yStart,int xEnd,int yEnd) drawRect(int xLeft,int yTop,int width,int height) drawOval(int xLeft,int yTop,int width,int height) drawString(String text,int xLeft,int yBottom) fillRect(int xLeft,int yTop,int width,int height) fillOval(int xLeft,int yTop,int width,int height) setColor(Color col) drawPolygon(int[] xp,int[] yp,int n)

public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.black); g.drawLine(10,10,100,100); g.setColor(Color.red); g.drawLine(10,100,100,10); g.setColor(Color.green); g.drawOval(120,60,70,40); g.setColor(Color.black); g.fillOval(238,160,30,30); g.setColor(Color.cyan); g.fillOval(10,120,100,60); g.setColor(this.getBackground()); g.fillOval(50,140,100,60); g.setColor(Color.blue); g.drawString(" Swing is £$~%",100,200 ); } This method is called whenever you ask it to (with repaint()) or whenever it ‘needs’ For instance when the application starts or when it is uncovered or..

Look in drawing folder at: SimpleGraphics classes

Screen Parameters int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;

Panel Parameters int panelHeight = this.getHeight(); int panelWidth = this.getWidth();

Images This is barebones – more in second year - there is now a special package called javax.imageio and other approaches like JavaFX Swing also has ImageIcon to decorate buttons etc. See images-code/ImagePanel.java for (over) simple loading and displaying of a single image ImageFormats: Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPG), Portable Network Graphics (PNG)

Multiple Images and Image manipulation see images-code/LotsOfImages classes

These examples use a MediaTracker When you first load an Image all that happens is that Java notes where it is. Does not get image, so hence doesn’t know what size!!! It isn’t until you do something with it that anything really happens. MediaTracker does the upload and waits for an ID – sort of fakes the machine into thinking image is needed NOW. You can also do all kinds of clever things with loading in groups, in order etc. Look at LyndaFrameDriver and see what happens without one (the images are there but all the clever sizing and positioning fails)

In This Lecture Drawing Images

In The Next Lecture Mouse interaction More on Listeners