java - Using a variable with paintComponent -
for homework assignment, trying paint box paintcomponent
using user inputed variables. have been able build need using fixed numbers. have been working on day , have not been able find way implement variables. here stripped down version of working on:
import javax.swing.*; import java.awt.*; public class problem3 extends jframe{ public static void main(string[] args) { int xcoord = integer.parseint( joptionpane.showinputdialog("enter x cord.")); jframe gd = new jframe(); gd.setlocationrelativeto(null); gd.setsize(300, 300); gd.setdefaultcloseoperation(jframe.exit_on_close); gd.setvisible(true); gd.add(new newpanel()); } public int getx(){ return xcoord; } } class newpanel extends jpanel { int xcoord = getx(); protected void paintcomponent(graphics g){ super.paintcomponent(g); g.drawrect(10,10,xcoord,50); } }
edit
since posting here have tried using setter method inside newpanel class
public void setx() { xcoord = integer.parseint( joptionpane.showinputdialog("enter x cord.")); }
running lead strange error have never seen before: stackoverflow error , dialog box pops multiple/hundreds of times , won't stop popping up.
edit 3 using hovercraft full of eels solution, got work! thank full of eels time, help, , patience.
public class problem3 extends jframe{ public static void main(string[] args) { jframe gd = new jframe(); gd.setlocationrelativeto(null); gd.setsize(300, 300); gd.setdefaultcloseoperation(jframe.exit_on_close); gd.setvisible(true); newpanel panel = new newpanel(); panel.setx(50); gd.add(panel); } } class newpanel extends jpanel { int xcoord; public void setx(int x){ xcoord = x; } public int getx(){ return xcoord; } protected void paintcomponent(graphics g){ super.paintcomponent(g); g.drawrect(10,10,xcoord,50); } }
just more basic java classes use mutator , accessor methods, known setters , getters, change state of object. in situation need give newpanel class setter method, public void setxcoor(int x) {...}
, perhaps similar 1 ycoord if need outside classes (the gui holds newpanel object) can change newpanel's values. once xcoord has been changed, you'll want call repaint() on same newpanel object paintcomponent method can called jvm displaying effects of changed xcoord value.
edit 1
question: why problem3 class extend jframe? seems unnecessary here.
edit 2
also, not code in main method:
int xcoord = integer.parseint( joptionpane.showinputdialog("enter x cord.")); jframe gd = new jframe(); gd.setlocationrelativeto(null); gd.setsize(300, 300); gd.setdefaultcloseoperation(jframe.exit_on_close); gd.setvisible(true); gd.add(new newpanel());
but rather in constructor of full-fledged class.
also, i'd give class newpanel class field, , i'd put object held field jframe allowing me have reference newpanel object can call it's methods elsewhere in class.
edit 3
regarding code:
public void setx() { xcoord = integer.parseint( joptionpane.showinputdialog("enter x cord.")); }
i wouldn't user input in drawing class. instead i'd setter method true setter method, similar ones i'm sure you've made many times before , in form:
public void setx(int xcoord) { // set field in here }
then user interaction, joptionpane or whatever in main gui or elsewhere in program. once user gives input, call setter method above on newpanel variable passing in user's input (as int of course).
edit 4
regarding code:
public static void main(string[] args) { // .... gd.add(new newpanel()); newpanel.setx(50); }
you're calling setx method on newpanel class, not on newpanel object, why compiler appropriately complaining. why suggested in 1 of edits above create newpanel variable in gui class, create gui in gui class's constructor, not in main method (which should call gui class's constructor), , use same newpanel variable place gui's jframe , call setx(...) method on.
Comments
Post a Comment