Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2007

Tip: Looking for answers? Try searching our database.

Stepwise painting

Thread view: 
Jul - 15 Jun 2007 18:49 GMT
I have a class extending a JComponent. I override the
paintComponent(Graphics g) method to perform custom painting. Lets say
I want to stepwise paint some arbitrary shape, e.g. a square one side
at a time, side 1, 2, 3 and 4.

If I paint the square in one step the paintComponent method could look
like this - pseudo;

paintComponent(Graphics g){
  super.paintComponent(g);
  paint side 1, 2, 3 and 4;
}

Is there a way to perform a stepwise painting of this square, without
having to paint the already painted sides again;

paintComponent(Graphics g){
  super.paintComponent(g);
  paint side 1;
}
paintComponent(Graphics g){
  super.paintComponent(g);
  paint side 1, 2;
}
paintComponent(Graphics g){
  super.paintComponent(g);
  paint side 1, 2, 3;
}
paintComponent(Graphics g){
  super.paintComponent(g);
  paint side 1, 2, 3 and 4;
}
Knute Johnson - 17 Jun 2007 18:28 GMT
> I have a class extending a JComponent. I override the
> paintComponent(Graphics g) method to perform custom painting. Lets say
[quoted text clipped - 28 lines]
>    paint side 1, 2, 3 and 4;
> }

You want your painting method to repaint all of what there is at that
time to paint.  That way if you damage the window it will repaint
itself.  Use some sort of state variable to control what is painted at
any given time.

Some minor additions to the XComponent class that I showed you before.

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

public class XComponent extends JComponent implements Runnable {
    private volatile int sides = 0;

    public XComponent() {
        setPreferredSize(new Dimension(40,40));
    }

    public void run() {
        while (true) {
            if (++sides > 4)
                sides = 0;
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) { }
        }
    }

    public void paintComponent(Graphics g) {
        if (sides > 0)
            g.drawLine(10,10,30,10);
        if (sides > 1)
            g.drawLine(30,10,30,30);
        if (sides > 2)
            g.drawLine(30,30,10,30);
        if (sides > 3)
            g.drawLine(10,30,10,10);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                XComponent x = new XComponent();
                f.add(x);
                f.pack();
                f.setVisible(true);
                new Thread(x).start();
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 23 Jun 2007 12:19 GMT
>Is there a way to perform a stepwise painting of this square, without
>having to paint the already painted sides again;
I take it you want half a second or so between stages.

So you want some sort of timer than triggers your paint ever half
second.  You ran use the

repaint( int x, int y, int width, int height )  

feature to just ask your paint method to paint a piece (containing the
next segment).  If it paints more, that part outside the clipregion is
ignored. Then you just do the math, not all the pixel shuffling.

You will need SwingUtilities.invokeLater since your timer will not be
the Swing thread.

See http://mindprod.com/jgloss/swing.html
http://mindprod.com/jgloss/timer.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.