Home Java Example Java Swing Drawing with Color in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Drawing with Color in Java

Advertisement
In this section, you will see how to draw colorful shapes in java swing.

Drawing with Color in Java

     

In this section, you will see how to draw colorful shapes in java swing. There are various colorful shapes have been drawn in the given program. This program has used various types of methods to draw shapes and fill these with the appropriate color. Following are some methods are using in the given program to complete the required result. Pictorial representation for the result is given below:

Colorfull Shapes in Swing Application

drawRect():
This is the method of the Graphics class (The Graphics class is used to drawing different-different type of shapes). This method draws the rectangle. It takes some integer value as parameter. This method is written like : Graphics.drawRect(x, y, height, width);.

x - This is the variable represents the row no. or the x - coordinate.
y - This is also a variable represents the column no. or the y - coordinate.

drawOval():
This is the method of the Graphics class which draws the oval on the frame. This method takes argument same as the drawRect() method. In this method first come the width and then height is specified.

fillRect():
This is the method of the Graphics class which is used to fill rectangle with the specified color which is set before using the setColor() method of the Graphics class. It also takes argument same as the drawRect() method.

fillOval():
This is also the method of the Graphics class which is used to fill the oval with color specified in the setColor() method before. This method also takes argument same as the drawOval() method.

Here is the code of program:

import javax.swing.*;
import java.awt.*;

public class DrawingColor{
  public static void main(String[] args) {
  DrawingColor d = new DrawingColor();
  }

  public DrawingColor(){
  JFrame frame = new JFrame("Drawing with Alpha");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(new MyComponent());
  frame.setSize(400,400);
  frame.setVisible(true);  
  }

  public class MyComponent extends JComponent{
  public void paint(Graphics g){
  int height = 200;
  int width = 120;
  g.setColor(Color.red);
  g.drawRect(10,10,height,width);
  g.setColor(Color.gray);
  g.fillRect(11,11,height,width)
  g.setColor(Color.red);
  g.drawOval(250,20, height,width);
  g.setColor(Color.magenta);
  g.fillOval(249,19,height,width)
  }
  }
}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Ask Questions?    Discuss: Drawing with Color in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Bill
May 10, 2012
specific rgb values

how do you set a shape to a specific rgb value instead of things like "Magenta" and "red" ?
Loranne Xuereb
July 24, 2012
Program

Very good and easy to understand program
savgate.com
November 5, 2012
greet

thx nice! a sample for random color change?
k.brinda
August 14, 2013
java program

pls,tell "graphices program in 2d shapes with frame,panel,font,sytle,image".
DMCA.com