Results 1 to 5 of 5

Thread: Help: Java swing JPanel to repaint PaintComponent( Graphics g )

  1. #1
    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.

  2. #2
    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.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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.

  4. #4
    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:

  5. #5
    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.

Similar Threads

  1. JPanel class of java
    By Ram Bharose in forum Software Development
    Replies: 4
    Last Post: 16-02-2010, 11:01 AM
  2. Java Swing and MySQL
    By shelton141 in forum Software Development
    Replies: 1
    Last Post: 22-09-2009, 08:50 AM
  3. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  4. Message Box example for Java Swing
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 05:30 PM
  5. Java swing help with repaint()
    By grudge in forum Software Development
    Replies: 4
    Last Post: 23-07-2009, 03:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,627,027.97672 seconds with 17 queries