java - Overriding paintComponent -
ok have been trying figure out past 26 hours guide , online no success.
all want draw oval when user clicks in paintpanel please can sleep :p
in paintapplet class:
private void paintpanelmouseclicked(java.awt.event.mouseevent evt) { // todo add handling code here: if(fillradiobutton.isselected()) { paintpanel.setbackground(jcolor.getcolor()); paintpanel.repaint(); } if(brushradiobutton.isselected()) { point componentpoint = paintpanel.getlocationonscreen(); paintpanel.add(new painter(componentpoint)); } }
painter class:
import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jframe; import javax.swing.jpanel; import java.awt.*; public class painter extends jpanel{ point component; public painter(point com) { component = com; } public void paintcomponent(graphics g) { super.paint(g); point cursorpoint= mouseinfo.getpointerinfo().getlocation(); //gets cursorpoint int compx = component.x; int compy = component.y; int curpx = cursorpoint.x; int curpy = cursorpoint.y; int finalx = curpx - compx; int finaly = curpy - compy; g.drawoval(finalx, finaly, 20, 20); } }
don't call
super.paint(g);
within paintcomponent
. result in infinite loop in paint chain mechanism. instead need
super.paintcomponent(g);
for better sooner consider posting sscce
Comments
Post a Comment