Tuesday, 15 March 2011

java - Buttons will not show up in FlowLayout -



java - Buttons will not show up in FlowLayout -

i making programme class. thought display image , up/down contrast. here example. able create image come , gray bar comes above image in example, buttons not there. not sure how through flowlayout, or if need utilize layout. i'm new gui's, help appreciated!

this how mine looks. doing wrong?

import javax.imageio.imageio; import javax.swing.*; import java.util.*; import java.io.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.applet.applet; /** * class extends jpanel , can load image * original size. * * @author dahai guo * */ class imagepanel extends jpanel{ private bufferedimage image; public imagepanel(bufferedimage image){ this.image=image; } /** * draws image. */ public void paintcomponent(graphics g){ super.paintcomponent(g); g.drawimage(image,0,0,image.getwidth(),image.getheight(),this); } } /** * object of class can load image , enable user * increment , decrease contrast of image. * * note class can deal contrast of grayness levels * * @author dahai guo * */ public class histgramequalizerapp extends jframe { int count=0; private flowlayout mylayout; private borderlayout borderlayout; private imagepanel imagepanel; private jbutton increasecontrastbutton; private jbutton decreasecontrastbutton; private bufferedimage image; /** * cummulative density function */ private int [] cdf=new int[256]; /** * current largest grayness level. */ private int imagemaxgraylevel=0; /** * current smallest grayness level. */ private int imagemingraylevel=300; /** * smallest nonzero cell in cdf of grayness level * of image. */ private int mincdfvalue; /** * largest grayness level when image first loaded. */ private int originalimagemaxgray; /** * smallest grayness level when image first loaded. */ private int originalimagemingray; private int max_gray_level=255; private int min_gray_level=0; /** * sets gui components , register action listener * 2 buttons increasing , decreasing contrast * @param filename filename of image * @throws ioexception thrown when problems occurs when loading image */ public histgramequalizerapp(string filename) throws ioexception{ // insert code seek { image = imageio.read(new file(filename)); } grab (ioexception e) { system.err.println("problem loading image."); system.exit(1); } // display image screen add(new jlabel(new imageicon(filename))); mylayout = new flowlayout(flowlayout.center, 20, 40); setlayout(mylayout); // buttons contrast command increasecontrastbutton = new jbutton(">>"); decreasecontrastbutton = new jbutton("<<"); add(increasecontrastbutton); increasecontrastbutton.setvisible(true); add(decreasecontrastbutton); decreasecontrastbutton.setvisible(true); //increasecontrastbutton.addactionlistener(this); //decreasecontrastbutton.addactionlistener(this); } /** * calculates current image's cdf. * * there 256 grayness levels in consideration. image scanned * pixel pixel. each pixel, grayness level average of rgb. * grayness level used generate histogram. * * when histogram ready, * cdf[i]=sum(histogram[0]+histogram[1]+...+histogram[i]). * * note method in charge of calculating next instance variable: * <ul> * <li>imagemaxgraylevel</li> * <li>imagemingraylevel</li> * <li>mincdfcell</li> * </ul> */ private void findcdf(){ // insert code } /** * finds rgb of pixel in image. * * @param image source image * @param x horizontal coordinate * @param y vertical coordinate * @param rgb array result saved */ private void getrgb(bufferedimage image, int x, int y, int[] rgb){ int temp=image.getrgb(x,y); color color=new color(temp); rgb[0]=color.getred(); rgb[1]=color.getgreen(); rgb[2]=color.getblue(); } /** * sets rgb values pixel of image * * @param image source image * @param x horizontal coordinate * @param y vertical coordinate * @param rgb rgb values set */ private void setrgb(bufferedimage image, int x, int y, int[] rgb){ color color=new color(rgb[0],rgb[1],rgb[2]); image.setrgb(x, y, color.getrgb()); } /** * inner class handles event on increasecontrastbutton , * decreasecontrastbutton. * * @author dahai guo * */ private class changeconstrastlistener implements actionlistener{ /** * variable decides how fast contrast changing. * * when increasing contrast, largest/smallest grayness level * increased/decreased step. * * when decreasing contrast, largest/smallest grayness level * decrease/increase step. */ private int step=10; /** * method deals both increasecontrastbutton , * decreasecontrastbutton. */ @override public void actionperformed(actionevent e){ // insert code imagepanel.repaint(); } /** * changes contrast each pixel of image * defined in outer class. * @param maxgray * @param mingray */ private void changeconstrast(int maxgray, int mingray){ int width=image.getwidth(); int height=image.getheight(); int rgb[]=new int[3]; for(int i=0;i<width;i++){ for(int j=0;j<height;j++){ equalize(i,j,maxgray,mingray); } } findcdf(); } /** * follows "histogram equalization" on wikipedia. * * changes rgb pixel @ (x,y) in image. * * @param x horizontal coordinate * @param y vertical coordinate * @param max new max grayness level * @param min new min grayness level */ private void equalize(int x, int y, int max, int min){ // insert code here } } public static void main(string args[]) throws ioexception{ if(args.length!=1){ system.err.println("missing path image file"); system.err.println("command: java histogramequalizer image_file"); system.exit(1); } histgramequalizerapp histapp = new histgramequalizerapp(args[0]); histapp.setdefaultcloseoperation( jframe.exit_on_close ); histapp.setsize( 512, 400 ); // set frame size histapp.setvisible( true ); // display frame histapp.setresizable(true); } }

here 1 way create work:

public class histgramequalizerapp extends jframe { private jbutton increasecontrastbutton = new jbutton(">>");; private jbutton decreasecontrastbutton = new jbutton("<<");; public histgramequalizerapp(string filename) throws ioexception { jpanel buttonspanel = new jpanel(new flowlayout(flowlayout.center, 20, 40)); buttonspanel.add(decreasecontrastbutton); buttonspanel.add(increasecontrastbutton); getcontentpane().add(new jlabel(new imageicon(filename))); getcontentpane().add(buttonspanel, borderlayout.page_start); } public static void main(string args[]) throws ioexception { histgramequalizerapp histapp = new histgramequalizerapp(args[0]); histapp.setdefaultcloseoperation(jframe.exit_on_close); histapp.pack(); histapp.setvisible(true); } }

java button user-interface layout

No comments:

Post a Comment