Using Graphics in Java Applications

Monday Feb 23rd 2015 by Manoj Debnath

Explore the Java GUI in general and work with a couple of practical graphics implementations.

Every window-based application we use is realized with smart graphics in various forms and manners. The implication is obvious, as the name suggests: GUI (Graphical User Interface). The core Java desktop library elements, such as AWT, Swing, and Graphics are examples of brilliant interplay of graphics in action. The idea is to make the computer user friendly by providing visual realism to our interaction with this dumb machine. Imagine that you are pointing a finger (mouse pointer) to direct, rather than typing a command. The man-machine interaction got a paradigm shift, not only by visual appeal but also in the sense of virtual touch. The article focuses on the Java GUI in general and a couple of practical graphics implementations.

Graphics APIs

Swing, AWT (Abstract Window Toolkit), and Graphics run on top of the JVM (Java Virtual Machine), along with other Java libraries. This core trio is responsible for creating windows, user interface components, and graphics and painting them on a computer screen in such a manner that the user applications do not get a hint of window system APIs of the underlying native platform.

Figure 1: Java GUI block diagram

Abstract Window Toolkit (AWT)

Java's first full-fledged GUI library was used to create GUI components for user interface programming. AWT closely works with the native libraries of the underlying platform to create and display graphical components. The library is quite heavy due to coercing with the native API library and inflexible due to compromising Java's philosophy of least native API influence.


While AWT fiddles with the graphical components, Graphics goes to the core and covers a broad set of operations, including basic and advanced drawing operations, image manipulation, text manipulation, and printing. Swing components look like Swing because they are drawn and rendered with the help of the Graphics library. Graphics2D is an extension of this library and takes the core APIs further, thus leveraging its flexibility to a new level.


Swing is lightweight because the GUI components in Swing do not correspond to the native components as they do it in AWT. They are drawn using Graphics from scratch; therefore, an interesting aspect of the Swing GUI is that they can be further extended and customized in several interesting ways. The customized look and feel we find in Swing is this simple example of its flexibility.

Creating Graphical Objects

Creating objects and shapes are some of the basic graphical utilities provided by Java, such as drawing lines, arcs, and so on. These simple utilities can be manipulated to draw meaningful objects. Charts and graphs are an excellent way to present and explain data figuratively. They can not only make a dull presentation of numbers aesthetically pleasing but also give a comprehensive picture of the idea at a glance. We can create a pie chart from scratch very easily by using the raw graphics primitive method with a slight use of basic mathematics as follows. (JavaFX provides some dedicated class to create charts; see an example application: Combining JavaFX and ...).

public class PieChart2 extends JPanel {
   private int originX, originY;
   private int radius;
   private static double d2r = Math.PI / 180.0;
   private Color colors[] = new Color[] {,, Color.yellow,,, Color.white, Color.gray,
      Color.cyan, Color.magenta, Color.darkGray };
   private double values[] = { 34, 56, 22, 78, 11, 89,
      22, 1 };
   private String labels[] = { "val1", "val2", "val3",
      "val4", "val5", "val6", "val7", "val8" };

   public PieChart2() {

   public PieChart2(Color[] colors, double[] values,
         String[] labels) {
      this.colors = colors;
      this.values = values;
      this.labels = labels;

   public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      Dimension size = this.getSize();
      originX = size.width / 2;
      originY = size.height / 2;
      int diameter = (originX < originY ?
         size.width - 40 : size.height - 40);
      radius = (diameter / 2) + 1;
      int cornerX = (originX - (diameter / 2));
      int cornerY = (originY - (diameter / 2));
      int startAngle = 0;
      double endAngle = 0.0;
      int arcAngle = 0;

      double sum = 0;

      for (int i = 0; i < values.length; i++)
         sum += values[i];

      for (int i = 0; i < values.length; i++) {
         startAngle = (int) (endAngle * 360 / sum);
         arcAngle = (int) (values[i] * 360 / sum);
         g.setColor(colors[i % colors.length]);
         Shape s = new Arc2D.Double(cornerX, cornerY, diameter,
            diameter, startAngle, arcAngle, Arc2D.PIE);
         RadialGradientPaint rgp =
               new RadialGradientPaint(new Point(
            getWidth() / 2, getHeight() / 2),
            diameter, new float[] {0f, 1f },
            new Color[] { colors[i], Color.gray });
         drawLabel(g2d, labels[i], startAngle + (arcAngle / 2));
         endAngle += values[i];
      g.drawOval(cornerX, cornerY, diameter, diameter);

   public void drawLabel(Graphics g, String text, double angle) {
      g.setFont(new Font(Font.DIALOG, Font.BOLD, 12));
      double radians = angle * d2r;
      int x = (int) ((radius + 5) * Math.cos(radians));
      int y = (int) ((radius + 5) * Math.sin(radians));
      if (x < 0) {
         x -= SwingUtilities.computeStringWidth(g.getFontMetrics(),
      if (y < 0) {
         y -= g.getFontMetrics().getHeight();
      g.drawString(text, x + originX, originY - y);

   public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setSize(400, 400);
      f.add(new PieChart2(), BorderLayout.CENTER);

Figure 2: Pie chart with dummy data

Manipulating Images

Images play an important role when working with graphics in an application. Java provides some APIs to manipulate images. The core API library, though, is not sufficient for an extensive image processing capability, yet it can be used to realize image processing to a good extent. Several third-party libraries, such as OpenCV and ImageJ, are built with a view to leverage Java with image processing functions. The following example is a simple code to convert a colored JPEG image to an equivalent grayscale image.

public class ColorToGrayscale {

   public static void convertToGrayscale(String fromFile,
         String toFile) {
      BufferedImage image = null;
      try {
         image = File(fromFile));
         for (int i = 0; i < image.getHeight(); i++) {
            for (int j = 0; j < image.getWidth(); j++) {
               Color imageColor = new Color(image.getRGB(j, i));
               int rgb = (int) (imageColor.getRed() * 0.299)
                  + (int) (imageColor.getGreen() * 0.587)
                  + (int) (imageColor.getBlue() * 0.114);
               Color newColor = new Color(rgb, rgb, rgb);
               image.setRGB(j, i, newColor.getRGB());

         ImageIO.write(image, "jpg", new File(toFile));
      } catch (IOException e) {

   public static void main(String args[]){

Figure 3: Colored picture

Figure 4: Grayscale picture created from the colored picture


The use of graphics in an application is limited only by one's imagination. It can be extensible and used to present data graphically, to make it more interesting, create customized GUI components adhering to the need of the user application, applying image processing capability, and so forth. In most cases, built-in graphical components are sufficient for our needs, but there are instances where a customized component may be the exact need of the application. Java aptly provides the tools that we can use, either to create one from scratch or extend the existing elements.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved