0% found this document useful (0 votes)
41 views3 pages

Java 2D Graphics Drawing Shapes

This document contains Java code that defines a class called JavaLesson47 that draws different 2D shapes to a JFrame using Graphics2D. It imports necessary classes for graphics and shapes. The main method creates an instance of JavaLesson47. The JavaLesson47 constructor creates and sets up the JFrame. It adds a DrawStuff component to the frame which overrides the paint method. In the paint method, different shapes like lines, arcs, ellipses, rectangles and curves are created using classes like Line2D, Arc2D, Ellipse2D, Rectangle2D and drawn to the Graphics2D canvas. Features like transparency, gradients and antialiasing are also demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Java 2D Graphics Drawing Shapes

This document contains Java code that defines a class called JavaLesson47 that draws different 2D shapes to a JFrame using Graphics2D. It imports necessary classes for graphics and shapes. The main method creates an instance of JavaLesson47. The JavaLesson47 constructor creates and sets up the JFrame. It adds a DrawStuff component to the frame which overrides the paint method. In the paint method, different shapes like lines, arcs, ellipses, rectangles and curves are created using classes like Line2D, Arc2D, Ellipse2D, Rectangle2D and drawn to the Graphics2D canvas. Features like transparency, gradients and antialiasing are also demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

package javaLessons_40_to_49;

import [Link]; // base class for creating all swing components


import [Link]; // as drawing everything in JFrame
import [Link].*; // create interfaces and paint graphics
import [Link].*; // defining 2D shapes

@SuppressWarnings("serial") // to suppress compile warnings

public class JavaLesson47 extends JFrame{ // 2D graphics

public static void main(String[] args) {

new JavaLesson47();

public JavaLesson47() {
// create frame
[Link](500, 500);
[Link]("Drawing Shapes");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new DrawStuff(), [Link]);
[Link](true);

private class DrawStuff extends JComponent {

public void paint(Graphics g) { // graphics is the base class that allows us to


draw

Graphics2D graph2 = (Graphics2D)g; // extend graphics to draw 2D shapes and


images with graphics 2D
// set rendering preferences clean up edges by getting
ANTIALIASING
[Link](RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

Shape drawLine = new [Link](20, 90, 55, 250);


// x, y, w, h,
starting_angle, angular_extent, type
Shape drawArc2D = new [Link](5, 150, 100, 100, 45, 45, [Link]);
// curve
Shape drawArc2D2 = new [Link](5, 200, 100, 100, 45, 45, [Link]);
// Straight line connects curve
Shape drawArc2D3 = new [Link](5, 250, 100, 100, 45, 45, [Link]);
// pie
// angular extent refers to how many degrees the arc continues from the
starting angle

// define color used for rendering


[Link]([Link]);

[Link](drawLine);
[Link](drawArc2D);
[Link](drawArc2D2);
[Link](drawArc2D3);

// upper left hand corner x and y, bottom


right hand corner x and y
Shape drawEllipse = new [Link](10,10,100,100);
// draw rectangle x, y, w, h
Shape drawRect = new [Link](300, 300, 150, 100);

// draw rounded rectangle upper left hand corner x and y, bottom right
hand corner x and y, arc height and width
Shape drawRoundRec = new [Link](25,25,50,50,45,45);

// curve defined with 4 points


CubicCurve2D cubicCurve = new [Link]();
// define 4 points
[Link](110,50,300,200,200,200,90,263);

// draw curve with 3 points (x1,y1,ctrl1,ctrl2,x2,y2)


ctrl are control points
Shape drawQuadCurve = new [Link](300, 100, 400, 200, 150, 300);

// create rectangle for use in makeing transparent


Shape drawTransRect = new [Link](300, 300, 75, 50);

[Link](drawEllipse);
// set color to green
[Link]([Link]);
// draw with fill instead of empty lines
[Link](drawRect);
// change stroke color
[Link]([Link]); // this works but use
//[Link](); to change stroke settings

[Link](drawRect);
[Link](drawRoundRec);
[Link](cubicCurve);
[Link](drawQuadCurve);

// to draw things transparent


0.40F = 60% transparency
[Link]([Link](AlphaComposite.SRC_OVER,
0.40F));

// to fill the interior of the transparent shape


[Link](new [Link](10, 10, 150, 100));

[Link](drawTransRect);

// remove transparency for all shapes after


[Link]([Link](AlphaComposite.SRC_OVER,
1.0F));

// draw gradients
// starting point, starting color,
angle-0 for vertical,
// how many pixels the blue goes to before it
changes color, the color it changes to (hex)
GradientPaint theGradient = new GradientPaint(0,0, [Link], 0, 60,new
Color(0x66ffff)); // add ,true to end to make color in middle

[Link](theGradient);

[Link](new [Link](150, 10, 150, 100));

}
}

You might also like