Java Graphics.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Graphics Programming UQC117S2 Semester /4. Session 3 1 Drawing in Java 2D Graphics API.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Web Design & Development Lecture 18. Java Graphics.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Java Graphics Swing Graphics
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
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.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real support of text boxes, buttons, etc. Procedural - not OOP –No.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Creating Your Own Widgets CompSci 230 S Software Construction.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Java Dynamic Graphics.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java Graphics Chris North cs3724: HCI. Presentations peter hou Vote: UI Hall of Fame/Shame?
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Introducing, the JFrame Gives us a work area beside System.out.println.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Eleventh Graphics in Java.
Java Applets.
Object-Orientated Analysis, Design and Programming
Creating Your Own Widgets
Java Graphics CS 2511.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
Java Applets.
Lecture 4: Standard Java Graphics
Presentation transcript:

Java Graphics

Swing Graphics Empty Swing containers have no visual appearance except for a background color Every JComponent must have a paintComponent method that is called when the component is first made visible or needs to be redrawn for some reason The JPanel component is a lightweight container, making it suitable as a drawing area A common way to do graphics is to extend the JPanel class and override the paintComponent method

The paintComponent Method Called by the JVM when this component needs to be redrawn A single argument, the component's graphics context (class: Graphics), is passed when paintComponent is called A Graphics object contains: the component on which it draws the current color and font location origin clipping information and more

Graphics vs. Graphics2D The Graphics class has limitations: Cannot use real number coordinates Cannot draw dotted, dashed, or variable-width lines Cannot easily draw complex curves or fill complex shapes Cannot use textures or gradient colors to fill shapes The newer Graphics2D class extends Graphics and provides these capabilities All GUI components use a Graphics2D object but paintComponent passes a Graphics object for backward compatibility

General Approach public class MyPanel extends JPanel { // instance variables public MyPanel() { // public constructor } // public methods // private helper methods public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; // drawing messages sent to g2d ...

The paintComponent Method super.paintComponent(g) is called first to ensure that painting responsibilities defined in JPanel are carried out You should not call paintComponent directly; it is called by the JVM when it needs to You can indirectly call paintComponent on a component by using component.repaint()

Some Basic Graphics Methods void setColor(Color color) void setFont(Font font) void drawString(String text, int x, int y) (x,y)is the coordinate of the lower left corner of the drawn string's leftmost character

Graphics Example import javax.swing.*; import java.awt.*; public class GraphicsPanel extends JPanel { public GraphicsPanel() { setPreferredSize(new Dimension(200,200)); setBackground(Color.magenta); // panel color } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; g2D.setColor(Color.blue); // drawing color g2D.setFont(new Font("Helvetica", Font.BOLD, 24)); g2D.drawString("Hello World", 25, 25);

Graphics Example (cont'd) import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainFrame extends JFrame { public MainFrame() { setSize(new Dimension(500,300)); setLocation(100,100); addWindowListener(new WindowAdapter () { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); getContentPane().setLayout( new FlowLayout(FlowLayout.CENTER)); GraphicsPanel gp = new GraphicsPanel(); getContentPane().add(gp); setVisible(true); public static void main(String[] args) { new MainFrame();

Display

Notes On The Example GraphicsPanel extends JPanel so that the paintComponent method can be overridden If you forget to call super's paintComponent method, you can get pixels from another desktop frame as background garbage The background color is associated with the panel; the paint color with the Graphics2D object The MainFrame class extends JFrame and an instance of it is created in the main method

Drawing Shapes You can draw any object that implements the java.awt.Shape interface. Example: suppose g2D is a Graphics2D object: Shape s = ...; g2D.draw(s); The Java library supplies a number of classes that implement the Shape interface type.