(C) 2010 Pearson Education, Inc. All rights reserved. Class Graphics (from package java.awt)...

40
(C) 2010 Pearson Education, Inc. All rights reserved. Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto the screen. Class JPanel (from package javax.swing) provides an area on which we can draw.

Transcript of (C) 2010 Pearson Education, Inc. All rights reserved. Class Graphics (from package java.awt)...

Page 1: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto the screen.

Class JPanel (from package javax.swing) provides an area on which we can draw.

Page 2: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

PaintingPainting

When a GUI needs to change its visual appearance it performs a paint operation

Swing components generally repaint themselves as needed

Painting code executes on the event-dispatching thread◦ If painting takes a long time, no events will be

handled during that time

Page 3: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

ExampleExample

import javax.swing.*; import java.awt.*;

public class Painting extends JPanel { public Painting() {}

public void paintComponent(Graphics g) {super.paintComponent(g);g.setColor( Color.yellow ); g.fillOval( 10,10,50,50 );g.setColor( Color.black ); g.drawOval( 10,10,50,50 );

}

public static void main( String args[] ) {JFrame win = new JFrame( "Painting" );win.setSize(100, 100);win.getContentPane().add( new Painting() );win.setvisible(true);

}}

Page 4: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

The The GraphicsGraphics Object Object

The Graphics object both a context for painting and methods for performing the painting.

The graphics context consists of state such as the current painting color, the current font, and the current painting area◦ The color and font are initialized to the

foreground color and font of the component just before the invocation of paintComponent

Page 5: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

The Coordinate SystemThe Coordinate System

Each component has its own integer coordinate system◦ Ranging from (0, 0) to (width - 1, height - 1)◦ Each unit represents the size of one pixel

Page 6: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

ColorColor

Combinations of Red, Green, Blue Each [0, 255]

Java: new Color(255, 150, 0)

Hokie Orange

Page 7: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

FontFont

Color and font: g2.setColor( new Color(r,g,b) ); g2.setFont( new Font(…) );

new font(“Serif”, Font.BOLD, Font.Italic, 18);

Page 8: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

Re-PaintRe-Paint

Screen is like a painter’s canvas All windows paint on the same surface! Windows don’t “remember” whats under them

Need to re-paint when portions are newly exposed

Receive Repaint events Open, resize, bring to front When other windows in front move, resize, close

Page 9: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 10: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 11: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 12: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

The keyword extends creates a so-called inheritance relationship.

The class from which DrawPanel inherits, JPanel, appears to the right of keyword extends.

In this inheritance relationship, JPanel is called the superclass and DrawPanel is called the subclass.

Page 13: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

JPanel has a paintComponent method, which the system calls every time it needs to display the JPanel.

The first statement in every paintComponent method you create should always be

super.paintComponent( g ); JPanel methods getWidth and getHeight return the JPanel’s width and height, respectively.

Graphics method drawLine draws a line between two points represented by its four arguments. The first two are the x- and y-coordinates for one endpoint, and the last two arguments are the coordinates for the other endpoint.

Page 14: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

To display the DrawPanel on the screen, place it in a window. Create a window with an object of class JFrame. JFrame method setDefaultCloseOperation with the

argument JFrame.EXIT_ON_CLOSE indicates that the application should terminate when the user closes the window.

JFrame’s add method attaches the DrawPanel (or any other GUI component) to a JFrame.

JFrame method setSize takes two parameters that represent the width and height of the JFrame, respectively.

JFrame method setVisible with the argument true displays the JFrame.

When a JFrame is displayed, the DrawPanel’s paintComponent method is implicitly called

Page 15: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Graphics methods drawRect and drawOval Method drawRect requires four arguments. The first two

represent the x- and y-coordinates of the upper-left corner of the rectangle; the next two represent the rectangle’s width and height.

To draw an oval, method drawOval creates an imaginary rectangle called a bounding rectangle and places inside it an oval that touches the midpoints of all four sides.

Method drawOval requires the same four arguments as method drawRect. The arguments specify the position and size of the bounding rectangle for the oval.

Page 16: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 17: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 18: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 19: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 20: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 21: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Colors displayed on computer screens are defined by their red, green, and blue components (called RGB values) that have integer values from 0 to 255.

The higher the value of a component color, the richer that shade will be in the final color.

Java uses class Color in package java.awt to represent colors using their RGB values.

Class Color contains 13 predefined static Color objects—BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE and YELLOW.

Page 22: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Class Color also contains a constructor of the form: public Color( int r, int g, int b )

so you can create custom colors by specifying the red, green and blue component values.

Graphics methods fillRect and fillOval draw filled rectangles and ovals, respectively.

Graphics method setColor sets the current drawing color.

Page 23: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 24: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 25: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 26: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 27: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Drawing arcs in Java is similar to drawing ovals—an arc is simply a section of an oval.

Graphics method fillArc draws a filled arc. Method fillArc requires six parameters.

The first four represent the bounding rectangle in which the arc will be drawn.

The fifth parameter is the starting angle on the oval, and the sixth specifies the sweep, or the amount of arc to cover.

Starting angle and sweep are measured in degrees, with zero degrees pointing right.

A positive sweep draws the arc counterclockwise. Method drawArc requires the same parameters as fillArc,

but draws the edge of the arc rather than filling it. Method setBackground changes the background color of a

GUI component.

Page 28: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 29: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 30: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 31: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 32: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 33: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 34: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 35: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 36: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Class Point (package java.awt) represents an x-y coordinate. We use objects of this class to store the coordinates of each mouse

drag event. Class Graphics is used to draw. MouseEvent method getPoint obtains the Point

where the event occurred. Method repaint (inherited from Component) indicates

that a Component should be refreshed on the screen as soon as possible.

Page 37: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 38: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Graphics method fillOval draws a solid oval. Four parameters represent a rectangular area (called the bounding

box) in which the oval is displayed. The first two are the upper-left x-coordinate and the upper-left y-

coordinate of the rectangular area. The last two represent the rectangular area’s width and height.

Method fillOval draws the oval so it touches the middle of each side of the rectangular area.

Page 39: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 40: (C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

(C) 2010 Pearson Education, Inc. All rights reserved.