i'm working on schoolproject involving parkinggarage. have simulator working reason can't seem able fix gui. note first java project ever , i'm new programming in general.
public class simulatorview extends jframe { private carparkview carparkview; private int numberoffloors; private int numberofrows; private int numberofplaces; private car[][][] cars; private jbutton button1; private jbutton button2; private jlabel label; private simulator sim; public simulatorview(int numberoffloors, int numberofrows, int numberofplaces, simulator sim) { this.numberoffloors = numberoffloors; this.numberofrows = numberofrows; this.numberofplaces = numberofplaces; this.sim = sim; cars = new car[numberoffloors][numberofrows][numberofplaces]; carparkview = new carparkview(); button1 = new jbutton("run 1 time"); button2 = new jbutton("run 100 times"); label = new jlabel("test bericht"); event = new event(); button1.addactionlistener(a); event2 b = new event2(); button2.addactionlistener(b); // dimension d = new dimension(100,100); // button1.setbounds(20,30,50,30); // button2.setbounds(20,30,50,30); // jframe jf = new jframe("parking simulator"); jf.setlayout(new gridlayout(5,5)); jf.setdefaultcloseoperation(jframe.exit_on_close); jf.setbounds(10,10,1024,1024); // container contentpane = jf.getcontentpane(); //contentpane.add(steplabel, borderlayout.north); contentpane.add(carparkview, borderlayout.center); contentpane.add(button1,borderlayout.east); contentpane.add(button2,borderlayout.west); //contentpane.add(population, borderlayout.south); pack(); jf.setvisible(true); updateview(); } public class event implements actionlistener { public void actionperformed(actionevent a){ sim.singletick(); } } public class event2 implements actionlistener { public void actionperformed(actionevent b){ thread querythread = new thread(); { sim.hundredtick(); } } } public void updateview() { carparkview.updateview(); } public int getnumberoffloors() { return numberoffloors; } public int getnumberofrows() { return numberofrows; } public int getnumberofplaces() { return numberofplaces; } public car getcarat(location location) { if (!locationisvalid(location)) { return null; } return cars[location.getfloor()][location.getrow()][location.getplace()]; } public boolean setcarat(location location, car car) { if (!locationisvalid(location)) { return false; } car oldcar = getcarat(location); if (oldcar == null) { cars[location.getfloor()][location.getrow()][location.getplace()] = car; car.setlocation(location); return true; } return false; } public car removecarat(location location) { if (!locationisvalid(location)) { return null; } car car = getcarat(location); if (car == null) { return null; } cars[location.getfloor()][location.getrow()][location.getplace()] = null; car.setlocation(null); return car; } public location getfirstfreelocation() { (int floor = 0; floor < getnumberoffloors(); floor++) { (int row = 0; row < getnumberofrows(); row++) { (int place = 0; place < getnumberofplaces(); place++) { location location = new location(floor, row, place); if (getcarat(location) == null) { return location; } } } } return null; } public car getfirstleavingcar() { (int floor = 0; floor < getnumberoffloors(); floor++) { (int row = 0; row < getnumberofrows(); row++) { (int place = 0; place < getnumberofplaces(); place++) { location location = new location(floor, row, place); car car = getcarat(location); if (car != null && car.getminutesleft() <= 0 && !car.getispaying()) { return car; } } } } return null; } public void tick() { (int floor = 0; floor < getnumberoffloors(); floor++) { (int row = 0; row < getnumberofrows(); row++) { (int place = 0; place < getnumberofplaces(); place++) { location location = new location(floor, row, place); car car = getcarat(location); if (car != null) { car.tick(); } } } } } private boolean locationisvalid(location location) { int floor = location.getfloor(); int row = location.getrow(); int place = location.getplace(); if (floor < 0 || floor >= numberoffloors || row < 0 || row > numberofrows || place < 0 || place > numberofplaces) { return false; } return true; } private class carparkview extends jpanel { private dimension size; private image carparkimage; /** * constructor objects of class carpark */ public carparkview() { size = new dimension(0, 0); } /** * overridden. tell gui manager how big be. */ public dimension getpreferredsize() { return new dimension(800, 400); } /** * overriden. car park view component needs redisplayed. copy * internal image screen. */ public void paintcomponent(graphics g) { if (carparkimage == null) { return; } dimension currentsize = getsize(); if (size.equals(currentsize)) { g.drawimage(carparkimage, 0, 0, null); } else { // rescale previous image. g.drawimage(carparkimage, 0, 0, currentsize.width, currentsize.height, null); } } public void updateview() { // create new car park image if size has changed. if (!size.equals(getsize())) { size = getsize(); carparkimage = createimage(size.width, size.height); } graphics graphics = carparkimage.getgraphics(); for(int floor = 0; floor < getnumberoffloors(); floor++) { for(int row = 0; row < getnumberofrows(); row++) { for(int place = 0; place < getnumberofplaces(); place++) { location location = new location(floor, row, place); car car = getcarat(location); color color = car == null ? color.white : color.red; drawplace(graphics, location, color); } } } repaint(); } /** * paint place on car park view in given color. */ private void drawplace(graphics graphics, location location, color color) { graphics.setcolor(color); graphics.fillrect( location.getfloor() * 260 + (1 + (int)math.floor(location.getrow() * 0.5)) * 75 + (location.getrow() % 2) * 20, 60 + location.getplace() * 10, 20 - 1, 10 - 1); // todo use dynamic size or constants } } }
according constraints want use borderlayout
. not set borderlayout
layout manager. setlayout(new borderlayout())
on content panel.
Comments
Post a Comment