i trying make zoomin/zoomout button work on gui. have 2(i have others not needed post) classes , actionlistener/buttons in one, , zoom method.
i having trouble calling zoom method when zoomin/zoomout button pressed.
class 1:(im importing gui, didnt want take room on here)
@suppresswarnings("serial") public class partyparkingguipanel extends jpanel implements actionlistener { private final int default_width = 800; private final int default_height = 600; private final int default_grid_width = default_height - 50; //panels private jpanel mappanel = new jpanel(); private jpanel inputpanel = new jpanel(); private jpanel setuppanel = new jpanel(); private jpanel statuspanel = new jpanel(); private jpanel setupbuttonspanel = new jpanel(); private jpanel setuptextfieldpanel = new jpanel(); private jpanel controlpanel = new jpanel(); //labels private jlabel gridsizelabel = new jlabel("grid size:"); private jlabel numcarslabel = new jlabel("#of cars:"); private jlabel numparkingspotslabel = new jlabel("#parking spots:"); //buttons private jbutton randomgrid = new jbutton("random grid"); private jbutton readfile = new jbutton("read file"); private jbutton resetgrid = new jbutton("reset grid"); private jbutton zoomin = new jbutton("zoom in"); private jbutton zoomout = new jbutton("zoom out"); private jbutton start = new jbutton("start"); private jbutton step = new jbutton("step"); private jbutton pause = new jbutton("pause"); //text area , citymap scrollpane private jtextarea status = new jtextarea("enter grid size, #cars, #parking spots" + " \nclick on random button generate random cars , spots" + " \nclick on reset grid clear grid",20, 20); private citymap citymap; private jscrollpane mapscroller = new jscrollpane(citymap); private jscrollpane statusscroller = new jscrollpane(status); //text fields private jtextfield gridsizefield = new jtextfield("10",12); private jtextfield numparkingspotsfield = new jtextfield("0",12); private jtextfield numcarsfield = new jtextfield("0",12); private int gridsize = parkingsimulation.default_grid_size; private int numcars; private int numspots; private arraylist<car> cars; private parkingsimulation simulator; private boolean showpaths = true; private int speedfactor = 1; private boolean animate = false; private int delay = 500; // milliseconds /** * set panel. */ public partyparkingguipanel() { setpreferredsize(new dimension(1200, 700)); simulator = new parkingsimulation(); simulator.setgridsize(gridsize); cars = simulator.getcars(); citymap = new citymap(parkingsimulation.default_grid_size, default_grid_width, simulator); citymap.setshowpaths(showpaths); zoomin.addactionlistener(this); zoomout.addactionlistener(this); startanimation(); } .... /* (non-javadoc) * @see java.awt.event.actionlistener#actionperformed(java.awt.event.actionevent) */ @override public void actionperformed(actionevent e) { if(e.getsource() == zoomin){ ?? } } .... class 2 has zoom method:
public class citymap extends jpanel { private int gridsize; private final int default_grid_width = 1024; private int displaysize = default_grid_width; private int blocksize; private int carsize; private int offset; private int numblocks; private parkingsimulation simulator; private arraylist<car> cars; private arraylist<parkingspot> spots; private boolean showpaths = false; private boolean flag = true; private final int path_thickness = 2; /** * draw city grid. * @param gridsize * @param displaysize */ public citymap(int gridsize, int displaysize, parkingsimulation simulator) { super(); this.simulator = simulator; this.cars = simulator.getcars(); this.spots = simulator.getspots(); this.displaysize = displaysize; this.gridsize = gridsize; setparameters(gridsize); setbackground(color.white); setpreferredsize(new dimension(displaysize, displaysize)); setdoublebuffered(true); //makes drawing smoother } /** * sets parameters drawing grid * @param size */ private void setparameters(int size) { numblocks = size; blocksize = displaysize/(numblocks + 1); offset = blocksize; carsize = blocksize/4; } /** * zoom factor (1 , higher) */ public void zoom(int factor) { setparameters(gridsize/factor); }
okay, 1 thing noticed both classes extend jpanel. i'm not entirely sure, think can cause form of runtime error within program because interfering, if that's not issue, ignore that. in order call method, or public, 1 class another, must create instance variable of class, or aka, object of class. in order this, type name of class you're using, such as
citymap objectofcitymap = new citymap(); then this:
@override public void actionperformed(actionevent e) { if(e.getsource() == zoomin){ // using object access method or function other class objectofcitymap.zoom(// factor amount); } } now, usually, wouldn't have such long variables objectofcitymap, show how object used. on how use variables. hope helps.
Comments
Post a Comment