Hello,
I want to use the same code to draw graphs just with different input so
I have the following code:
showgraph sg1 = new showgraph("AAAA",tp,x[n], y[n]);
showgraph sg2 = new showgraph("BBBBB",tp,x[n+1], y[n+1]);
public showgraph(String text, JPanel tp, int x, int y)
{
JPanel pan = new graphpanel(text);
setClosable(true);
setPreferredSize(new
java.awt.Dimension(globals.smallFrameX,globals.smallFrameY));
pan.setBackground(new java.awt.Color(255, 255, 255));
pan.setForeground(new java.awt.Color(51, 51, 51));
getContentPane().add(pan, java.awt.BorderLayout.CENTER);
pack();
this.setVisible(true);
tp.add(this, new org.netbeans.lib.awtextra.AbsoluteConstraints(x,
y, globals.smallFrameX, globals.smallFrameY ) );
this.setTitle(title);
this.setLocation(x,y);
this.toFront();
}
public class graphpanel extends JPanel{
public static String data;
public graphpanel(String str)
{
int xpan = this.getWidth();
int ypan = this.getHeight();
System.out.println(xpan +" - "+ypan);
data=str;
}//graphpanel
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int xpan1 = this.getWidth();
int ypan1 = this.getHeight();
System.out.println(xpan1 +" - "+ypan1);
g.drawString( data, 10, 10 );
}
Now when I run the code it will produce 2 windows (internal frames as
expected) but both will have the same content "AAAA". What did I do
wrong.
Furthermore, Iäd like to have as much time consuming code in the
graphpanel method instead of the paintComponent method, but it seems
that the witdh and height (xpan and ypan) isn't known in the moment
when graphpanel is called. I guess it is due to the fact that the
pack() in showgraph is called afterwords.
How can I resolve this issues?
Andrew Thompson - 31 Oct 2005 17:04 GMT
> tp.add(this, new org.netbeans.lib.awtextra.AbsoluteConstraints(x,
> y, globals.smallFrameX, globals.smallFrameY ) );
...
> Now when I run the code it will produce 2 windows (internal frames as
> expected) but both will have the same content "AAAA". What did I do
> wrong.
It probably has something to do with that netbeans layout,
but I doubt you will find much help with your IDE here.
Try learning and using the Sun core layouts, and if you
have problems, I am sure someone here will be able to
help you.
A tip for when posting code as well. Your layout problem had
little or nothing to do with Graphs, especially if you can
replace those graphs with JLables and get the same effect.
If you prepare an SSCCE*, it helps you track down exactly
which parts of code are relevant to a problem.
* <http://www.physci.org/codes/sscce.jsp>
gbgkille69@spray.se - 31 Oct 2005 19:20 GMT
Andrew Thompson skrev:
> > tp.add(this, new org.netbeans.lib.awtextra.AbsoluteConstraints(x,
> > y, globals.smallFrameX, globals.smallFrameY ) );
[quoted text clipped - 17 lines]
> which parts of code are relevant to a problem.
> * <http://www.physci.org/codes/sscce.jsp>
Thanks I don't think it is a layout problem, I've the same effect when
using flowlayout and/or null layout. I used the abstract layout as this
is the simplest and best looking layout for my needs within Netbeans.
My actual application is much bigger and does draw graphs. but to keep
it simple here i removed everything except a drawstring line.
Nigel Wade - 01 Nov 2005 10:24 GMT
> Hello,
>
[quoted text clipped - 25 lines]
>
> public class graphpanel extends JPanel{
This isn't right:
> public static String data;
it will give you one String shared between all graphpanel objects.
> public graphpanel(String str)
> {
[quoted text clipped - 17 lines]
> expected) but both will have the same content "AAAA". What did I do
> wrong.
I would have expected both to show "BBBBBB" rather than "AAAA".

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
gbgkille69@spray.se - 01 Nov 2005 11:52 GMT
yes you're right, it's "BBBB" not "AAAA". my mistake.
paramtere data so that it isn't shared?
just for my understanding, why is it shared att all? I thought by
calling like this
showgraph sg1 = new showgraph("AAAA",tp,x[n], y[n]);
showgraph sg2 = new showgraph("BBBBB",tp,x[n+1], y[n+1]);
I would create two completey indipendent and uniq objects?
Nigel Wade - 02 Nov 2005 11:00 GMT
> yes you're right, it's "BBBB" not "AAAA". my mistake.
> paramtere data so that it isn't shared?
[quoted text clipped - 4 lines]
> showgraph sg2 = new showgraph("BBBBB",tp,x[n+1], y[n+1]);
> I would create two completey indipendent and uniq objects?
It creates two objects of type showgraph, each of which contains a unique object
of type graphpanel. In graphpanel the string data is declared static which
means there is only one copy for the class, and it is shared by every object.
Each time you create a new object of type graphpanel you change the value of
data for the class, and this is reflected in every object of type graphpanel
already created.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
gbgkille69@spray.se - 03 Nov 2005 13:28 GMT