Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 22-07-2009
Member
 
Join Date: May 2008
Posts: 24
Help: Java swing JPanel to repaint PaintComponent( Graphics g )

Hi,

I am not much experienced with swing in JAVA programming & hence I am learning it.
I want to know more about the swing Jpanel & paint component in detail.
OR is there any third party graphics I can use just like we do for C++?

Please direct me to a good & self explaining tutorial. SInce I have been facing problem with JPanel & repaint with a init function, am not able to print out paint component.

Thanks in advance.
Reply With Quote
  #2  
Old 22-07-2009
Member
 
Join Date: Apr 2008
Posts: 26
Re: Help: Java swing JPanel to repaint PaintComponent( Graphics g )

First thing you normally do in paintComponent is clear the off-screen bitmap via super.paintComponent.

Code:
public void paintComponent(Graphics g) {
  super.paintComponent(g);
  ...
}
Note that if you are using Swing in Java 1.2, you can cast the Graphics object to a Graphics2D object and do all sorts of new stuff added in the Java2D package. Graphics2D is an abstract subclass of class Graphics. actual object used to draw in every paintComponent method is an instance of a subclass of Graphics2D that is passed to method paintComponent and accessed via the superclass Graphics.
Reply With Quote
  #3  
Old 22-07-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Help: Java swing JPanel to repaint PaintComponent( Graphics g )

Swing Graphics

To do custom graphics in an applet or application, you will almost always write a new class that extends the JPanel class. In that class, you override the definition of the paintComponent() method. This method should have the following form.

Code:
 public void paintComponent(Graphics g) {
	super.paintComponent(g);
	drawing messages sent to g
    }
if the drawings need to be updated while the applet or application is running then you need to invoke the following method.

void repaint() This method can be invoked from within the JPanel subclass or from another object.

A Graphics object controls the visual appearance of a Swing component. Most Swing components have a built-in appearance, but a JPanel has none except for a uniform background color. This makes JPanel ideal for custom graphics.

To use the Graphics2D capabilities in a JPanel subclass, you need to have a variable of type Graphics2D. This can be obtained by coercing the paint method parameter. For example, you can modify the paintComponent() method in the above template as follows.

Code:
public void paintComponent(Graphics g) {
	super.paintComponent(g);
	Graphics2D g2d = (Graphics2D)g;
	drawing messages sent to g2d
    }
g and g2d actually refer to the same object, which is an instance of the Graphics2D class. They differ only in the variable types, which determines the operations allowed by a Java compiler.
Reply With Quote
  #4  
Old 22-07-2009
Member
 
Join Date: Apr 2008
Posts: 53
Re: Help: Java swing JPanel to repaint PaintComponent( Graphics g )

JPanel: paintComponent(Graphics g)

Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  Color color;

  public MainClass(Color color) {
    this.color = color;
  }

  public void paintComponent(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(color);
    g.drawOval(0, 0, width, height);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Oval Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(2, 2));

    Color colors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
    for (int i = 0; i < 4; i++) {
      MainClass panel = new MainClass(colors[i]);
      frame.add(panel);
    }

    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}
Have a look at above sample code:
Reply With Quote
  #5  
Old 22-07-2009
Member
 
Join Date: Aug 2008
Posts: 90
Re: Help: Java swing JPanel to repaint PaintComponent( Graphics g )

Have a look at this Java Note:
Calling paintComponent

When a listener (mouse, button, keyboard, ...) of yours is called, the listener code often makse changes that should be displayed in your graphics area. Never call paintComponent() method directly.

1. Change instance variables.
The listener code should set instance variables that paintComponent() uses in drawing the panel. After changing the values, the next time paintComponent() is called, these new values will be used. But you won't want to wait for a call to paintComponent(), call repaint().

2. Call repaint()
The repaint() method consolidates all requests to change the panel (there may be several repaint requests between screen refreshes). It adds an update request to the GUI event queue so that the update will be properly coordinated with other GUI actions (Swing and AWT are not thread-safe). This update request, when processed, calls update(), which calls paint(), which calls your paintComponent() method (as well as calling paintBorder() and paintChildren(). Along the way update() redrew your background.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Help: Java swing JPanel to repaint PaintComponent( Graphics g )"
Thread Thread Starter Forum Replies Last Post
JPanel class of java Ram Bharose Software Development 4 16-02-2010 11:01 AM
Java Swing and MySQL shelton141 Software Development 1 22-09-2009 08:50 AM
drawPolygon in java Swing? JagdishP Software Development 3 07-08-2009 11:33 PM
Message Box example for Java Swing Visala28 Software Development 3 31-07-2009 05:30 PM
Java swing help with repaint() grudge Software Development 4 23-07-2009 03:41 PM


All times are GMT +5.5. The time now is 05:35 PM.