Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

I am currently trying to program Java Swing drawing for a flag program so the user types a flag name and the image pops up, for the image to pop up i need to use Java Swing drawing to hand draw my flag, i know how to get the three rectangle of the three colors orange, white and green and also the circle in the center but i don't know how to make the Java Swing drawing make it segregate the circle in two 24 compartments.

Posted by Aravind Ranganathan on Wednesday, 10.29.08 @ 23:12pm | #81400

Latest Searches:
list box from database
text color
jstl list size
placeholder
create file folder
drill down
Tomahawk commandLink t
jpanel
struts2 checkbox
retrieving data from h
Html tag inside out im
regularexpression
jsf validator
hibernate findall
Icon
Telephony
code to add and retriv
Flash Navigation Drop
diffreence between ser
breakั?�??????ั?
how to configure msacc
s:If Struts2
access bean properties
'string variable in sw
Reading File into Byte
jsf node set href
jsp bean get property
javascript connect db
sun.jdbc.odbc.JdbcOdbc
JDBC with Mysql in Ecl
Applet event button
Insert Data into Datab
JTree Open Icon
concatenate java
clear the console in j
for loop
download struts applic
jasper report
java display applet me
servlet exception hand
code for how connect j
struts-config
java program examples
jsp to jdbc codes
<SELECT> and Post
ล ยà¸?ฤ??????à¸
getter setter methods
c:out in var of c:fore
upload file using ajax
Flex TreeGrid
Photoshop Drawing Penc
Quartz
Java / multi thread
ASP Ad Management Crea
insert data to text fi
example
create a tree to be di
WHERE Statement in SQL
factory method
username and password
Photoshop Animation An
java jtable
permutation
JDBC with Mysql in Ecl
multiple submit button
Button
data ware
oracle.jdbc.driver.Dat
server side validation
draw Polyline
web.xml file
convert string to stin
org.gjt.mm.mysql.Drive
compare java.util.date
struts formfile
JavaWorld article abou
Photoshop Basics Using
logout session
system.out.println(x
data binding
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.