Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1760 > unrolled thread

trouble creating resizabl

Started by"E.D.I" <e.d.i@THRWHITE.remove-dii-this>
First post2011-04-27 15:35 +0000
Last post2011-04-27 15:35 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  trouble creating resizabl "E.D.I" <e.d.i@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
    Re: trouble creating resi "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000

#1760 — trouble creating resizabl

From"E.D.I" <e.d.i@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Subjecttrouble creating resizabl
Message-ID<1181144282.851041.219850@q66g2000hsg.googlegroups.com>
  To: comp.lang.java.gui
i want to create a component that is just kind if icon that is
resizable so i made a lable with an icon,
first of all i cant strech the picture on the lable so i made a
picture with a correct size to fit on to the
lable, but when i'm using this as a componnent in the form whenever i
change the size of it the icon
stays with the same size , why is that and how can i create the icon
to work as a background image?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1762 — Re: trouble creating resi

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: trouble creating resi
Message-ID<7351f4bebd9f9@uwe>
In reply to#1760
  To: comp.lang.java.gui
E.D.I wrote:

Sub: trouble creating resizable jlabel with an image??

Please find your shift key and apply it once at the
start of each sentence, for every use of the word 
'I' and any reference to J2SE classes (it is a JLabel, 
unless you are talking about some completely 
unrelated class).  Also note that one question
mark denotes a question, whereas two indicates 
that the person asking, is impatient and needy - 
please restrict yourself to one.

>...how can i create the icon
>to work as a background image?

This code probably answers the question of why it
is not wise to do this, better than it answers the 
actual question.

<sscce>
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

class BackgroundImageLabel extends JPanel {

  Image image;
  JLabel label;

  BackgroundImageLabel(URL imgURL, String text) {
    super( new BorderLayout() );
    image = Toolkit.getDefaultToolkit()
      .createImage( imgURL );
    MediaTracker mt = new MediaTracker(this);
    mt.addImage( image, 0 );
    try {
      mt.waitForAll();
    } catch(InterruptedException e) {
      // wake and continue..
    }
    label = new JLabel(text);
    add( label, BorderLayout.CENTER );
  }

  public void paintComponent(Graphics g) {
    g.drawImage( image,
      0,0,getWidth(),getHeight(),
      this );
    label.repaint();
  }

  public Dimension getMinimumSize() {
    Dimension labelSize = label.getMinimumSize();
    int imageWidth = image.getWidth(this);
    int imageHeight = image.getHeight(this);
    int width = ( (imageWidth/2)<labelSize.width ?
      imageWidth/2 :
      labelSize.width );
    int height = ( (imageHeight/2)<labelSize.height ?
      imageHeight/2 :
      labelSize.height );

    return new Dimension(width,height);
  }

  public Dimension getPreferredSize() {
    Dimension labelSize = label.getPreferredSize();
    int imageWidth = image.getWidth(this);
    int imageHeight = image.getHeight(this);
    int width = ( (imageWidth)<labelSize.width ?
      imageWidth :
      labelSize.width );
    int height = ( (imageHeight)<labelSize.height ?
      imageHeight :
      labelSize.height );

    return new Dimension(width,height);
  }

  public static void main(String[] args)
    throws MalformedURLException {

    URL imageURL = new URL(
      "http://java.sun.com/im/logo_sun_small_sdn.gif");
    JPanel p = new JPanel( new BorderLayout() );

    p.add( new BackgroundImageLabel(imageURL, "North Label"),
      BorderLayout.NORTH );
    p.add( new BackgroundImageLabel(imageURL, "Eastern Label"),
      BorderLayout.EAST );
    p.add( new BackgroundImageLabel(imageURL, "Western Label"),
      BorderLayout.WEST );
    p.add( new BackgroundImageLabel(imageURL, "Center Label"),
      BorderLayout.CENTER );
    p.add( new BackgroundImageLabel(imageURL, "South Label"),
      BorderLayout.SOUTH );
    p.validate();

    JOptionPane.showMessageDialog(null, p);
  }
}
</sscce>

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via http://www.javakb.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web