java - drawImage() on JPanel OR add Image on top of GridLayout -


i have 100 100 grid of labels. i've got method creates , populates array of strings. next method creates array of labels , adds string (created previous method) labels using settext() method. of labels contain images too. method after takes jlabels , adds them jpanel of grid layout(lets call x1). i've added jpanel jscrollpane(x2), jscrollpane gets added jpanel(x3) empty border , final jpanel(x3) gets added jframe. that's how i've created grid , i'm happy that, don't want change it.

i add image x1 - jpanel grid layout. have add paintcomponent method , use drawimage() method. question how eclipse know panel add image to? i's prefer not create separate class x1, did before , didn't work out right , rather not go down incredibly frustrating road again, i'm sorry!

i have considered using glass pane no longer able see images of jlabels - important.

i think adding image background of jpanel best because want have button shows/hides grid lines - borders of jlabels.

i hope i'm making sense.

below code. understand it's lot of code in 1 class. did have in 2 separate classes didn't work me. find easier. hope don't mind

package rovermars;  import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.dimension; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.image.bufferedimage; import java.io.ioexception;  import javax.imageio.imageio; import javax.swing.borderfactory; import javax.swing.box; import javax.swing.boxlayout; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jcheckbox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextfield; import javax.swing.border.border; import javax.swing.border.etchedborder;  public class menupanel extends jpanel {      private static final long serialversionuid = -3928152660110599311l;      public jpanel frame, textfield, buttons, cppanel;     public jtextfield commands;     public jbutton plot, submit, undo;     public jlabel position, cplabel;     public border loweredetched;     public jcheckbox gridlines;      public submitbutton sub;      static final int rows = 100, columns = 100;      // ******images******     static bufferedimage north, south, west, east;      public void imageloader() {          try {             north = imageio.read(this.getclass().getresource("north.png"));             south = imageio.read(this.getclass().getresource("south.png"));             west = imageio.read(this.getclass().getresource("west.png"));             east = imageio.read(this.getclass().getresource("east.png"));         } catch (ioexception e) {             // todo auto-generated catch block             system.out.println("error occured: " + e);             e.printstacktrace();         }     }      // ******images******      public void createmenu(jpanel p) {           // text field panel         commands = new jtextfield(20);         textfield = new jpanel();         textfield.setpreferredsize(new dimension(150, 50));         textfield.setborder(borderfactory.createemptyborder(10, 10, 10, 10));         textfield.setbackground(new color(204, 153, 255));         textfield.add(commands);          // have button next text field clear contents.         // might need give jpanel new flow layout.           // buttons panel         buttons = new jpanel();         buttons.setpreferredsize(new dimension(150, 250));         buttons.setlayout(new boxlayout(buttons, boxlayout.y_axis));         buttons.setbackground(new color(170, 051, 170));          // create , add buttons buttons panel         buttons.add(box.createrigidarea(new dimension(30, 10)));         plot = new jbutton("plot");         plot.setalignmentx(component.center_alignment);         plot.setalignmenty(component.center_alignment);          buttons.add(plot);         buttons.add(box.createrigidarea(new dimension(30, 10)));         submit = new jbutton("submit");         submit.setalignmentx(component.center_alignment);         submit.setalignmenty(component.center_alignment);          submit.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent e) {                 sub = new submitbutton();                 sub.submit(commands);                 cplabel.settext("*****set current position*****");                 labels[2][2].seticon(new imageicon(north));                  // able move rover here using loops                 // , if statements.              }         });          buttons.add(submit);         buttons.add(box.createrigidarea(new dimension(30, 10)));         undo = new jbutton("undo");         undo.setalignmentx(component.center_alignment);         undo.setalignmenty(component.center_alignment);         buttons.add(undo);         buttons.add(box.createrigidarea(new dimension(30, 10)));          gridlines = new jcheckbox();         gridlines.settext("show gridlines");         gridlines.setalignmentx(component.center_alignment);         gridlines.setalignmenty(component.center_alignment);          gridlines.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent arg0) {                 // set colour of jlabels array here.                 system.out.println("clicked");              }         });          buttons.add(gridlines);         buttons.add(box.createrigidarea(new dimension(30, 20)));           loweredetched = borderfactory                 .createetchedborder(etchedborder.raised);          cplabel = new jlabel("current position: ", jlabel.center);          cppanel = new jpanel();         cppanel.setbackground(new color(153, 153, 204));         cppanel.add(cplabel);         cppanel.setborder(loweredetched);           // panel main window         jpanel frame = new jpanel();         frame.setpreferredsize(new dimension(150, 350));         frame.setlayout(new borderlayout());         frame.add(textfield, borderlayout.north);         frame.add(buttons, borderlayout.center);           // main panel         p.setpreferredsize(new dimension(350, 700));         p.setbackground(new color(153, 153, 204));         p.setlayout(new boxlayout(p, boxlayout.y_axis));         p.setborder(borderfactory.createemptyborder(10, 50, 10, 25));         p.add(box.createrigidarea(new dimension(100, 100)));         p.add(frame);         p.add(box.createrigidarea(new dimension(15, 15)));         p.add(cppanel);         p.add(box.createrigidarea(new dimension(100, 300)));     }       // line 142 202 creating grid     public void stringarray(string[][] labeltext) {         int x = 1; // increment rows          (int = 0; < labeltext.length; i++) { // x             (int j = 0; j < labeltext.length; j++) { // y                 labeltext[i][j] = integer.tostring(x); // populate string                 x++;             }         }     }      public void jlabelarray(jlabel[][] label, string[][] labeltext) {          (int = 0; < label.length; i++) { // x             (int j = 0; j < label.length; j++) { // y                 label[i][j] = new jlabel();                 label[i][j].settext(labeltext[i][j]);                 label[i][j].setopaque(false);                 label[i][j].setborder(borderfactory.createlineborder(new color(                         0, 155, 200), 1));                 // label[i][j].setbackground(color.white);              }         }     }      public void populategrid(jpanel grid, jlabel[][] label) { // add labels                                                                 // panel,          string x1[][] = new string[rows][columns];         stringarray(x1);         jlabelarray(label, x1);          grid.setbackground(color.red);          int gheight = label.length, gwidth = label.length;         grid.setlayout(new gridlayout(gwidth, gheight));          (int = 0; < label.length; i++) { // x             (int j = 0; j < label.length; j++) { // y                 grid.add(label[i][j]);              }         }     }      public void creategrid(jpanel finalpanel, jpanel grid) {          // add grid scroll pane         jscrollpane x4 = new jscrollpane(grid);         x4.setpreferredsize(new dimension(600, 600)); // not delete this.         x4.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_always);         x4.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);          // add scroll pane panel border         finalpanel.setbackground(new color(153, 153, 204));         finalpanel.setborder(borderfactory.createemptyborder(50, 25, 50, 50));         finalpanel.add(x4);      }      // variables creaetegui method.     static menupanel t = new menupanel();     static jpanel menu = new jpanel();     static jpanel finalpanel = new jpanel();     static jpanel gridpanel = new jpanel();     static jlabel labels[][] = new jlabel[rows][columns];      public static void creategui() {           t.createmenu(menu);         t.populategrid(gridpanel, labels);         t.creategrid(finalpanel, gridpanel);          jframe f = new jframe();         f.settitle("project testing");         f.setdefaultcloseoperation(jframe.exit_on_close);         f.setvisible(true);         f.setlocation(100, 100);         f.setalwaysontop(true);         f.setsize(500, 500);          f.add(finalpanel, borderlayout.center);         f.add(menu, borderlayout.west);          f.pack();      }      public static void main(string args[]) {          creategui();          t.imageloader();         labels[2][2].seticon(new imageicon(west));       }  } 

thank much! appreciate or suggestions :d

as said need override paintcomponent method of jpanel , put drawimage(...) in there. so:

@override public void paintcomponent(graphics g) {     //super.paintcomponent(g);    g.drawimage(image, 0, 0, null); } 

where image instance of class image loaded in initialization code (don't load in paintcomponent, slow , want load once).

there 2 ways accomplish that:

  • make own class extending jpanel , put code there. want create method setbackgroundimage(image) can call main class pass image loaded disk.

  • make anonymous class, doing similar without explicitely defining new class. instead of creating panel this:

    jpanel gridpanel = new jpanel(); 

    do this:

    jpanel gridpanel = new jpanel() {     @override     public void paintcomponent(graphics g)     {         //super.paintcomponent(g);        g.drawimage(image, 0, 0, null);     } }; 

of course must in actual code (not static initialization) since want make sure load image before.

finally couple of suggestions:

  • variable names start in lower case convention (as opposite class names start in upper case). don't example in jpanel grid argument , comands field.

  • you violating swing's single threading rule. is, must call invokelater in main wrapping gui initializing code. example @ swing's hello world. can find detailed explanation of here.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -