Top Banner
Working with 2D Shapes --------- ------- ----- --- -
45

Swing-2D

Jul 20, 2016

Download

Documents

bhuvangates

Swing-2D
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: Swing-2D

Working with 2D Shapes

-------------------------

Page 2: Swing-2D

Graphics• Java 1.0, the Graphics class had

methods to draw – lines, – rectangles, – ellipses, and so on.

• Drawing operations are very limited:– Cannot vary the line thickness and – Cannot rotate the shapes.

Page 3: Swing-2D

Graphics 2D• Java SE 1.2 introduced the Java 2D

library, which implements a powerful set of graphical operations.

• To draw shapes in the Java 2D library, you need to obtain an object of the Graphics2D class.

• This class is a subclass of the Graphics class.

Page 4: Swing-2D

• paintComponent automatically receive an object of the Graphics2D class.

• Simply use a cast, as follows:public void paintComponent(Graphics g){

Graphics2D g2 = (Graphics2D) g;. . .

}

Page 5: Swing-2D

Geometric Shapes• Classes to represent lines,

rectangles, and ellipses:– Line2D– Rectangle2D– Ellipse2D

• These classes all implement the Shape interface.

Page 6: Swing-2D

Rectangle• To draw a shape,

– you first create an object of a class that implements the Shape interface and

– then call the draw method of the Graphics2D class.

• For example:Rectangle2D rect = . . .;g2.draw(rect);

Page 7: Swing-2D

Concrete Subclasses• The designers of the 2D library

decided to supply two versions of each shape class: – one with float coordinates and

• Rectangle2D.Float– one with double coordinates

• Rectangle2D.Double

Page 8: Swing-2D

2D Rectangle Classes

Page 9: Swing-2D

Rectangle2D• Use Rectangle2D variables to hold

the rectangle references:• Rectangle2D.Float floatRect = new Rectangle2D.Float(10.0F, 25.0F,

22.5F, 20.0F);• Rectangle2D.Double doubleRect = new Rectangle2D.Double(10.0, 25.0,

22.5, 20.0);

Page 10: Swing-2D

Point2D• Point2D class with subclasses:

– Point2D.Float and – Point2D.Double

• To make a point object:Point2D p = new Point2D.Double(10, 20);

Page 11: Swing-2D

The bounding rectangle of an ellipse

• The classes Rectangle2D and Ellipse2D both inherit from the common superclass RectangularShape.

• Admittedly, ellipses are not rectangular, but they have a bounding rectangle.

Page 12: Swing-2D

RectangularShape• The RectangularShape class defines

over 20 methods that are common to these shapes:– getWidth, – getHeight, – getCenterX, and – getCenterY

Page 13: Swing-2D

Rectangle2D and Ellipse2D• Rectangle2D and Ellipse2D objects are

simple to construct. Specify:– the x- and y-coordinates of the top-left

corner; and – the width and height.Ellipse2D e = new Ellipse2D.Double(150, 200, 100,

50);• constructs an ellipse that is bounded by

a rectangle with the top-left corner at (150, 200), width 100, and height 50.

Page 14: Swing-2D

Relationships b/w the shape classes

Page 15: Swing-2D

Blank Rectangle• If p isn’t the top-left corner, one or both of

the coordinate differences will be negative and the rectangle will come out empty.

• In that case, first create a blank rectangle and use the setFrameFromDiagonal method, as follows:Rectangle2D rect = new Rectangle2D.Double();rect.setFrameFromDiagonal(px, py, qx, qy);

• if you know the corner points as Point2D objects p and q, thenrect.setFrameFromDiagonal(p, q);

Page 16: Swing-2D

setFrameFromCenter• The setFrameFromCenter method

uses the center point, but it still requires one of the four corner points.

• Constructing an ellipse as follows:Ellipse2D ellipse = new Ellipse2D.Double(centerX - width / 2, centerY - height / 2, width, height);

Page 17: Swing-2D

Line 2D• To construct a line, supply the start

and end points, either as Point2D objects or as pairs of numbers:Line2D line = new Line2D.Double(start, end);

orLine2D line = new Line2D.Double(startX, startY, endX, endY);

Page 18: Swing-2D

Using Color• The setPaint method of the

Graphics2D class lets you select a color that is used for all subsequent drawing operations on the graphics context.

• For example:g2.setPaint(Color.RED);g2.drawString("Warning!", 100, 100);

Page 19: Swing-2D

fill• Fill the interiors of closed shapes

(such as rectangles or ellipses) with a color.

• Simply call fill instead of draw:Rectangle2D rect = . . .;g2.setPaint(Color.RED);g2.fill(rect); // fills rect with red color

Page 20: Swing-2D

Colors• To draw in multiple colors, you select

a color, draw or fill, then select another color, and draw or fill again.

• You define colors with the Color class. • The java.awt.Color class offers

