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 / December 2003

Tip: Looking for answers? Try searching our database.

How to keep JPanel bgcolor during plotting?

Thread view: 
hiwa - 18 Dec 2003 06:53 GMT
When we want to keep the initial background color of a panel for a
plotting application, we do simply the following for java.awt.Panel:
<code>
public class PlotPanel extends Panel{
 int oldX, oldY, newX, newY;

 public PlotPanel(){ ...
   setBackground(Color.white);
   ...
 }

 public void update(Graphics g){ //suppress bg redrawing
   g.setColor(Color.blue); //pen color
   paint(g);
 }

 public void paint(Graphis g){
   g.drawLine(oldX, oldY, newX, newY); //one stroke of plotting
 }
 .....
}
</code>
But, when we use a JPanel instead of Panel for a plotting canvas,
suddenly things become complicated and I have not found the solution
yet after struggling almost a full day. Let me repeat the problem:
how could we keep the initial background color for plotting on a
JPanel? ...I don't override update() method for JPanel version
of the PlotPanel class during the struggle because the spec of
the method is different for JComponent from awt.Component.
Andrew Thompson - 18 Dec 2003 07:38 GMT
> When we want to keep the initial background color of a panel for a
> plotting application, we do simply the following for java.awt.Panel:
> <code>

Try this for pointers..
http://www.physci.org/test/AnimatePanel/

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 18 Dec 2003 10:51 GMT
> > When we want to keep the initial background color of a panel for a
> > plotting application, we do simply the following for java.awt.Panel:
[quoted text clipped - 4 lines]
>
> HTH
Thanks Andy. But ...
<code>
   public void paintComponent(Graphics g)
   {
       super.paintComponent(g);
       for (int ii=0; ii<balls.length; ii++)
       {
           Ball b = balls[ii];
           b.draw(g);
       }
   }
</code>
This code from JAnimateFrame.java is effective for animation, not for
plotting because super.paintComponent(g) erases the previously drawn
lines all together. For animation, this behavior is one of the
principal requirements but is not for plotting.

Could you give another help?
Andrew Thompson - 18 Dec 2003 11:39 GMT
> > > When we want to keep the initial background color of a panel for a
> > > plotting application, we do simply the following for java.awt.Panel:
[quoted text clipped - 5 lines]
> > HTH
> Thanks Andy. But ...

My name is not Andy.

> <code>
...
> Could you give another help?

I'll.. think about it.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 19 Dec 2003 01:16 GMT
> My name is not Andy.
>
[quoted text clipped - 3 lines]
>
> I'll.. think about it.
Hi And<B>rew</B>,
Sorry for having used a wrong name!

Currently, my not-so-happy solution is as follows:
<code>
 public void paintComponent(Graphics g){

   g.drawImage(bufImg, 0, 0, this);
 }

 public void doPlot(){
   Graphics g = null;
   while (! stop){
     if (begin){
       bufImg = createImage(getSize().width, getSize().height);
       g = bufImg.getGraphics();
       g.setColor(Color.white); //make white canvas
       g.fillRect(0, 0, getSize().width, getSize().height);
       begin = false;
     }
     else{
       prevX = newX; prevY = newY;
       newX = getNewx();
       newY = getNewy();
       g.setColor(Color.blue);
       // draw into the Image
       g.drawLine(prevX, prevY, newX, newY);
     }  
     repaint();
     try{
       Thread.sleep(1000); //pause
     }
     catch(Exception e){
       e.printStackTrace();
     }
   }
 }
</code>
I miss simpler one.
Andrew Thompson - 19 Dec 2003 02:44 GMT
> > My name is not Andy.
> >
[quoted text clipped - 5 lines]
> Hi And<B>rew</B>,
> Sorry for having used a wrong name!

G'Day hiwa        ;-)

> Currently, my not-so-happy solution is as follows:
> <code>
[quoted text clipped - 21 lines]
>         g.drawLine(prevX, prevY, newX, newY);
>       }

I have an admission to make.  I never looked at
your initital code snippet.  (till this instant)

I do not spend many moments of my life looking
at code snippets, on prinicipal, as well as for
practical reasons.

In this situation, there was even less point,
since I already knew how to achieve what
you wanted.

Now I realise that _you_ actually knew how to
achieve the effect from the start (as your initial
code 'snippet' seems to imply), but were just
unhappy with the solution.

The only real further help I can be here is to say
..get over it.  Simply draw the grid after the BG
of the panel has been painted, but before
drawing _your_ own objects.

(shrugs) Hey, anybody else listening feel free to
jump in and prove me wrong, but that seems the
way to do it.

[  BTW - I have upgraded my own to draw a
simple grid as well.  Source is linked from the page.
http://www.physci.org/test/AnimatePanel/ ]

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 19 Dec 2003 10:05 GMT
> Now I realise that _you_ actually knew how to
> achieve the effect from the start (as your initial
> code 'snippet' seems to imply), but were just
> unhappy with the solution.
I feel plotting into an Image object is kinda rather tall talk and I
hate it. As I'd like to have simpler code which might be on a par with
the plotting on a java.awt.Panel, I had posted the initial post. I
want to do plotting on a Swing component more 'naturally'.

> [  BTW - I have upgraded my own to draw a
> simple grid as well.  Source is linked from the page.
> http://www.physci.org/test/AnimatePanel/ ]
Your grid is a beautiful tour de force. But it is still an animation
done in a single for loop in a single invocation of the
paintComponent() method. Plotting can also be done by calling
Graphics#drawPolyine() in a single paint. But my requirement is
realtime update of the x/y coords.


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



©2009 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.