awt

Drawing components with color example

With this tutorial we shall show you how to draw Components with color. This is a very important operation when you want to liven up the graphics of you application, and customize them as you want.

Basically, all you have to do in order to draw components with color is:

  • Create a new Frame.
  • Create a class that extends Component class and override the paint method.
  • Create a new Graphics2D instance.
  • Use Color startColor = Color.red and Color endColor = Color.blue to apply gradient coloring.
  • Use GradientPaint to set up gradient coloring.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class DrawingColoredComponents {

  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 width = getSize().width-1;

  int height = getSize().height-1;

  // Apply gradient coloring

  Color startColor = Color.red;

  Color endColor = Color.blue;

  /**
    * A non-cyclic gradient. For cyclic gradient we could use 
    * GradientPaint(startX, startY, startColor, endX, endY, endColor, true)
    * For a predefined non gradient color we should use
    * g2d.setColor(Color)
    */

  GradientPaint gradient = new GradientPaint(x, y, startColor, width, height, endColor);

  g2d.setPaint(gradient);

  /**
    * 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.drawOval(x, y, width, height);

     }

  }
}

This was an example on how to draw components with color.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron 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
Inline Feedbacks
View all comments
Back to top button