predefined constants for the following 13 standard colors:– BLACK, BLUE, CYAN, DARK_GRAY,

GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW

Page 21: Swing-2D

Custom Color• Specify a custom color by creating a Color

object by its red, green, and blue components.

• Using a scale of 0–255 (that is, one byte) for the redness, blueness, and greenness, call the Color constructor like this:Color(int redness, int greenness, int blueness)

• Here is an example of setting a custom color:g2.setPaint(new Color(0, 128, 128)); // a dull blue-greeng2.drawString("Welcome!", 75, 125);

Page 22: Swing-2D

Background Color• To set the background color, you use the

setBackground method of the Component class, an ancestor of JComponent.MyComponent p = new MyComponent();p.setBackground(Color.PINK);

• setForeground method specifies the default color that is used for drawing on the component.

Page 23: Swing-2D

• String filename = "...";• Image image = ImageIO.read(new

File(filename));

Page 24: Swing-2D

• String urlname = "...";• Image image = ImageIO.read(new

URL(urlname));

Page 25: Swing-2D

• public void paintComponent(Graphics g)

• {• . . .• g.drawImage(image, x, y, null);• }

Page 26: Swing-2D

• for (int i = 0; i * imageWidth <= getWidth(); i++)

• for (int j = 0; j * imageHeight <= getHeight(); j++)

• if (i + j > 0)• g.copyArea(0, 0, imageWidth,

imageHeight, i * imageWidth, j * imageHeight);

Page 27: Swing-2D

• Its content, such as the state of a button (pushed in or not), or the text in a text field

• Its visual appearance (color, size, and so on)

• Its behavior (reaction to events)

Page 28: Swing-2D

• The model, which stores the content• The view, which displays the content• The controller, which handles user

input

Page 29: Swing-2D
Page 30: Swing-2D
Page 31: Swing-2D
Page 32: Swing-2D

Flow Layout• FlowLayout()• FlowLayout(int align)• FlowLayout(int align, int hgap, int vgap)

constructs a new FlowLayout• Parameters:

– align One of LEFT, CENTER, or RIGHT– hgap The horizontal gap to use in pixels

(negative values force an overlap)– vgap The vertical gap to use in pixels

(negative values force an overlap)

Page 33: Swing-2D

Border Layout

Page 34: Swing-2D

Border Layout• BorderLayout()• BorderLayout(int hgap, int vgap)

constructs a new BorderLayout.• Parameters:

– hgap The horizontal gap to use in pixels (negative values force an overlap)

– vgap The vertical gap to use in pixels (negative values force an overlap)

Page 35: Swing-2D

Grid Layout

Page 36: Swing-2D

Grid Layout• GridLayout(int rows, int columns)• GridLayout(int rows, int columns, int

hgap, int vgap)

constructs a new GridLayout. One of rows and columns (but not both) may be zero, denoting an arbitrary number of components per row or column.

Page 37: Swing-2D

• Parameters: – rows The number of rows in the grid– columns The number of columns in the

grid– hgap The horizontal gap to use in pixels

(negative values force an overlap)– vgap The vertical gap to use in pixels

(negative values force an overlap)

Page 38: Swing-2D

Text Fields• JPanel panel = new JPanel();• JTextField textField = new

JTextField("Default input", 20);• panel.add(textField);

Page 39: Swing-2D

• JTextField(int cols)– constructs an empty JTextField with a

specified number of columns.• JTextField(String text, int cols)

– constructs a new JTextField with an initial string and the specified number of columns.

• int getColumns()• void setColumns(int cols)

– gets or sets the number of columns that this text field should use.

Page 40: Swing-2D

Labels and Labeling Components• JLabel label = new JLabel("User

name: ", SwingConstants.RIGHT);or• JLabel label = new JLabel("User

name: ", JLabel.RIGHT);

Page 41: Swing-2D

• JLabel(String text)• JLabel(Icon icon)• JLabel(String text, int align)• JLabel(String text, Icon icon, int align)

constructs a label.• Parameters:

– text The text in the label– icon The icon in the label– align One of the SwingConstants constants

LEFT (default), CENTER, or RIGHT

Page 42: Swing-2D

• String getText()• void setText(String text)

– gets or sets the text of this label.• Icon getIcon()• void setIcon(Icon icon)

– gets or sets the icon of this label.

Page 43: Swing-2D

Password Fields• JPasswordField(String text, int columns)

– constructs a new password field.• void setEchoChar(char echo)

– sets the echo character for this password field. This is advisory; a particular look and feel may insist on its own choice of echo character. A value of 0 resets the echo character to the default.

• char[] getPassword()– returns the text contained in this password field.

For stronger security, you should overwrite the content of the returned array after use. (The password is not returned as a String because a string would stay in the virtual machine until it is garbage-collected.)

Page 44: Swing-2D

Text Areas

Page 45: Swing-2D

• textArea = new JTextArea(8, 40); // 8 lines of 40 columns each