java - Jpanel repaint with mouse movement event -
ok here thing making function grapher , want when move mouse on line show coordinates. set basic drawstring on panel show mouse coords can not repaint normally. seems painting fine not clearing contents before painting here code have far.
public class grapher extends jpanel implements mousemotionlistener{ private final int border_gap = 15; private final int max_score = 20; private final int pref_width = 800; private final int pref_height = 650; private final int graph_point_width = 12; private static final int graph_interval = 15; private static point mse; private list<point> values; public grapher(list<point> values) { setmse(new point(0,0)); this.values = values; addmousemotionlistener(this); } public void paintcomponent(graphics g){ super.paintcomponents(g); graphics2d g2d = (graphics2d) g; g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); float xscale = ((float) getwidth() - 2 * border_gap) / (values.size()-1); float yscale = ((float) getheight() - 2 * border_gap) / (max_score-1); g2d.drawstring(xscale+"", 50, 50); g2d.drawstring(yscale+"", 50, 70); g2d.drawstring(getwidth()/2+"", 50, 90); g2d.drawstring(mse.x +" " + mse.y, 50, 30); //create axis g2d.setcolor(new color(0x7e7e7e)); //x line g2d.drawline(border_gap, (getheight() - border_gap)/2, getwidth() - border_gap, (getheight() - border_gap)/2); //y line g2d.drawline(getwidth()/2, getheight() - border_gap, getwidth()/2, border_gap); } public dimension getpreferredsize(){ return new dimension(pref_width, pref_height); } private static void createandshowgui(){ list<point> values = new arraylist<point>(); int maxdatapoints = 20; (int = -graph_interval; <= graph_interval; i++) { string val = ""; try { val = equationsolver.solveequation(variablereplace('x', i)); } catch (exception e) { val ="0"; e.printstacktrace(); } system.out.println(i + "= " + val); values.add(new point(i, integer.parseint(val))); } grapher panel = new grapher(values); jframe frame = new jframe("grapher"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(panel); frame.pack(); //frame.setlocationbyplatform(true); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable(){ public void run(){ createandshowgui(); } }); } @override public void mousemoved(mouseevent e) { setmse(new point(e.getx(), e.gety())); removeall(); repaint(); } @override public void mousedragged(mouseevent e) { setmse(new point(e.getx(), e.gety())); //repaint(); } ok here images code doing top numbers mouse position. fresh start program: http://snag.gy/bfruj.jpg. after move mouse around little: http://snag.gy/lnqie.jpg
thanks help.
i suggest major problem having related g.dispose()
the graphics context shared, meaning if dispose of graphics context, nothing else can paint it. unless create context, should never dispose it
your other problem is, not calling super.paintcomponent, responsible preparing graphics context painting (by clearing painted), instead, calling super.paintcomponents(g) ... not s @ end...
Comments
Post a Comment