?
Solved

positioning an image together with buttons etc

Posted on 2003-03-11
8
Medium Priority
?
179 Views
Last Modified: 2010-03-31
Hi!

Im VERY new to java, so pls if possible explain your answers a bit ;)
i guess i need java for dummies :)

so heres my problem:

i want to display a picture in an applett and also display some AWT items like a status bar, list fields and some labels etc

now i load the picture with
----
   public void paint(Graphics screen) {
           screen.drawImage(mypicture, 100, 50,200,300, this);
   }
------

works fine, but i find it hard to put that picture in to my layout. either the GridLayout lays on top of the picture or i must place everything "higher" than the picture and move the picture downwards trough its Y coordinate. but thats not what i want, instead i think i could best show you what i want by a small diagramm:
------------------------
Statusbar  |  Picture
ListButton | ListButton
ListButton | ListButton
Label
Text Input
------------------------
i can figure out how to position every thing else, im also using panels, but i cant position the picture in this grid - how can i do it?

I hope you got the idea of my question :D

Greetz
Chris
0
Comment
Question by:jabberwocky84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:antonsigur
ID: 8112548
You can put a image into JLabel, like you do with text.

You must load the image as icon:

Then you can do:

JLabel lb = new JLabel("text with image",ic);

0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8112617
Sorry, submited too soon... here is my post

        //1st create the icon from your image.
        ImageIcon icon = new ImageIcon("C:\\picture.gif");
        //OR
        ImageIcon icon = new ImageIcon(myImage);
               
       
        //Now create the JLabel with both text and image on it, you can also but images on buttoms.
        //The JLbael.LEFT tells the label where the image should be.
        //You can just create label, and no text with new JLabel(icon);
        JLabel lb = new JLabel("Some text",icon,JLabel.LEFT);

This is ease, now you just put your label on the screen like other componnents...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8112643
This is an AWT applet...

Try making a label component that will accept an image when you create it...  Then just add the ImageLabel as you would add any other component...

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class ImageLabel extends Label implements ImageObserver
{
Image pic;
String label;

PicButton()
{
super("PIC Button");
}

PicButton(Image i,String s)
{
pic=i;
label=s;
repaint();
}

public void paint(Graphics g)
{
setSize(50,50);
setBackground(Color.white);
g.drawImage(pic,5,5,40,40,this);
g.drawString(label,55,55);
}

public boolean imageUpdate(Image i,int info,int x,int y,int h,int w)
{
if((info & ImageObserver.ALLBITS) != 0) repaint();
return true;
}
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
TimYates earned 400 total points
ID: 8112707
oops...  that should be:

public class ImageLabel extends Label implements ImageObserver
{
Image pic;
String label;

ImageLabel()
{
super("Image Label");
}

ImageLabel(Image i,String s)
{
pic=i;
label=s;
repaint();
}

public void paint(Graphics g)
{
setSize(50,50);
setBackground(Color.white);
g.drawImage(pic,5,5,40,40,this);
g.drawString(label,55,55);
}

public boolean imageUpdate(Image i,int info,int x,int y,int h,int w)
{
if((info & ImageObserver.ALLBITS) != 0) repaint();
return true;
}
}
0
 

Author Comment

by:jabberwocky84
ID: 8128091
ah THX a lot :D

it works now :D got it all figured out !

greetz

jabberwock
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8128178
Cool :-)

Glad I could help :-)

If you accept one of my comments as an answer, then this question will be closed, and moved to the Previously Answered questions section of the site :-)

Good luck!

Tim
0
 

Author Comment

by:jabberwocky84
ID: 8158000
thanks a lot Tim - the image loading works fine as i said!

But now i encounter a new problem: i want to change the picture during runtime of the applett, but it doesnt work.

it just loads one time at applett initialization and somehow cannot be changed anymore after that ...

----------------------
Image jabberwockStatusurl = getImage(getDocumentBase(),path);
jabberwockStatus = new ImageLabel(jabberwockStatusurl, status);
panelPicture.add(jabberwockStatus);
----------------------

this is the way i load the picture, but it somehow isnt possible to reload it during runtime (by giving it a different URL - any suggestions?

THX

Chris
0
 

Author Comment

by:jabberwocky84
ID: 8158028
thanks a lot Tim - the image loading works fine as i said!

But now i encounter a new problem: i want to change the picture during runtime of the applett, but it doesnt work.

it just loads one time at applett initialization and somehow cannot be changed anymore after that ...

----------------------
Image jabberwockStatusurl = getImage(getDocumentBase(),path);
jabberwockStatus = new ImageLabel(jabberwockStatusurl, status);
panelPicture.add(jabberwockStatus);
----------------------

this is the way i load the picture, but it somehow isnt possible to reload it during runtime (by giving it a different URL - any suggestions?

THX

Chris
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question