java - How do i get the image from my jlable to auto scale or resize to the size of the JLabel -
this question has answer here:
- java: jpanel background not scaling 2 answers
im trying add image jlabel , have encountered problem image big jlabel i'm looking way make image automatically scale size of jlabel.
here main body of code call guimain class:
public class main { public static void main(string[] args) { int = 0; int t = 0; int st = 0; int h = 0; texts textobject = new texts(); textobject.textlist(); commands commandobject = new commands(); commandobject.commands(); guimain guiobject = new guimain(); guiobject.displaygui(); } }
and here code guimain class, include 1 image button because there allot of objects in code.
import java.awt.eventqueue; import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedimage; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jbutton; import javax.swing.jlist; import javax.swing.jtextpane; import javax.swing.jlabel; import javax.swing.jtextarea; import javax.swing.jscrollbar; import javax.swing.jtextfield; import javax.swing.jpanel; import javax.swing.border.titledborder; public class guimain { private jframe frame; private jtextfield textfield; private imageicon image1; public void displaygui() { this.frame.setvisible(true); } //launch application. public static void main(string[] args) { guimain window = new guimain(); } //create application. public guimain() { frame = new jframe(); frame.setbounds(100, 100, 611, 471); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); ... jlabel lblhunger = new jlabel(); image img2 = new imageicon(this.getclass().getresource("/food.png")).getimage(); lblhunger.seticon(new imageicon(img2)); lblhunger.setbounds(211, 58, 50, 50); panel.add(lblhunger); } } }
don't use null layout!!!
swing designed used layout managers. let each component determine preferred size , let layout manager size , position component based on rules of layout manager.
instead of doing custom painting can use darry'ls stretch icon. icon painted based on space available icon.
i have encountered problem image big jlabel
just reread part of question. in case may want use image.getscaledinstance(...)
method scale image appropriate size. can use regular icon , appropriate layout manager.
what layout able use choose dimensions , objects positioned
you not supposed choose size/location of component. job of layout manager. read tutorial on layout managers , choose 1 (or combination of layout managers) appropriate requirements.
Comments
Post a Comment