java - Draw circle only, not ellipse -
i have below code draws ellipse when user drags mouse, want circle drawn , not ellipse also.
i've tried few things haven't quite got work, understand places circle inside rectangle, if can lock places inside square should want? code may not efficient, i'm trying write project i'm working, clear not school homework or anything. if can @ least circle aspect of code working save me lot of time in project
import java.awt.alphacomposite; import java.awt.basicstroke; import java.awt.borderlayout; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.renderinghints; import java.awt.shape; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import java.awt.geom.line2d; import java.awt.geom.ellipse2d; import java.util.arraylist; import javax.swing.jcomponent; import javax.swing.jframe; public class drawingboardwithmatrix extends jframe { public static void main(string[] args) { drawingboardwithmatrix drawingboardwithmatrix = new drawingboardwithmatrix(); } public drawingboardwithmatrix() { this.setsize(600, 600); this.setdefaultcloseoperation(jframe.exit_on_close); this.add(new paintsurface(), borderlayout.center); this.setvisible(true); } private class paintsurface extends jcomponent { arraylist<shape> shapes = new arraylist<>(); point startdrag, enddrag; public paintsurface() { this.addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { startdrag = new point(e.getx(), e.gety()); enddrag = startdrag; repaint(); } @override public void mousereleased(mouseevent e) { shape r = makecircle(startdrag.x, startdrag.y, e.getx(), e.gety()); shapes.add(r); startdrag = null; enddrag = null; repaint(); } }); this.addmousemotionlistener(new mousemotionadapter() { @override public void mousedragged(mouseevent e) { enddrag = new point(e.getx(), e.gety()); repaint(); } }); } private void paintbackground(graphics2d g2){ g2.setpaint(color.light_gray); (int = 0; < getsize().width; += 10) { shape line = new line2d.float(i, 0, i, getsize().height); g2.draw(line); } (int = 0; < getsize().height; += 10) { shape line = new line2d.float(0, i, getsize().width, i); g2.draw(line); } } @override public void paint(graphics g) { graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); paintbackground(g2); g2.setstroke(new basicstroke(2)); g2.setcomposite(alphacomposite.getinstance(alphacomposite.src_over, 0.50f)); (shape s : shapes) { g2.setpaint(color.black); g2.draw(s); // g2.setpaint(colors[(colorindex++) % 6]); // g2.fill(s); } if (startdrag != null && enddrag != null) { g2.setpaint(color.light_gray); shape r = makecircle(startdrag.x, startdrag.y, enddrag.x, enddrag.y); g2.draw(r); } } private ellipse2d.float makecircle(int x1, int y1, int x2, int y2) { return new ellipse2d.float(math.min(x1, x2), math.min(y1, y2), math.abs(x1 - x2), math.abs(y1 - y2)); } } }
your makecircle()
draws ellipse width math.abs(x1 - x2)
, height math.abs(y1 - y2)
. if want draw circle, width , height must equal. there several ways accomplish this:
- use
math.abs(x1 - x2)
both width , height - use
math.abs(y1 - y2)
both width , height - decide between 2 based on mouse position
however, concerned statement "i don't have time right learn java properly" - will come bite you. if have intention of having programming serious hobby or potentially job down line, must expend lot of effort understand doing.
Comments
Post a Comment