java - JFrame help - passing value into PaintComponent() -
i've been working on small "game" project while , haven't been able pass value paintcomponent
method in way.
here's program. used boolean test
parameter, paintcomponent
method not consider test
true
, not paint blue square.
how can fix this? appreciated.
class 1 (swing):
package swing; import javax.swing.*; import javax.*; import java.awt.*; import java.awt.event.*; public class swing { private static draw object = new draw(); public static void main(string[] args) { jframe frame = new jframe("roguelikelike mvp"); frame.setvisible(true); frame.setfocusable(true); frame.setsize(500,500); frame.setdefaultcloseoperation(jframe.exit_on_close);; frame.add(object); } static class move implements keylistener{ public boolean isfocustraversable ( ) { return true; } public void keypressed(keyevent e) { int x = 10; int y = 10; char c = e.getkeychar(); if(c == 's') y+=10; if(c == 'a') x-=10; if(c == 'w') y-=10; if(c == 'd') x+=10; object.setalignmentx(x); object.setalignmenty(y); } public void keyreleased(keyevent e) {} public void keytyped(keyevent e) {} } }
class 2 (draw):
package swing; import javax.swing.*; import java.awt.*; public class draw extends jpanel { boolean test = true; private static final long serialversionuid = 1l; /*public void passmethod(draw object) { int x = (int) object.getalignmentx(); int y = (int) object.getalignmenty(); }*/ public void drawing(draw object){ draw thing = new draw(); thing.setx(object.getalignmentx()); int x = 55; repaint(); } private void setx(float alignmentx) { setx((int) alignmentx); } private void sety(float alignmenty) { sety((int) alignmenty); } public void paintcomponent(graphics g, boolean test){ super.paintcomponents(g); //int x = (int)object.getalignmentx(); //int y = (int)object.getalignmenty(); if(test==true) { g.setcolor(color.blue); g.fillrect(getx(),gety(),100,100); } } }
paintcomponent
can overloaded, overloaded version won't used paint component
. paintcomponent
isn't called user, painting routine. instead of overloading method should consider using variable test
, reading/updating variable. this:
private boolean test = true; @override public void paintcomponent(graphics g){ super.paintcomponent(g); if(test){ //the rest of code } }
Comments
Post a Comment