awt

Draw shapes example

With this tutorial we shall show you how to draw simple shapes in a Java Desktop Application. This is a very important step when designing your own graphics for your App.

Basically, all you have to do in order to draw shapes in a Java application is:

  • Create a new Frame.
  • Create a class that extends the Component class and override the paint method.
  • Use Graphics2D.drawLine to draw a simple line.
  • Use Graphics2D.drawOval to draw an oval shape in the screen.
  • Use Graphics2D.drawRect to draw a rectangle on the screen.
  • Use Graphics2D.drawArc to draw an arch.
  • Create a new Polygon to add a polygon to your drawing, and use addPoint to add more points to the polygon.

Let’s see the code snippet that follows:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
package com.javacodegeeks.snippets.desktop;
 
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
 
public class DrawShapesExample {
 
  public static void main(String[] args) {
 
// Create a frame
 
Frame frame = new Frame();
 
// Add a component with a custom paint method
 
frame.add(new CustomPaintComponent());
 
// Display the frame
 
int frameWidth = 300;
 
int frameHeight = 300;
 
frame.setSize(frameWidth, frameHeight);
 
frame.setVisible(true);
 
  }
 
 /**
  * To draw on the screen, it is first necessary to subclass a Component
  * and override its paint() method. The paint() method is automatically called
  * by the windowing system whenever component's area needs to be repainted.
  */
  static class CustomPaintComponent extends Component {
 
public void paint(Graphics g) {
 
  // Retrieve the graphics context; this object is used to paint shapes
 
  Graphics2D g2d = (Graphics2D)g;
 
  // Draw an oval that fills the window
 
  int x = 0;
 
  int y = 0;
 
  int w = getSize().width-1;
 
  int h = getSize().height-1;
 
  /**
    * The coordinate system of a graphics context is such that the origin is at the
    * northwest corner and x-axis increases toward the right while the y-axis increases
    * toward the bottom.
    */
 
  g2d.drawLine(x, y, w, h);
 
  // to draw a filled oval use : g2d.fillOval(x, y, w, h) instead
 
  g2d.drawOval(x, y, w, h);
 
  // to draw a filled rectangle use : g2d.fillRect(x, y, w, h) instead
 
  g2d.drawRect(x, y, w, h);
 
  // A start angle of 0 represents a 3 o'clock position, 90 represents a 12 o'clock position,
 
  // and -90 (or 270) represents a 6 o'clock position
 
  int startAngle = 45;
 
  int arcAngle = -60;
 
  // to draw a filled arc use : g2d.fillArc(x, y, w, h, startAngle, arcAngle) instead
 
  g2d.drawArc(x, y, w/2, h/2, startAngle, arcAngle);
 
  // to draw a filled round rectangle use : g2d.fillRoundRect(x, y, w, h, arcWidth, arcHeight) instead
 
  g2d.drawRoundRect(x, y, w, h, w/2, h/2);
 
  Polygon polygon = new Polygon();
 
  polygon.addPoint(w/4, h/2);
 
  polygon.addPoint(0, h/2);
 
  polygon.addPoint(w/4, 3*h/4);
 
  polygon.addPoint(w/2, 3*h/4);
 
  // Add more points...
 
  // to draw a filled round rectangle use : g2d.fillPolygon(polygon) instead
 
  g2d.drawPolygon(polygon);
 
}
 
  }
 
}

This was an example on how to draw shapes in a Java Desktop Application.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button