Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

package javaLessons_40_to_49;

import javax.swing.JComponent; // base class for creating all swing components


import javax.swing.JFrame; // as drawing everything in JFrame
import java.awt.*; // create interfaces and paint graphics
import java.awt.geom.*; // 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
this.setSize(500, 500);
this.setTitle("Drawing Shapes");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new DrawStuff(), BorderLayout.CENTER);
this.setVisible(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
graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

Shape drawLine = new Line2D.Float(20, 90, 55, 250);


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

// define color used for rendering


graph2.setPaint(Color.BLACK);

graph2.draw(drawLine);
graph2.draw(drawArc2D);
graph2.draw(drawArc2D2);
graph2.draw(drawArc2D3);

// upper left hand corner x and y, bottom


right hand corner x and y
Shape drawEllipse = new Ellipse2D.Float(10,10,100,100);
// draw rectangle x, y, w, h
Shape drawRect = new Rectangle2D.Float(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 RoundRectangle2D.Double(25,25,50,50,45,45);

// curve defined with 4 points


CubicCurve2D cubicCurve = new CubicCurve2D.Double();
// define 4 points
cubicCurve.setCurve(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 QuadCurve2D.Double(300, 100, 400, 200, 150, 300);

// create rectangle for use in makeing transparent


Shape drawTransRect = new Rectangle2D.Double(300, 300, 75, 50);

graph2.draw(drawEllipse);
// set color to green
graph2.setColor(Color.GREEN);
// draw with fill instead of empty lines
graph2.fill(drawRect);
// change stroke color
graph2.setPaint(Color.BLACK); // this works but use
//graph2.setStroke(); to change stroke settings

graph2.draw(drawRect);
graph2.draw(drawRoundRec);
graph2.draw(cubicCurve);
graph2.draw(drawQuadCurve);

// to draw things transparent


0.40F = 60% transparency
graph2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.40F));

// to fill the interior of the transparent shape


graph2.fill(new Rectangle2D.Float(10, 10, 150, 100));

graph2.draw(drawTransRect);

// remove transparency for all shapes after


graph2.setComposite(AlphaComposite.getInstance(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, Color.BLUE, 0, 60,new
Color(0x66ffff)); // add ,true to end to make color in middle

graph2.setPaint(theGradient);

graph2.fill(new Rectangle2D.Float(150, 10, 150, 100));

}
}

You might also like