how to draw in java

Solutions on MaxInterview for how to draw in java by the best coders in the world

showing results for - "how to draw in java"
Alessio
06 Nov 2018
1package com.javacodegeeks.snippets.desktop;
2 
3import java.awt.Component;
4import java.awt.Frame;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.Polygon;
8 
9public class DrawShapesExample {
10 
11  public static void main(String[] args) {
12 
13        // Create a frame
14
15        Frame frame = new Frame();
16
17        // Add a component with a custom paint method
18
19        frame.add(new CustomPaintComponent());
20
21        // Display the frame
22
23        int frameWidth = 300;
24
25        int frameHeight = 300;
26
27        frame.setSize(frameWidth, frameHeight);
28
29        frame.setVisible(true);
30 
31  }
32 
33 /**
34  * To draw on the screen, it is first necessary to subclass a Component 
35  * and override its paint() method. The paint() method is automatically called 
36  * by the windowing system whenever component's area needs to be repainted.
37  */
38  static class CustomPaintComponent extends Component {
39 
40          public void paint(Graphics g) {
41
42            // Retrieve the graphics context; this object is used to paint shapes
43
44            Graphics2D g2d = (Graphics2D)g;
45
46            // Draw an oval that fills the window
47
48            int x = 0;
49
50            int y = 0;
51
52            int w = getSize().width-1;
53
54            int h = getSize().height-1;
55
56            /**
57              * The coordinate system of a graphics context is such that the origin is at the 
58              * northwest corner and x-axis increases toward the right while the y-axis increases 
59              * toward the bottom.
60              */
61
62            g2d.drawLine(x, y, w, h);
63
64            // to draw a filled oval use : g2d.fillOval(x, y, w, h) instead
65
66            g2d.drawOval(x, y, w, h);
67
68            // to draw a filled rectangle use : g2d.fillRect(x, y, w, h) instead
69
70            g2d.drawRect(x, y, w, h);
71
72            // A start angle of 0 represents a 3 o'clock position, 90 represents a 12 o'clock position,
73
74            // and -90 (or 270) represents a 6 o'clock position
75
76            int startAngle = 45;
77
78            int arcAngle = -60;
79
80            // to draw a filled arc use : g2d.fillArc(x, y, w, h, startAngle, arcAngle) instead
81
82            g2d.drawArc(x, y, w/2, h/2, startAngle, arcAngle);
83
84            // to draw a filled round rectangle use : g2d.fillRoundRect(x, y, w, h, arcWidth, arcHeight) instead
85
86            g2d.drawRoundRect(x, y, w, h, w/2, h/2);
87
88            Polygon polygon = new Polygon();
89
90            polygon.addPoint(w/4, h/2);
91
92            polygon.addPoint(0, h/2);
93
94            polygon.addPoint(w/4, 3*h/4);
95
96            polygon.addPoint(w/2, 3*h/4);
97
98            // Add more points...
99
100            // to draw a filled round rectangle use : g2d.fillPolygon(polygon) instead
101
102            g2d.drawPolygon(polygon);
103
104          }
105 
106  }
107 
108}