• No results found

Shapes

N/A
N/A
Protected

Academic year: 2020

Share "Shapes"

Copied!
56
0
0

Loading.... (view fulltext now)

Full text

(1)

Shapes

(2)
(3)

Shape Class

Shape class inherits from Node class

Several classes inherit from shape class

Can be used to draw objects to the screen

http://

docs.oracle.com/javafx/2/api/javafx/scene/sha

pe/Shape.html

Most methods in shape class

Stroke of shapeFill of shape

(4)

Line

Line class is used to draw lines

Subclass of shape class

Has a start point and end point

startX, endX, startY, endY

(5)

Example - LineExample.java

LineExample.java

Shown in the next few slides

(6)

Line Code

@Override

public void start(Stage mainFrame) {

//sx, sy, ex, ey

Line line1 = new Line(0.0, 0.0, 200, 200);

Pane drawingPane = new Pane();

drawingPane.getChildren().add(line1);

scene = new Scene(drawingPane, 300, 300); mainFrame.setScene(scene);

(7)

Line Image

Point (0, 0)

Point (50, 50)

X Y

(8)

Line Options

Can make line thinker

Ex: line1.setStrokeWidth(4)

Can make line colored

Ex: line1.setStroke(Color.RED)

(9)

Centering the line

Find width and height of the panel

Subtract the width of the line from the width of

the panel

Subtract the height of the line from the height of

the panel

Take half the x and y distance and add to the

starting point

Take the other half and subtract from the end

point

(10)

Center Line Code

double xDifference = drawingPane.getWidth() - Math.abs(line1.getStartX() - line1.getEndX());

double yDifference = drawingPane.getHeight() - Math.abs(line1.getStartY() - line1.getEndY());

(11)

Line Image 2

(12)
(13)

Scalable Line

Bind points of line to relative points on the

pane

Need to get the line point properties

Need to call bind for those properties

(14)

Scalable Line

Start with our existing code from the previous

example

Bind the points after setting them

Only need to bind end points

Start points will not change

Changes

The line will scale with the screen

The line distance will not be kept constant

(15)

Scalable Line Code

line1.endXProperty().bind(drawingPane.widthPr

operty().subtract(.5*xDifference));

line1.endYProperty().bind(drawingPane.heightP

roperty().subtract(.5*yDifference));

(16)
(17)

Scalable Line Image 2

(18)

Polyline

Connect a bunch of points

Shape does not have to be closed

Holds a list of points

X1, y1, x2, y2, x3, y3, etc…

http://

(19)

Example - PolylineExample.java

PolylineExample.java

Shown in the next few slides

(20)

Polyline Example Code

(21)

Smarter Code

double startX = 10.0; double startY = 10.0; double increment = 50.0;

for(int i=0; i<16; i++) {

if(i%4 == 0) {

polyline1.getPoints().add(startX); polyline1.getPoints().add(startY); }

else if(i%4 == 1) {

polyline1.getPoints().add(startX);

polyline1.getPoints().add(startY + increment); }

//continued on next slide

(22)

Smarter Code Continued

else if(i%4 == 2) {

polyline1.getPoints().add(startX + increment); polyline1.getPoints().add(startY + increment); }

else if(i%4 == 3) {

polyline1.getPoints().add(startX + increment); polyline1.getPoints().add(startY);

startX = startX + increment + increment; }

(23)

Polyline Image

(24)

Line Properties

Just like lines

Can set stroke color

polyline1.setStroke(Color.BLUE);

Can set stroke width

(25)

Polyline Image 2

(26)

Text

Can draw text anywhere on the panel

Text starts at point specified

Can set various text properties

Font, size, color

http://

(27)

Example – TextExample.java

TextExample.java

Example shown in next few slides

(28)

Text Code

@Override

public void start(Stage mainFrame) {

Text textPiece = new Text(10, 10, "String of text");

Pane drawingPane = new Pane();

drawingPane.getChildren().add(textPiece); scene = new Scene(drawingPane, 300, 300);

mainFrame.setScene(scene); mainFrame.show();

(29)

Text Image

(30)

Text Properties

Font

textPiece.setFont(Font.font("Times New Roman",

FontWeight.BOLD, FontPosture.REGULAR, 20));

Underline

textPiece.setUnderline(true);

Color

(31)

Text Image 2

(32)

Centering Text

Get total width of text

textPiece.getLayoutBounds().getWidth()

Get total height of text

textPiece.getLayoutBounds().getHeight()

Get total width of pane

drawingPane.getWidth()

Get total height of pane

(33)

Centering Text

Subtract off the width of the text from the total

panel width

Subtract off the height of the text from the

total panel height

Set the start x value to half the width

difference

Set the start y value to half the height

difference

(34)

Centering Text Code

double textWidth = textPiece.getLayoutBounds().getWidth(); double textHeight = textPiece.getLayoutBounds().getHeight(); double differenceX = drawingPane.getWidth() - textWidth; double differenceY = drawingPane.getHeight() - textHeight;

(35)

Centered Text Image

(36)

Dimension Notes

Everything is sized when the scene size is set

scene = new Scene(drawingPane, 300, 300);

Before that line of code everything has no

width or height

(37)

Rectangle

Can create rectangles

Can specify

Upper left corner locationWidth

HeightFill color

Border color

– Corner type (pointy / curved)

http://

docs.oracle.com/javafx/2/api/javafx/scene/shape/R

(38)

Example - RectangleExample.java

RectangleExample.java

(39)

Rectangle Code

Create rectangle

Rectangle rectangle1= new Rectangle(50, 50, 100, 100); – (x, y, width, height)

Set Border Width

rectangle1.setStrokeWidth(4);

Set Border Color

rectangle1.setStroke(Color.RED);

Set Fill Color

rectangle1.setFill(Color.BLUE);

(40)
(41)

Circle Code

Create Circle

Circle circle1 = new Circle(150, 150, 100);Center x, center y, radius

Set fill

Same for all shapes

circle1.setFill(Color.BLUE);

http://

docs.oracle.com/javafx/2/api/javafx/scene/sha

pe/Circle.html

(42)
(43)

Example - CircleExample.java

CircleExample.java

Example shown in next few slides

(44)

Multiple Circles

Create circles with the same center pointChoose different size radii

(45)

Multiple Circles Continued

Choose a different fill color for each circle

(46)

Multiple Circles Continued

Add the circles to the pane

The largest circle should be added firstThe circles added last will display on top

(47)

Multiple Circles Image

(48)

Ellipse

Defined by a center point and 2 radii

Ellipse ellipse1 = new Ellipse(150, 150, 30, 60);

Everything else is similar to other shapes

http://

(49)

Ellipse Image

(50)

Example - ShapeExample6.java

(51)

Polygon

Similar to polyline class

Shape is automatically closed

http://

docs.oracle.com/javafx/2/api/javafx/scene/sha

pe/Polygon.html

Next example will reuse the same exact

polyline code and add a fill color

Will change polyline to polygon

(52)

Example - PolygonExample.java

PolygonExample.java

(53)

Polyline Image

(54)
(55)

Compound Shape Example

(56)

Example - CompoundExample.java

http://docs.oracle.com/javafx/2/api/javafx/scene/sha http://docs.oracle.com/javafx/2/api/javafx/scene/shape/Line.html http://docs.oracle.com/javafx/2/api/javafx/scene/sha http://docs.oracle.com/javafx/2/api/javafx/scene/text/ http://docs.oracle.com/javafx/2/api/javafx/scene/shape/R http://docs.oracle.com/javafx/2/api/javafx/scene/sha http://docs.oracle.com/javafx/2/api/javafx/scene/sha http://docs.oracle.com/javafx/2/api/javafx/scene/sha

References

Related documents

(Sareeh Abdul Karim, 2010) confirmed that the movement scope is directly related to the time periods and the harmony of their application with motion performance, which

The elastic body of the sensor mechanical structure comprises of central support beam, cross elastic beams, compliant beams and base of the body.. Here the

Using the World Bank’s Enterprise Surveys database, they find that reported constraints reflect country characteristics and vary systematically by level of income—the most

At this point, Representative Szederkényi uttered his opinion that the part of the proposition related to the naturalization and re-naturalization of foreigners pri- marily

establishes that annual changes in base margin will be allocated based on System Average Percent Change (“SAPC”) and applied to each functional category of the base margin..

In an earlier study describing the growth patterns of untreated children with the classic SV form of CAH we showed that growth velocity and bone maturation are not increased in

1900 1902 1903 1907 1908 1910 1930 Gothic Typefaces by Benton Globe Gothic Franklin Gothic Alternate Gothic Monotone Gothic News Gothic Clearface Gothic Bank Gothic... Advertising