 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andy Guest
|
Posted: Fri Mar 05, 2004 12:57 am Post subject: linking images to other components |
|
|
OK I finally know what I want now.
I have one class that extends JFrame and includes a variety of Swing
components, one of which is a JTextArea, another of which is a JPanel
containing an Image (maybe it's not necessary or desirable to load it
on to a JPanel - suggestions welcome).
However it is done though I need to maintain equality between the
image size and the JTextArea size (when the container frame is
expanded). I tried to do it by passing a Dimension parameter through
to a class that makes the JPanel. This didn't work and I think it's
because the program doesn't 'know' the size of the JTextArea before it
creates the image panel (it thinks that both height and width are 0).
Here's the code, any suggestions are welcome.
//Picture class - draws image of given size and puts it on to JPanel
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.*;
public class Picture extends JPanel
{
private String fileName;
private Image myImage, myScaledImage;
private Dimension imgSize;
/** Creates a new instance of Picture */
public Picture(String file, Dimension size)
{
fileName=file;
myImage=new ImageIcon(fileName).getImage();
imgSize=size;
}
public void paint(Graphics g)
{
super.paintComponent(g);
if(fileName!=null)
{
int width, height;
if(imgSize.width==0 || imgSize.height==0)
{
width=200; height=200;
}
else
{
width=imgSize.width;
height=imgSize.height;
}
myScaledImage=myImage.getScaledInstance(width, height,
myImage.SCALE_SMOOTH);
g.drawImage(myScaledImage,0,0,this);
}
else
g.drawString("Image not available",60,90);
}
}
//this is the snippet of code from the class that builds the JFrame -
mid is a
//class variable
mid=new JPanel(new GridLayout(1,2,20,0));
mid.add(description);
picPanel=toCheck.getPicture(description.getSize());
mid.add(picPanel);
//the getPicture method refers to this one here
public JPanel getPicture(Dimension size)
{
Picture itemPic=new Picture(fileName, size);
return itemPic;
}
Once again, any comments are welcome, sorry for being such an
incompetant.
Andy
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Fri Mar 05, 2004 1:27 am Post subject: Re: linking images to other components |
|
|
On 4 Mar 2004 16:57:14 -0800, Andy wrote:
| Quote: | //Picture class - draws image of given size and puts it on to JPanel
import java.awt.*;
|
I was with you right up to the point
that my compiler coomplained about this
line (which had wrapped)
| Quote: | //this is the snippet of code from the class that builds the JFrame -
|
<http://www.physci.org/codes/sscce.jsp>
--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
Thomas Weidenfeller Guest
|
Posted: Fri Mar 05, 2004 8:11 am Post subject: Re: linking images to other components |
|
|
Andy wrote:
| Quote: | containing an Image (maybe it's not necessary or desirable to load it
on to a JPanel - suggestions welcome).
|
It work, but if you have no special image processing needs, you could
also just use a JLabel with an ImageIcon.
| Quote: | However it is done though I need to maintain equality between the
image size and the JTextArea size (when the container frame is
expanded).
|
Just leave the resizing to the layout manager.
| Quote: | public void paint(Graphics g)
{
super.paintComponent(g);
|
Well ... You want to think about that one a little bit.
/Thomas
|
|
| Back to top |
|
 |
Andy Guest
|
Posted: Sun Mar 07, 2004 11:11 am Post subject: Re: linking images to other components |
|
|
| Quote: | //Picture class - draws image of given size and puts it on to JPanel
import java.awt.*;
I was with you right up to the point
that my compiler coomplained about this
line (which had wrapped)
//this is the snippet of code from the class that builds the JFrame -
|
There was other stuff in the message that wouldn't have compiled
either becuase it was only part of the program.
I went here and followed your advice. I also tried to solve the
problem using JLabel instead of a custom Picture class (much easier);
JTextArea sameSizeAsImage = new JTextArea();
int width=sameSizeAsImage.getWidth();
int height=sameSizeAsImage.getHeight();
JLabel imageLabel=new JLabel(new ImageIcon(picFile.gif));
imageLabel.setPreferredSize(width,height);
I gather this would work providing I use an appropriate LayoutManager
(i.e. one that listens to setPreferredSize().
| Quote: | "Very few things are as tempting as a link to the problem"
|
I know you didn't write this in the message but it's a quote from your
site. So here's a link to the problem
<http://www.geocities.com/andyfaeglasgow/Projects.html>
It's not an applet so to see the application download the source code
for "Content Management System"
Regards,
Andy
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Sun Mar 07, 2004 2:12 pm Post subject: Re: linking images to other components |
|
|
On 7 Mar 2004 03:11:00 -0800, Andy wrote:
| Quote: | //Picture class - draws image of given size and puts it on to JPanel
import java.awt.*;
I was with you right up to the point
that my compiler coomplained about this
line (which had wrapped)
....
http://www.physci.org/codes/sscce.jsp
I went here and followed your advice.
|
Well, kinda'.. *
| Quote: | I also tried to solve the
problem using JLabel instead of a custom Picture class (much easier);
JTextArea sameSizeAsImage = new JTextArea();
int width=sameSizeAsImage.getWidth();
int height=sameSizeAsImage.getHeight();
JLabel imageLabel=new JLabel(new ImageIcon(picFile.gif));
imageLabel.setPreferredSize(width,height);
I gather this would work providing I use an appropriate LayoutManager
(i.e. one that listens to setPreferredSize().
"Very few things are as tempting as a link to the problem"
I know you didn't write this in the message but it's a quote from your
site. So here's a link to the problem
http://www.geocities.com/andyfaeglasgow/Projects.html
|
Why on Earth did you include the
215Kb mysql-connector jar?
Not only was it not necessary, but your
GUI actually comes up if I rename or
remove it. Why cause people who are
trying to help, an extra and unnecessary
download?
OK, that aside, your code now compiles
cleanly, but it still has too much dross
attached to expect other to understand your
layout. Simplify it futher so that _only_
the components necessary to reproduce the problem
are present, detach all other classes
from ContentManager, put bright borders on
each and every component and panel so that
they can be easily distinguished.
Now that I have got that out, if you want
the second JTextArea labeled 'description'
to be the same size as the panel to it's right,
a GridLayout will do it..
Of course, if you have already put each
component in a larger, more complex
Layout, you will need to extract them from
it before you go further.
Your use of LayoutManagers is ..brave.
GridBagLayouts are not for the faint hearted,
or inexperienced. You nest them inside other
Layouts.
Let's pause for a moment. Do you actually
need to stick with layouts in the core classes?
Looking at your overall GUI, it screams
'FormLayout' to me, have a look at this..
<http://www.jgoodies.com/freeware/formsdemo/index.html>
--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|