|
-
October 1st, 1999, 05:25 AM
#1
Plot class - update problem...
Hiya all,
This is probably a really simple question, but I've looked in books, posted questions etc and so far none the wiser. Basically what I'm trying to do, is to write a class which creates a JFrame with a JPanel in it. Then I want a set of methods which the main code can call, which places plot points at the designated co-ordinates.
As an example, I might want to do this (in the main code);
PlotFrame firstPanel = new PlotFrame(200, 300, "First window");
PlotFrame secondPanel = new PlotFrame(200, 300, "First window");
..... some code or other which creates, manipulates a large set of data ....
firstPanel.plotPoint(x, y, color);
firstPanel.plotPoint(x, y, other_color);
secondPanel.plotPoint(x, y, yet_other_color);
etc
I can create the window no probs (with different sizes, colors etc etc), but once I've plotted the first point, anynew point will "delete" the old plots once the panel is redrawn?!? I have tried with only redrawing the actual plotpoint, but if there are lines crossing over etc, this wont work anyway.
Please, any hints/tips or pointers are very much appreciated. This is driving me mad 
Cheers and best regards
Thomas
-
October 1st, 1999, 08:03 AM
#2
Re: Plot class - update problem...
override update method in your frame and call paint in it
public void update( Graphics g ){
paint( g );
}
Basic functionality of update is ,
. Clears this component by filling it with the background color.
· Sets the color of the graphics context to be the foreground color of this component.
· Calls this component's paint method to completely redraw this component.
You dont need to do the first step .. Here below is the default implementation ..
public void update(Graphics g) {
if ((this instanceof java.awt.Canvas) ||
(this instanceof java.awt.Panel) ||
(this instanceof java.awt.Frame) ||
(this instanceof java.awt.Dialog) ||
(this instanceof java.awt.Window)) {
g.clearRect(0, 0, width, height);
}
paint(g);
}
It's clearing the frame whenever repaint is called.
-
October 5th, 1999, 03:25 AM
#3
Re: Plot class - update problem...
Cheers for the reply. I did what was suggested, however it made no diff. After this hint, I furthered my research into the API, and found that the JFrame/JPanel update is alread over-riding the original update call. It only calls paint, and doesnt do the g.clearRect.
I would be happy to post the example code I'm trying to get working if anyone would like to have a look at it. I'm sure its v basic to get working, but its driving me potty. Someone has suggested I have to create an Image (off screen) and draw all my gfx's on this, and then override the paint method so it displays the off-screen Image?!?
Cheers again
Thomas
-
October 5th, 1999, 10:54 AM
#4
Re: Plot class - update problem...
post your code... Thanks for the point...
-
October 5th, 1999, 11:22 AM
#5
Re: Plot class - update problem...
Hiya,
Cheers for looking at the code. Sorry its a little long, but it shows the exact example I'm fighting with. Should be so simple.... 
Cheers
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GraphPanel extends JPanel
{ public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.setColor(Color.blue);
g.drawPolyline(xPoints, yPoints, num);
}
public void drawPolyline(int[] in_xPoints, int[] in_yPoints, int in_num)
{ xPoints = in_xPoints;
yPoints = in_yPoints;
num = in_num;
repaint();
}
private int[] xPoints;
private int[] yPoints;
private int num;
}
class GraphFrame extends JFrame
{ public GraphFrame(int inSizeX, int inSizeY, String inWinName)
{ setTitle(inWinName);
setSize(inSizeX, inSizeY);
setBackground(Color.white);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0); }
} );
Container contentPane = getContentPane();
plotPanel = new GraphPanel();
plotPanel.setBackground(Color.white);
contentPane.add(plotPanel);
}
public void drawPolyline(int[] xPoints, int[] yPoints, int num)
{ plotPanel.drawPolyline(xPoints, yPoints, num);
}
private GraphPanel plotPanel;
}
public class Test
{ public static void main(String args[])
{ String winName = "Test Plot";
GraphFrame brPlot = new GraphFrame(250, 150, winName);
brPlot.show();
drawStuff(brPlot);
}
public static void drawStuff(GraphFrame brPlot)
{ int counter;
int[] xPoints = new int[100];
int[] yPoints = new int[100];
for(counter=0;counter<100;counter++) {
xPoints[counter] = counter;
yPoints[counter] = 50;
}
brPlot.drawPolyline(xPoints,yPoints,100);
brPlot.drawPolyline(yPoints,xPoints,100);
}
}
-
October 5th, 1999, 04:25 PM
#6
Re: Plot class - update problem...
Instead of calling repaint() , call paintImmediately method in your panel class..
public void drawPolyline(int[] in_xPoints, int[] in_yPoints, int in_num)
{
xPoints = in_xPoints;
yPoints = in_yPoints;
num = in_num;
Rectangle rect = this.get;
paintImmediately( this.getBounds().x , this.getBounds().y , this.getBounds().width , this.getBounds().height );
}
for more information , check this post...
http://codeguru.developer.com/bbs/wt...collapsed&sb=5
-
October 5th, 1999, 04:40 PM
#7
Re: Plot class - update problem...
And one more thing , dont call super.paint() in your panel's paint() method ( I used paint method
insteadof paintComponent method )
public void paint( Graphics g ){
// super.paint( g );
g.setColor(Color.blue);
g.drawPolyline(xPoints, yPoints, num);
}
-
October 6th, 1999, 03:02 AM
#8
Re: Plot class - update problem...
Morning,
Thanks a lot for the help! It all works beautifully now Have just used this code to update my actual programm, and things are looking bright. Not sure if I would have ever gotten to this. Goes to show that book and api's in all their glory, sometime a little help goes a heck of a long way further!
Thanks again!
Thomas
-
October 6th, 1999, 07:54 AM
#9
Re: Plot class - update problem...
You are always welcome.. Wow!!! 6 points ... Thanks for it..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|