dcsimg
October 1, 2016
Hot Topics:

Using Graphics in Java Applications

  • February 23, 2015
  • By Manoj Debnath
  • Send Email »
  • More Articles »

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.

Java1
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.

Graphics

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

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.red,
      Color.blue, Color.yellow, Color.black,
      Color.green, 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() {
      super();
   }

   public PieChart2(Color[] colors, double[] values,
         String[] labels) {
      super();
      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 });
         g2d.setPaint(rgp);
         g2d.fill(s);
         drawLabel(g2d, labels[i], startAngle + (arcAngle / 2));
         endAngle += values[i];
      }
      g.setColor(Color.gray);
      g.drawOval(cornerX, cornerY, diameter, diameter);
   }

   public void drawLabel(Graphics g, String text, double angle) {
      g.setFont(new Font(Font.DIALOG, Font.BOLD, 12));
      g.setColor(Color.black);
      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(),
            text);
      }
      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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(400, 400);
      f.add(new PieChart2(), BorderLayout.CENTER);
      f.setVisible(true);
   }
}

Java2
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 = ImageIO.read(new 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) {
         e.printStackTrace();
      }
   }

   public static void main(String args[]){
      convertToGrayscale("e:/temp/colorpic.jpg",
         "e:/temp/grayscalepic.jpg");
   }
}

Java3
Figure 3: Colored picture

Java4
Figure 4: Grayscale picture created from the colored picture

Conclusion

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.


Tags: Java, JavaFX, graphics, JVM, Swing, AWT, Java GUI




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel