java - Image not showing up in Swing? -


i don't know i'm doing wrong. (although i'm sure it's quite bit.) i've been trying adjust code hours no avail.

i'm attempting cut pieces of picture , display them. later, randomize located , try place them in correct positions. now, however, i'm having issues having show on jpanel @ all. @ 1 point, test code displayed image. now, however, same test code doesn't work.

if can see i'm doing wrong/where issue is, please let me know. it's simple , stupid.

import java.awt.*; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.*; import java.awt.image.bufferedimage; import java.io.*; import java.util.*; import javax.imageio.*; import javax.swing.*;  public class lab10 {      /**      * @param args command line arguments      */     public static void main(string[] args) {         myframe frame = new myframe();         mypanel panel = new mypanel();         frame.add(panel);         panel.setfocusable(true);       }  }  class myframe extends jframe{     myframe(){         setdefaultcloseoperation(exit_on_close);         setvisible(true);         setsize(800,800);         setresizable(false);     } }  class mypanel extends jpanel{     public static final int size = 4;     private int oldx = -1;     private int oldy = -1;     private final string filename = "housetyrell.png";      public imagearray imagearray = new imagearray(filename, size);      mypanel(){          addmouselistener(new mouseadapter(){              public void mousepressed(mouseevent e){                 if(e.getbutton()==1){                     if(oldx == -1){                         oldx = e.getx();                         oldy = e.gety();                     }else{                         //imagearray.swappoints(oldx, oldy, e.getx(), e.gety());                         oldx = -1;                         oldy = -1;                         repaint();                     }                 }             }         });     }      public void paintcomponent(graphics g){         super.paintcomponent(g);         graphics2d g2 = (graphics2d) g;         imagearray.draw(g2);          /*image image2;         try {             image2 = imageio.read(new file("housetyrell.png"));             system.out.println("i should print");              try{                 g2.drawimage(image2, 0, 0, null);             } catch(exception my){                 system.out.println("drawing issue");             }          } catch (ioexception ex) {             system.out.println("reading issue");          }*/      } }   class imagearray{     private square[][] squares;     private string filename;     private int size;     private int w;     private int h;      public imagearray(string filename, int size) {         this.size = size;         this.filename= filename;         squares = new square[size][size];         try {             bufferedimage image = imageio.read(new file(filename));             w = image.getwidth() / size;             h = image.getheight() / size;             (int row = 0; row < size; row++) {                 (int col = 0; col < size; col++) {                     squares[row][col] = new square(                     image.getsubimage(row * w , col * h , w, h), row, col, w, h);                 }             }          } catch (exception e) {             system.out.println("can't open file!");         }         shuffle();         }       //todo     public void shuffle(){         for(int = 0; < size * size; i++){          }     }      //todo     public void swappoints(int oldx, int oldy, int newx, int newy){      }      public void draw(graphics2d g2){         for(int = 0; < squares.length; i++){             for(int j = 0; j < squares[0].length; j++){                 square square = squares[i][j];                 square.draw(g2);              }         }     }  }  class square{     private bufferedimage image;     private int row;     private int col;     private int x,y;     private int w;     private int h;      square(bufferedimage image, int row, int col, int w, int h){         this.image = image;         this.row = row;         this.col = col;         this.x = row * w;         this.y = col * h;         this.w = w;         this.h = h;     }      public void draw(graphics2d g2){         try{             g2.drawimage(image, x, y, null);         } catch (exception my){             system.out.println("square issue");         }     }      public bufferedimage getimage(){         return image;     }      public int getrow(){         return row;     }      public int getcol(){         return col;     } } 

you making frame visible before you've finished establishing ui, try calling invalidate, validate , repaint on frame after you've added panel...

public static void main(string[] args) {     myframe frame = new myframe();     mypanel panel = new mypanel();     frame.add(panel);     panel.setfocusable(true);     frame.invalidate();     frame.validate();     frame.repaint();  } 

frankly, myframe class doing little (other making life more difficult), i'd consider getting rid of it, maybe replace builder method returns frame not visible yet...

you should creating ui within context of event dispatching thread, can solve other "weird" issues.

public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         @override         public void run() {             try {                 uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());             } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 ex.printstacktrace();             }              myframe frame = new myframe();             mypanel panel = new mypanel();             frame.add(panel);             panel.setfocusable(true);             frame.invalidate();             frame.validate();             frame.repaint();         }     }); } 

see initial threads more details


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -