• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to convert a light-weight into a heavy-weight component?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i need to display an image only using AWT components but labels in it doesn't allow it. so, 'm using swing component JLabel to add an image as an icon. the problem is the swing components did not display in an applet so it makes sense to convert this JLabel into Label(AWT component) just to add an image. or, if there some other easiest way is also most welcome.

plz, anyone who has the instant spark can enlighten here.

bye bye.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with overriding Label's paint(Graphics g) method and painting your image there?

Here is a thread that talks about drawing a background image, albeit on a JPanel, but it should be the same if you just put the code in the label's paint() method instead of paintComponent() as the example shows.
 
Akshay Kumar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u very much. it's working too good.
 
Akshay Kumar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bolinger,

here is another problem related to setting a border to an AWT Panel bcoz it does not support setting border unlike JPanel.

Need to implement like one here,

setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.WHITE, Color.BLACK));

so, here is my code to construct an error msg dialog box using AWT components only. 'm restricted to use swings here.

plz, guide me ASAP. thanks.


------------------------------------------Code Follows----------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class mypanel extends Panel {

public mypanel(String[] msg){

super();
Font msgFont = new Font("arial",Font.BOLD,12);
setForeground(Color.BLACK);
setBackground(new Color(214,211,206));
setFont(msgFont);
//setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.WHITE, Color.BLACK));
setLayout(new BorderLayout());

Panel centerPanel = new Panel();
GridLayout gl = new GridLayout(msg.length, 1);
centerPanel.setLayout(gl);
Label l[] = new Label[msg.length];

for (int j=0;j<l.length;j++){
l[j] = createLabel(msg[j]);
l[j].removeFocusListener(null);
centerPanel.add(l[j]);
}

add(centerPanel, BorderLayout.CENTER);

Panel bottom = new Panel();
mylabel l2 = new mylabel();
bottom.add(l2);
add(bottom, BorderLayout.SOUTH);

Panel northPanel = new Panel();
add(northPanel, BorderLayout.NORTH);
Panel eastPanel = new Panel();
add(eastPanel, BorderLayout.EAST);
Panel westPanel = new Panel();
add(westPanel, BorderLayout.WEST);

}

private Label createLabel(String text){
return (new Label(text));
}


}

class mylabel extends Label{

Image okImage;

public mylabel(){
super(" ");
String fileName = "layout_images/cal_ok.gif";
URL url = getClass().getResource(fileName);
try{
okImage = ImageIO.read(url);
}catch(Exception e){System.out.println("Exception Caught");}

addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent m){
System.out.println("MouseMoved");
}
public void mouseClicked(MouseEvent m){
System.out.println("MouseClicked");
}
}
);

addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent m){
System.out.println("mouseDrag");
}
public void mouseMoved(MouseEvent m){
System.out.println("mouseMoved");
}
}
);
}

public int getWidth(){
return okImage.getWidth(this);
}
public int getHeight(){
return okImage.getHeight(this);
}

public void paint(Graphics g){
g.drawImage(okImage, 0, 0,this);
}
}
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic