• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Akshay Kumar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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);
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!