Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-20-2008, 01:14 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 235
willemjav is on a distinguished road
Text-pain
Computers are in fact sophisticated typewriters and that is why an important part of programming deals with text and text-writing. The text components in java are many and complicated.
My question is, how to display several lines of text (edited in a ´simple´ JTextArea component) over an image, without getting a white square that marks the text pane over the image.
Before I used (successfully) the component JLabel to display a single line of text over the image of my slideshow applet. As far as I understand you cannot display multi lines of text in JLabel. I would also like to be able set the position of the text block over the picture. Just give me a hint in what direction I need to go.

willemjav
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-20-2008, 02:23 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
My question is, how to display several lines of text (edited in a ´simple´ JTextArea component) over an image, without getting a white square that marks the text pane over the image.
This would be difficult.
As far as I understand you cannot display multi lines of text in JLabel.
JLabel has rudimentary support for html version 3.2. For some ideas you can see Using HTML in Swing Components.
I would also like to be able set the position of the text block over the picture.
The easiest way to do this is to render the text in a graphic component using classes/methods in the java.awt.font package. So you draw your image and then render the text above/over the image in a paintComponent method, usually of a JPanel.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-20-2008, 03:33 AM
Senior Member
 
Join Date: Jun 2008
Posts: 194
Fubarable is on a distinguished road
One way is to set the JTextArea opaque property to false. If the JTextArea is in a JScrollPane, you'll have to set it *and* it's jviewport's opaque to false as well. For instance:

Code:
import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class TextAreaOverImage { private static final String IMAGE_PATH = "http://upload.wikimedia.org/wikipedia/commons/b/b5/HMS_Cardiff_%28D108%29_1.jpg"; private BufferedImage image; private JTextArea textarea = new JTextArea(20, 40); private JPanel mainPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { g.drawImage(image, 0, 0, this); } } }; public TextAreaOverImage() { URL imageUrl; try { imageUrl = new URL(IMAGE_PATH); image = ImageIO.read(imageUrl); Dimension imageSize = new Dimension(image.getWidth(), image.getHeight()); mainPanel.setPreferredSize(imageSize); JScrollPane scrollpane = new JScrollPane(textarea); textarea.setOpaque(false); scrollpane.setOpaque(false); scrollpane.getViewport().setOpaque(false); mainPanel.add(scrollpane); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public JPanel getPanel() { return mainPanel; } private static void createAndShowGUI() { JFrame frame = new JFrame("TextAreaOverImage Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new TextAreaOverImage().getPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-20-2008, 08:13 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 235
willemjav is on a distinguished road
text drawn over text
Sorry, hardwired, I probably did not phrase my question very well. I know, now, that indeed using a graphical content it is possible the use: drawString(String str, int x, int y)

To display a text line over a picture.

Thanks, Fubarable, that was relatively easy to do for the moment. I looked up opaque and found “not able to see through/ not transparent”. Setting Opaque false makes since (yes, sometimes a programming problem is in fact a language problem). I added some lines of your code:

infotextPanel = new JTextArea();
scrollpane = new JScrollPane(infotextPanel); // probably won´t use the scroll pane
infotextPanel.setOpaque(false);
scrollpane.setOpaque(false);
scrollpane.getViewport().setOpaque(false);
setLayout(null);
setPreferredSize(new Dimension(800, 485));
add(infotextPanel);

public void drawinfotext (String infotext, int infotextx, int infotexty) {
infotextPanel.setBounds(infotextx, infotexty, 300, 300); // when setting // the layout to null this method changes the position, correct?
infotextPanel.setBounds(infotextx, infotexty, 300, 300);
// infotextPanel.setText(infotext);
}


great, I do see the text without white square. Thanks for the suggestion.

But I still have another ¨tiny¨ problem each time I draw a new picture the new text of that picture is printed over the previous text (which does not disappear!). The reason lies probably in the fact that I do the drawing in another class.
The class SetimagePanel triggers all GUI components and the class Editimages does the image/ text drawing etc. Each time one selects another image in JList present in the SetimagePanel class the image/text drawing happens:

public void valueChanged(ListSelectionEvent e) { // reads the list selection
// and gets the array index
if(!e.getValueIsAdjusting()) { // basically reads out the JList
sizeSlider.setValue(100); // draws the image and updates
//textinfo and all data
index = Piclist.getSelectedIndex();
editImages.downloadImage(filepath + "/Imagestore/" + img[index].name);

editImages.displayImage(1.0);
infotextPane.setText(img[index].textinfo); // the text of the edit pane
editImages.drawinfotext(img[index].textinfo, img[index].infotextx, img[index].infotexty); // text displayed over image
etc.
The info text gets two time drawn, ones on the edit pane at the same class (so the text can be edited) and ones over the image in a different class (to see the result together with the image), and only the first draw works correct! I tried several things (like repaint()) to repair this problem, but sincerely I do not understand why this happens (in one pane and not in the other……both are JTextArea panes).

willemjav
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-21-2008, 11:00 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 235
willemjav is on a distinguished road
I solved this problem myself.
I had the following code, which creates the info panel over the image,
at the wrong spot (in a file download method, so each time a image gets download the method creates a new info panel etc.). Instead it should be in the constructor of the second class (how stupid):

public Editimages() { //constructor of edit class
infotextPanel = new JTextArea();
scrollpane = new JScrollPane(infotextPanel);
infotextPanel.setOpaque(false);
scrollpane.setOpaque(false);
scrollpane.getViewport().setOpaque(false);
setLayout(null);
setPreferredSize(new Dimension(800, 485));
add(infotextPanel);

}

thanks
willemjav
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-21-2008, 11:01 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 235
willemjav is on a distinguished road
by the way where are these solved labels?
so I can mark this thread as solved
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text to XML conversion tarandeep.singh XML 1 06-14-2008 03:17 AM
Unsupported Content-Type: text/html Supported ones are: [text/xml] luislopezco Advanced Java 0 05-26-2008 05:26 PM
Using SWT Text Java Tip Java Tips 0 01-10-2008 11:41 AM
map javax.swing.text.Element to javax.swing.text.View elizabeth New To Java 1 07-30-2007 08:02 PM


All times are GMT +3. The time now is 12:51 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org