java - previously displayed images disappear after mouse-click -
i'm programming gui network-able battleships-like game (yes me too). if right-click set waypoint maplabels disappear , don't it, since right-click should send string server , not in way influence display itself. if call panel.repaint(); pane.validate(); after setting waypoint displayed again, performance coming close impossibly slow that.
i realize, lot of code, can't imagine problem beeing in fieldlistener, though must be. therefore have no idea post. if want see else, ask it...
here's part of our code, responsible problem:
/** * listener field. * @author andris * */ private class fieldlistener implements mouselistener { private int[] xy; private int[] pressedpos = new int[] {-1,-1}; @override public void mousepressed(mouseevent evt) { field field = map.getfield(xy[0],xy[1]); pressedpos = xy.clone(); switch(evt.getbutton()){ case mouseevent.button1://-------well, button1...... break; case mouseevent.button3: switch(selbtn){ case 2://---------------this case client.out("some string");// sends server, nothing else... break; } break; } }
after server answers executed (in totally different classes different packages, , disappearing fields private, though):
public class server { client.out(cfg.chat_server+"you have set waypoint @ ("+x+","+y+")."); public class chatframe extends jpanel { public void out(string nextstring){ text.append(" "+nextstring); text.append("\n"); jscrollbar scroll = display.getverticalscrollbar(); if(!scroll.getvalueisadjusting())scroll.setvalue(integer.max_value); }
here's custom paint method (maybe imageobserver wrong):
private class maplabel extends jlabel{ //------------- in mapframe private field field; private int[] xy; public void paint(graphics g){ image image = getimagensettooltip(); graphics2d g2d=(graphics2d)g; g2d.drawimage(image, 0, 0, actimgsize, actimgsize, this); g2d.setclip(0, 0, actimgsize, actimgsize); super.paint(g2d); } /** * gets proper image , sets tool-tip * @return */ private image getimagensettooltip(){ image result; string tooltip = "("+xy[0]+","+xy[1]+")"; tiletype type = field.gettype(); switch(type){ case harbor: result=harborimg[0]; break; case land: //------------------------------etc... this.settooltiptext(tooltip); return result; }
here's of rest:
...lots of imports... /** * frame in gamemap displayed. * @author andris * */ @suppresswarnings("serial") public class mapframe extends jpanel { ...lots of variables... /** * creates frame, doesn't make visible yet. * @param window gamewindow in frame embedded. * @param client client runs this. */ public mapframe(gamewindow window, client client){ ...lots of variables initialized... panel = new jpanel(); pane = new jscrollpane(panel, jscrollpane.vertical_scrollbar_as_needed, jscrollpane.horizontal_scrollbar_as_needed); buttons = new jbutton[nbuttons]; buttonlistener = new buttonlistener(); for(int i=0; i<nbuttons; i++){ buttons[i] = new jbutton(buttontexts[i]); buttons[i].setname(buttontexts[i].replace(' ', '_')); buttons[i].addactionlistener(buttonlistener); buttonpanel.add(buttons[i]); }
there problem, paint code
public void paint(graphics g){ image image = getimagensettooltip(); graphics2d g2d=(graphics2d)g; g2d.drawimage(image, 0, 0, actimgsize, actimgsize, this); g2d.setclip(0, 0, actimgsize, actimgsize); super.paint(g2d); }
painting before super.paint
can have potential of wiping out have painted. occurs because paint
calls paintcomponent
, paintborder
, paintcomponents
.
one of jobs of paintcomponent
prepare graphics context painting (cleaning up)
you should, also, avoid messing clip. because of way painting works in swing, run risk of allowing component paint beyond physical boundaries. clip set repaint manager equal size of component before paint
called
instead, should using paintcomponent
perform custom painting
take @ perofming custom painting more details
now, question raises mind, why painting custom image on jlabel
, when 1 of features of jlabel
display icon?
Comments
Post a Comment