Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

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

[an error occurred while processing this directive]

Java: JPanel - Drawing Area

To use a JPanel for graphics

  1. Define a new class that extends JPanel and define a paintComponent method in it.
  2. Create an instance of the class and add it to a panel.

Define a subclass of JPanel and override paintComponent()

Create a subclass of JPanel to override its paintComponent() method. It may be more convenient to define this class inside of your outer class so that it can access instance variables (fields) in the outer class. For example,

. . .
class MyDrawing extends JPanel {
    . . .  // Methods, constructors, fields.
    public void paintComponent(Graphics g) {
         super.paintComponent(g);    // paints background
         . . .  // do your drawing here
    }
}

super.paintComponent()

The first line in paintComponent() is usually a call to the parent class's paintComponent() method (super refers to the parent class). The call to the parent's method may be omitted if the entire background is drawn by your version of paintComponent() so that it appears opaque, but it's probably best to make the call to avoid what Sun calls "visual artifacts".

Override paintComponent() rather than paint()

You sometimes see code that overrides paint() instead of paintComponent(). This was necessary in the older AWT, however in Swing paint() calls three methods: paintComponent(), paintBorder(), and paintChildren. It is therefore better to override paintComponent() to allow borders, altho you will not notice a difference in most cases.

Using the new class on a layout

When you create your GUI (usually in a constructor) create a new drawing panel object and add it to a container (the content pane or another panel). For example (assuming content is a content pane or a panel).

    . . .
MyDrawing drawing = new MyDrawing();
content.add(drawing);

Repaint the panel after changes

Whenever the drawing needs to be changed because a value in the program has changed, call the panel's repaint() method. Often a listener (for a button, menu item, timer, ...) changes a value, then calls repaint(), which (eventually) calls your paintComponent() method. For example,

. . . // After anything changes that should appear in graphics
drawing.repaint();
Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  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

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

Copyright © 2007. All rights reserved.