[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

positioning an image together with buttons etc

Posted on 2003-03-11
8
Medium Priority
?
182 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
  • 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

640 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