Creating Your Own Widgets

Slides:



Advertisements
Similar presentations
Custom Painting Gestione della Grafica customizzata Vedi anche:
Advertisements

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.
Web Design & Development Lecture 19. Java Graphics 2.
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.
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
UID – Swing and Graphics Boriana Koleva
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.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
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.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
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.
Web Design & Development Lecture 18. Java Graphics.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Nov 061 Size Control How is a component’s size determined during layout and during resize operations? Three factors determine component sizes: The component’s.
(c) University of Washington07-1 CSC 143 Introduction to Graphical Interfaces in Java: AWT and Swing Reading: Ch. 17.
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.
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.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
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.
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)
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Deliver Information, not Just Data Design displays carefully –Visual order and user focus (control contrast) –Match the medium (mixed metaphors) –Attention.
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.
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
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.
Object-Oriented Software Engineering Using Threads and simple Animation.
Java Swing One of the most important features of Java is its ability to draw graphics.
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?
Graphics in Java Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
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
Advanced AWT The Rendering Pipeline
Java Graphics.
Java Graphics CS 2511.
GUI AND GRAPHICS.
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.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
עקרונות תכנות מונחה עצמים
Introduction to Graphical Interfaces in Java: AWT and Swing
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

Creating Your Own Widgets CompSci 230 S1 2016 Software Construction Creating Your Own Widgets

Overview When writing applications, you typically use the standard widgets provided. On occasion, you will need to create your own custom widgets. There are several reasons that you might want to do this: To add a new type of widget not provided by the standard widgets To extend the functionality of an existing widget Custom widgets are created by subclassing in the existing widget class hierarchy. 14

How Swing Widgets are Painted All Swing widgets inherit from JComponent JComponent defines paint(Graphics g) paint() called by the system whenever drawing is necessary paint() calls other methods of JComponent paintComponent() paints the widget (override this!) paintBorder() paints a border the widget may have paintChildren() paints the children of the widget, if it is a container (don't override this!) You never call paint() directly Instead invalidate the widget region by calling repaint() repaint() asynchronously calls paint() (through windowing system) 14

Creating a Custom Widget Steps: Create new class that extends JPanel Override paintComponent(Graphics g) with custom drawing code Make sure to honor the width and height of the widget Possibly call super.paint(g) to draw the superclass widget (e.g. unicolored background) Override getPreferredSize() to return the right preferred size for your widget class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello World!",10,20); } public Dimension getPreferredSize() { return new Dimension(100,50); 14

Drawing with Java Always draw in a graphics context (Graphics / Graphics2D): Uniform way to draw on different devices, like a universal canvas Properties of the current pen used for drawing color, background, font stroke, i.e. pen size and shape paint, i.e. a color pattern to use composite type, i.e. how it looks when shapes are drawn onto existing shapes (e.g. blending them together) clipping rectangle to limit painting area Drawing methods, e.g. draw(Shape), fill(Shape), drawString(), drawImage() Transformation methods to apply to the drawing operations, e.g. scale(), rotate(), translate() 14

Example: RoundedButton Extends Jbutton Override the paintComponent() method public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Switch on anti-aliasing, which looks better g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setColor(getBackground()); g2.fill(new Rectangle2D.Float(0, 0, getWidth(), getHeight())); g2.setColor(new Color(110, 120, 210)); g2.fill(new RoundRectangle2D.Float(0, 0, getWidth(), getHeight(), 50, 50)); g2.setColor(new Color(120, 130, 255)); g2.setStroke(new BasicStroke(5)); g2.draw(new RoundRectangle2D.Float(2, 2, getWidth() - 4, getHeight() - 4, 50, 50)); g2.setStroke(new BasicStroke(1)); FontMetrics metrics = g2.getFontMetrics(getFont()); int h = metrics.getAscent(); int w = metrics.stringWidth(getText()); g2.setColor(getForeground()); g2.drawString(getText(),(getWidth() - w) / 2, (getHeight() + h) / 2); } 14

Summary Drawing can be performed using graphics objects A graphics context (Graphics2D) Strokes, Fonts, Colors... Shape objects that can be drawn or filled Custom components can be created by overriding the method paintComponent(Graphics g) of a widget 14