image - Can't change background color of java application -


i'm trying hand @ making picture viewer in java , life of me, can't background color of viewer change black. here's latest code:

public class pictureviewer extends jframe {     static class pauseaction extends abstractaction {         public void actionperformed(actionevent arg0) {             pauseviewer = !pauseviewer;         }     }      static class quitaction extends abstractaction {         public void actionperformed(actionevent e) {             stopviewer = true;             pauseviewer = true;             viewer.setnexttoview();             system.exit(0);         }     }      static double height;     static final string newline = system.getproperty("line.separator");     static boolean pauseviewer = false;     static dimension screensize = new dimension();     static boolean stopviewer = false;     static pictureviewer viewer;     static double width;      jlabel area = new jlabel("", jlabel.center);     int currentpic = 0;     file dir = new file(".");     bufferedimage image;         string path;     action pauseaction;     int pausetime = 5;     action quitaction;     private jscrollpane scrollpane = new jscrollpane(area,  jscrollpane.vertical_scrollbar_never,jscrollpane.horizontal_scrollbar_never);     arraylist<file> thesepictures;      public static void main(string[] args) throws ioexception {         //create , set window.         viewer = new pictureviewer();         viewer.setundecorated(true);  //remove minimize, maximize , close buttons entirely.          //get list of files display.         viewer.initialize();          //set content pane.         viewer.addcomponents();         viewer.setpreferredsize(screensize);          //display window.         viewer.pack();         viewer.setvisible(true);          //start showing pictures.         while (!stopviewer) {             try {                 viewer.showpictures();             } catch (interruptedexception e) {                 e.printstacktrace();             }         }          //perform cleanup         viewer.setnexttoview();     }      public void addcomponents() {         //set actions.         pauseaction = new pauseaction();         quitaction = new quitaction();         scrollpane.getinputmap().put(keystroke.getkeystroke("p"), "dopauseaction");         scrollpane.getactionmap().put("dopauseaction", pauseaction);         scrollpane.getinputmap().put(keystroke.getkeystroke("space"), "doquitaction");         scrollpane.getactionmap().put("doquitaction", quitaction);         scrollpane.setbackground(color.black);         getcontentpane().add(scrollpane);          viewer.repaint();     }      public arraylist<file> getpictures(file dir) {         arraylist<file> listfiles = new arraylist<file>(arrays.aslist(dir.listfiles()));         int selectthis = (int) (math.random() * listfiles.size());         boolean emptylist = true;          if (listfiles.get(selectthis).isdirectory()) {             return getpictures(listfiles.get(selectthis));         } else {             //if selected file not directory, go through list of files , remove directories.              arraylist<file> newlist = new arraylist<file>();              (file thisfile : listfiles) {                 if (!thisfile.isdirectory() && !thisfile.getname().contains(".next") && !thisfile.getname().contains(".jar")) {                     newlist.add(thisfile);                 }             }              listfiles = newlist;         }          return listfiles;     }      public void initialize() {         screensize = toolkit.getdefaulttoolkit().getscreensize();         height = screensize.getheight();         width = screensize.getwidth();         string filepath = new file(".").getabsolutepath();         filepath = filepath.substring(0, filepath.length() - 1);         string directory = "";          while (thesepictures == null || thesepictures.size() == 0) {             thesepictures = getpictures(dir);         }          string absolutepath = thesepictures.get(0).getabsolutepath();         path = absolutepath.substring(0,absolutepath.lastindexof(file.separator));          //look see if .next file exists.  if so, read in file object.  if not, set index 0.         file checkfile = new file(path + "\\.next");          if (checkfile.exists()) {             try {                 inputstream inputfile = new fileinputstream(path + "\\.next");                 inputstream buffer = new bufferedinputstream(inputfile);                 objectinput input = new objectinputstream(buffer);                 file lastviewedpic = (file) input.readobject();                  if (thesepictures.contains(lastviewedpic)) {                     currentpic = thesepictures.indexof(lastviewedpic);                 } else {                     currentpic = 0;                 }             } catch (filenotfoundexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             } catch (classnotfoundexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();                 system.out.println("look @ " + path + "\\.next");                 system.exit(-1);             }         } else {             currentpic = 0;         }     }      public void readinfile(string filename) {         file file = new file(filename);         if(file.isfile()) {             try {                 image = imageio.read(file);                  if (image.getwidth() > width || image.getheight() > height) {                     bufferedimage newimage = new bufferedimage(width.intvalue(), height.intvalue(), bufferedimage.type_int_rgb);                     graphics2d g = newimage.creategraphics();                     g.drawimage(image, 0, 0, width.intvalue(), height.intvalue(), null);                     g.dispose();                     image = newimage;                 }             } catch (ioexception e) {                 showmessagedialog(viewer,"does not compute !","no file read or found",information_message);                 e.printstacktrace();             } catch (exception e) {                 showmessagedialog(viewer, "problem: " + e.getlocalizedmessage());             }         }     }      public void setimage(jlabel area){         imageicon icon = new imageicon(image);         area.seticon(icon);         viewer.repaint();     }      protected void setnexttoview() {         //see if next picture view file exists.  if not, create it.         file checkfile = new file(path + "\\.next");          if (!checkfile.exists()) {             try {                 checkfile.createnewfile();             } catch (ioexception e) {                 e.printstacktrace();             }         }          try {             fileoutputstream outputfile = new fileoutputstream(path + "\\.next");             objectoutputstream writer = new objectoutputstream(outputfile);             writer.writeobject(thesepictures.get(currentpic));             writer.close();         } catch (ioexception e) {             e.printstacktrace();         }     }      private void showpictures() throws interruptedexception {         while (!pauseviewer) {             //if reach last file in directory, switch directory (it same directory).             if (currentpic + 1 == thesepictures.size()) {                 currentpic = 0;                 thesepictures = new arraylist<file>();                  while (thesepictures == null || thesepictures.size() == 0) {                     thesepictures = getpictures(dir);                 }             } else {                 currentpic += 1;             }              readinfile(thesepictures.get(currentpic).getabsolutepath());             setimage(area);             timeunit.seconds.sleep(pausetime);         }     } }    

just doing wrong?

thanks!

you using many static fields no reason, making class less extensible. pictureviewer class extends jframe , inside instead of using same reference (on calling methods getcontentpane().add(scrollpane)) instead creating new static reference using pictureviewer viewer = new pictureviewer(), how can both on same instance.

moreover, in order change background of jscrollpane thingy :

scrollpane.getviewport().setbackground(color.black); 

here modified code, though never went deep rectify bad practices, though did managed bring few of them :-)

import static javax.swing.joptionpane.information_message; import static javax.swing.joptionpane.warning_message; import static javax.swing.joptionpane.showmessagedialog;  import java.awt.borderlayout; import java.awt.color; import java.awt.eventqueue; import java.awt.dimension; import java.awt.graphics2d; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.awt.image.bufferedimage; import java.io.bufferedinputstream; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.objectinput; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.malformedurlexception; import java.net.url; import java.util.arraylist; import java.util.arrays; import java.util.concurrent.timeunit;  import javax.imageio.imageio; import javax.swing.abstractaction; import javax.swing.action; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jscrollpane; import javax.swing.jtextfield; import javax.swing.keystroke;  public class pictureviewer {     class pauseaction extends abstractaction {         public void actionperformed(actionevent arg0) {             pauseviewer = !pauseviewer;         }     }      class quitaction extends abstractaction {         public void actionperformed(actionevent e) {             stopviewer = true;             pauseviewer = true;             setnexttoview();             system.exit(0);         }     }      static double height;     static final string newline = system.getproperty("line.separator");     static boolean pauseviewer = false;     static dimension screensize = new dimension();     static boolean stopviewer = false;     static jframe viewer;     static double width;      jlabel area = new jlabel("", jlabel.center);     int currentpic = 0;     file dir = new file(".");     bufferedimage image;         string path;     action pauseaction;     int pausetime = 5;     action quitaction;     private jscrollpane scrollpane = new jscrollpane(area,  jscrollpane.vertical_scrollbar_never,jscrollpane.horizontal_scrollbar_never);     arraylist<file> thesepictures;      private void displaygui() {         //create , set window.         viewer = new jframe("picture viewer");         viewer.setdefaultcloseoperation(jframe.dispose_on_close);         viewer.setundecorated(true);  //remove minimize, maximize , close buttons entirely.          //get list of files display.         initialize();          //set content pane.         addcomponents();         viewer.setpreferredsize(screensize);          //display window.         viewer.pack();         viewer.setvisible(true);          //start showing pictures.         while (!stopviewer) {             try {                 showpictures();             } catch (interruptedexception e) {                 e.printstacktrace();             }         }          //perform cleanup         setnexttoview();     }      public static void main(string[] args) throws ioexception {         runnable runnable = new runnable() {             @override             public void run() {                 new pictureviewer().displaygui();             }         };         eventqueue.invokelater(runnable);     }      public void addcomponents() {         //set actions.         pauseaction = new pauseaction();         quitaction = new quitaction();         scrollpane.getinputmap().put(keystroke.getkeystroke("p"), "dopauseaction");         scrollpane.getactionmap().put("dopauseaction", pauseaction);         scrollpane.getinputmap().put(keystroke.getkeystroke("space"), "doquitaction");         scrollpane.getactionmap().put("doquitaction", quitaction);         scrollpane.getviewport().setbackground(color.black);         viewer.add(scrollpane);     }      public arraylist<file> getpictures(file dir) {         arraylist<file> listfiles = new arraylist<file>(arrays.aslist(dir.listfiles()));         int selectthis = (int) (math.random() * listfiles.size());         boolean emptylist = true;          if (listfiles.get(selectthis).isdirectory()) {             return getpictures(listfiles.get(selectthis));         } else {             //if selected file not directory, go through list of files , remove directories.              arraylist<file> newlist = new arraylist<file>();              (file thisfile : listfiles) {                 if (!thisfile.isdirectory() && !thisfile.getname().contains(".next") && !thisfile.getname().contains(".jar")) {                     newlist.add(thisfile);                 }             }              listfiles = newlist;         }          return listfiles;     }      public void initialize() {         screensize = toolkit.getdefaulttoolkit().getscreensize();         height = screensize.getheight();         width = screensize.getwidth();         string filepath = new file(".").getabsolutepath();         filepath = filepath.substring(0, filepath.length() - 1);         string directory = "";          while (thesepictures == null || thesepictures.size() == 0) {             thesepictures = getpictures(dir);         }          string absolutepath = thesepictures.get(0).getabsolutepath();         path = absolutepath.substring(0,absolutepath.lastindexof(file.separator));          //look see if .next file exists.  if so, read in file object.  if not, set index 0.         file checkfile = new file(path + "\\.next");          if (checkfile.exists()) {             try {                 inputstream inputfile = new fileinputstream(path + "\\.next");                 inputstream buffer = new bufferedinputstream(inputfile);                 objectinput input = new objectinputstream(buffer);                 file lastviewedpic = (file) input.readobject();                  if (thesepictures.contains(lastviewedpic)) {                     currentpic = thesepictures.indexof(lastviewedpic);                 } else {                     currentpic = 0;                 }             } catch (filenotfoundexception e) {                 e.printstacktrace();             } catch (ioexception e) {                 e.printstacktrace();             } catch (classnotfoundexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();                 system.out.println("look @ " + path + "\\.next");                 system.exit(-1);             }         } else {             currentpic = 0;         }     }      public void readinfile(string filename) {         file file = new file(filename);         if(file.isfile()) {             try {                 image = imageio.read(file);                  if (image.getwidth() > width || image.getheight() > height) {                     bufferedimage newimage = new bufferedimage(width.intvalue(), height.intvalue(), bufferedimage.type_int_rgb);                     graphics2d g = newimage.creategraphics();                     g.drawimage(image, 0, 0, width.intvalue(), height.intvalue(), null);                     g.dispose();                     image = newimage;                 }             } catch (ioexception e) {                 showmessagedialog(viewer,"does not compute !","no file read or found",information_message);                 e.printstacktrace();             } catch (exception e) {                 showmessagedialog(viewer, "problem: " + e.getlocalizedmessage());             }         }     }      public void setimage(jlabel area){         imageicon icon = new imageicon(image);         area.seticon(icon);         viewer.repaint();     }      protected void setnexttoview() {         //see if next picture view file exists.  if not, create it.         file checkfile = new file(path + "\\.next");          if (!checkfile.exists()) {             try {                 checkfile.createnewfile();             } catch (ioexception e) {                 e.printstacktrace();             }         }          try {             fileoutputstream outputfile = new fileoutputstream(path + "\\.next");             objectoutputstream writer = new objectoutputstream(outputfile);             writer.writeobject(thesepictures.get(currentpic));             writer.close();         } catch (ioexception e) {             e.printstacktrace();         }     }      private void showpictures() throws interruptedexception {         while (!pauseviewer) {             //if reach last file in directory, switch directory (it same directory).             if (currentpic + 1 == thesepictures.size()) {                 currentpic = 0;                 thesepictures = new arraylist<file>();                  while (thesepictures == null || thesepictures.size() == 0) {                     thesepictures = getpictures(dir);                 }             } else {                 currentpic += 1;             }              readinfile(thesepictures.get(currentpic).getabsolutepath());             setimage(area);             timeunit.seconds.sleep(pausetime);         }     } } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -