Top Banner
Graphics Object Orientated Programming in Java Benjamin Kenwright
52

Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

May 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

GraphicsGraphicsObject Orientated Programming in Java

Benjamin Kenwright

Page 2: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Outline

Essential Graphical Principals

JFrame Window (Popup Windows)

Extending JFrameExtending JFrame

Drawing from paintComponent

Drawing images/text/graphics

Today’s Practical

Review/Discussion

Page 3: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Revision Question

Will the following code compile? If yes then what will be the output?

javac Bark.javajava Bark

Page 4: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Compile: Yes

Output: WoofWoof

Page 5: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

Will the following code compile? If yes then what will be the output?

Page 6: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Compile: Yes

Output: String

Page 7: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

Will the following code compile? If yes then what will be the output?

Page 8: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Compile: No

Reason: Method is ambiguous

Page 9: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

Will the following code compile? If yes then what will be the output?

Page 10: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Compile: Yes

Output: ArithmeticException

Page 11: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

Will the following code compile? If yes then what will be the output?

Page 12: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Compile: Yes

Output: null==null

Page 13: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

Will the following code compile? If yes then what will be the output?

Page 14: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Compile: Yes

Output: I’m alive

Page 15: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Graphics

How do you draw `complex’ diagrams?

Customize/control what is drawn on our windows

Animated clocks, bar-charts, images,

Page 16: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Graphical Coordinate System

Page 17: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

JFrame

Simple GUI WindowLets us perform simple drawing operations

Basic UsageInstantiate the JFrame and assign text for the title bar

• JFrame frame = new Jframe(‘Title’);

Specify size• frame.setSize(width, height)

Designate that closing the window ends the program• frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Show the window• frame.setVisible(true);

Page 18: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Using JFrame

Page 19: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Extending JFrame

Page 20: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Basic Drawing

Page 21: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Example JFrame

Page 22: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Example JPanel

Page 23: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Drawing Methods

Page 24: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Graphics Class Methods

Page 25: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Graphics Class Methods

Page 26: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Graphics Class Methods

Page 27: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

What would be the outputof this program?

Sketch on paper what the output would look like

Page 28: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

javac MyPaint.javajava –cp . MyPaint

Page 29: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Example

1

2

Page 30: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

QuestionWhat will the output for the following program?

Page 31: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Page 32: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Fixing Problems

Page 33: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Drawing Images

Page 34: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Images

Page 35: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Review Basic Graphic Methods

Page 36: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

Write down the graphics methods you’d use to draw the following output:

Page 37: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Question

What would the followingprogram output?

javac MyPaint.javajava –cp . MyPaint

Page 38: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Left mouse button is pressed

Page 39: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Force Redraw

To notify Java to redraw our window (i.e., call paintComponent)

call ‘repaint()’call ‘repaint()’

For example, add new graphics each time the mouse button is pressed

Page 40: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Review

Graphics can be drawn using a class which extends JPanelSwing will call the paintComponent method to draw:protected void paintComponent(Graphics g);

There are a variety of drawing methods:drawLine(int x1, int y1, int x2, int y2);drawRect(int x, int y, int w, int h);drawOval(int x, int y, int w, int h);drawPolygon(int[] xpoints, int[] ypoints, int

npoints);

Page 41: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Other Graphics Capabilities

Lots of GUI ControlsButtons, sliders, checkboxes, tables, etc

• which you can override and improve

Rich 2D drawingRich 2D drawingLine properties, translucent drawing, rotating,

scaling images, coordinate transforms, ...

AnimationDouble-buffering, combining drawing with

threads, ..

Page 42: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Examples

Page 43: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Experiment

Only scratched the surface of GraphicsVery simple windows and graphics

Combine the graphics with GUI componentsButtons/menusButtons/menus

Custom ‘interfaces’ componentse.g., Override default draw method for a button to

create a new improved button look

EventsMouse button presses (drawing program)Timing (animations)

Page 44: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Challenge

Create a simple vector drawing program in Java

Clear/load/save drawingsClear/load/save drawings

Select buttons for different operations

line, circle, color, ...

Detect mouse button presses

draw lines, drag shapes, pick objects

Page 45: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Summary

Overview Essential Graphical Principles

JFrame, JPanel, paintComponent, @Override (Checking)@Override (Checking)

Hands-On/Practical

Today is about Graphics with Java

Drawing

Page 46: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

This Week

Read Associated Chapters

Comprehensive Edition (Additional Chapters)Chapters)

Review Slides

Java Exercises

Online Quizzes

Page 47: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Crosswordsemicolon

o d u l u s

n

Page 48: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Exercises

Chapter exercises 15.1-15.3

Comment your codeComment your code

Remember to zip the files together

The name of the .zip file should be your student number

(e.g., 39293923923.zip)

Page 49: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Questions/Discussion

Page 50: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Revision QuestionWhat does the following program print?

a) Pain, Gain or Main (varies randomly)

b) Pain or Main (varies randomly)

c) Main (always)

d) None of the above

Page 51: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There

Answer

Investigate for next week...

Page 52: Graphics - GitHub Pages · Graphics can be drawn using a class which extends JPanel Swing will call the paintComponentmethod to draw: protected void paintComponent(Graphics g); There