java - How to draw rectangles from a JList Selection -
i'm looking regarding java program i'm writing. need display simple bar graph consisting of rectangles based off of data saved in array has been generated jlist selection event. however, nothing drawn in jpanel , cannot figure out how fix it.
list selection event (for reference)
try { bufferedreader in = new bufferedreader((new filereader("scores.txt"))); string sline; while ((sline = in.readline()) != null) { vecathlete.addelement(new stage3_datafile(sline)); } string[] saathlete = new string[vecathlete.size()]; (int ii = 0; ii < vecathlete.size(); ii++) saathlete[ii] = vecathlete.get(ii).sname; jlist_athlete.setlistdata(saathlete); jlist_athlete.setselectedindex(0); // reading input line line // close file in.close(); } catch (ioexception e) { system.out.println("error: not read text file!"); } jlist_athlete.addlistselectionlistener(new javax.swing.event.listselectionlistener() { public void valuechanged(javax.swing.event.listselectionevent e) { double[][] darun = new double[2][7]; final double dscoreused = 5; double dminscorerun1 = 10, dmaxscorerun1 = 0; double dminscorerun2 = 10, dmaxscorerun2 = 0; double dtotalrun1 = 0, dtotalrun2 = 0; jlabel_athlete.settext(vecathlete.get(jlist_athlete.getselectedindex()).sname); jlabel_country.settext(vecathlete.get(jlist_athlete.getselectedindex()).scountry); jlabel_run1_score_1.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[0]); jlabel_run1_score_2.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[1]); jlabel_run1_score_3.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[2]); jlabel_run1_score_4.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[3]); jlabel_run1_score_5.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[4]); jlabel_run1_score_6.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[5]); jlabel_run1_score_7.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun1[6]); jlabel_run2_score_1.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[0]); jlabel_run2_score_2.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[1]); jlabel_run2_score_3.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[2]); jlabel_run2_score_4.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[3]); jlabel_run2_score_5.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[4]); jlabel_run2_score_6.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[5]); jlabel_run2_score_7.settext(vecathlete.get(jlist_athlete.getselectedindex()).satemprun2[6]); darun[0][0] = double.parsedouble(jlabel_run1_score_1.gettext()); darun[0][1] = double.parsedouble(jlabel_run1_score_2.gettext()); darun[0][2] = double.parsedouble(jlabel_run1_score_3.gettext()); darun[0][3] = double.parsedouble(jlabel_run1_score_4.gettext()); darun[0][4] = double.parsedouble(jlabel_run1_score_5.gettext()); darun[0][5] = double.parsedouble(jlabel_run1_score_6.gettext()); darun[0][6] = double.parsedouble(jlabel_run1_score_7.gettext()); darun[1][0] = double.parsedouble(jlabel_run2_score_1.gettext()); darun[1][1] = double.parsedouble(jlabel_run2_score_2.gettext()); darun[1][2] = double.parsedouble(jlabel_run2_score_3.gettext()); darun[1][3] = double.parsedouble(jlabel_run2_score_4.gettext()); darun[1][4] = double.parsedouble(jlabel_run2_score_5.gettext()); darun[1][5] = double.parsedouble(jlabel_run2_score_6.gettext()); darun[1][6] = double.parsedouble(jlabel_run2_score_7.gettext()); for(int = 0; < 7; i++) { if(darun[0][i] <= dminscorerun1) { dminscorerun1 = darun[0][i]; } if(darun[0][i] >= dmaxscorerun1) { dmaxscorerun1 = darun[0][i]; } dtotalrun1 += darun[0][i]; } dtotalrun1 = (dtotalrun1 - dminscorerun1 - dmaxscorerun1) / dscoreused; jlabel_totalrun1.settext(string.valueof(dtotalrun1)); jlabel_run1addinfo.settext("min score: " + (dminscorerun1) + ", max score: " + (dmaxscorerun1)); for(int = 0; < 7; i++) { if(darun[1][i] <= dminscorerun2) { dminscorerun2 = darun[1][i]; } if(darun[1][i] >= dmaxscorerun2) { dmaxscorerun2 = darun[1][i]; } dtotalrun2 += darun[1][i]; } dtotalrun2 = (dtotalrun2 - dminscorerun2 - dmaxscorerun2) / dscoreused; jlabel_totalrun2.settext(string.valueof(dtotalrun2)); jlabel_run2addinfo.settext("min score: " + (dminscorerun2) + ", max score: " + (dmaxscorerun2)); if(dtotalrun1 >= dtotalrun2) jlabel_finalscore.settext(string.valueof(dtotalrun1)); else jlabel_finalscore.settext(string.valueof(dtotalrun2)); jpanel_graph.repaint(); } });
jpanel definitions
jpanel jpanel_graph = new jpanel(); jpanel_graph.setborder(new bevelborder(bevelborder.lowered, null, null, null, null)); jpanel_graph.setbounds(729, 159, 700, 200); frame.getcontentpane().add(jpanel_graph); img = new bufferedimage(jpanel_graph.getwidth(), jpanel_graph.getheight(), bufferedimage.type_int_rgb); g2dimg = (graphics2d)img.getgraphics(); // draw filled white coloured rectangle on entire area clear it. g2dimg.setpaint(color.white); g2dimg.fill(new rectangle2d.double(0, 0, img.getwidth(), img.getheight()));
jpanel drawing event
class myjpanel extends jpanel { private rectangle2d.double rectangle; public void paintcomponent(graphics g) { super.paintcomponent(g); for(int = 0; < 7; i++) { dheight = darun[0][i]; rectangle = new rectangle2d.double(dx, dy, dwidth, dheight); g2dimg.setpaint(color.red); g2dimg.draw(rectangle); g2dimg.fill(rectangle); dx += dxincrement; g2dimg.drawimage(img, 0, 0, null); } } }
thanks in advance!
so, can tell, there no way "graph" know should paint. basic idea is, "graph" should focused on painting data/model, need way tell when data changes.
there lots of ways might this, example, devise model, add/remove data to, trigger updates graph pane listeners , updates new data.
or pass data want updated directly graph pane, in case, might simplest solution available you
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.rectangle2d; import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.listselectionevent; import javax.swing.event.listselectionlistener; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private graphpane graphpane; public testpane() { setlayout(new borderlayout()); defaultlistmodel<double> listmodel = new defaultlistmodel<>(); (int index = 0; index < 100; index++) { listmodel.addelement(math.random()); } graphpane = new graphpane(); add(graphpane); jlist<double> listdata = new jlist<>(listmodel); add(new jscrollpane(listdata), borderlayout.west); listdata.addlistselectionlistener(new listselectionlistener() { @override public void valuechanged(listselectionevent e) { graphpane.setdata(listdata.getselectedvalueslist().toarray(new double[0])); } }); } } public class graphpane extends jpanel { private double[] data; public void setdata(double[] data) { this.data = data; repaint(); } @override public dimension getpreferredsize() { return new dimension(200, 200); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (data != null && data.length > 0) { graphics2d g2d = (graphics2d) g.create(); double barwidth = getwidth() / (double)data.length; int xpos = 0; (double value : data) { double height = (getheight() - 1) * value; rectangle2d bar = new rectangle2d.double(xpos, getheight() - height, barwidth, height); g2d.setcolor(color.green); g2d.fill(bar); g2d.setcolor(color.black); g2d.draw(bar); xpos += barwidth; } g2d.dispose(); } } } }
Comments
Post a Comment