简体   繁体   中英

Java Paint Program

I have started a Java Paint program that seems to be working fine... There is just one problem. In my program I have it set up so that it repaint()'s ovals using MouseListener methods and overrides paintComponent(Graphics g). The problem is when I move my mouse to fast it begins to separate my ovals instead of making one smooth line when the mouse is dragged. Is there a way to fix this.

PS Keep in mind that I much rather use the fillOval method not the drawLine, because I still would like to set the stroke.

Thanks in advance

See Custom Painting Approaches for the two common ways to do painting. The example draws a Rectangle without problems as the mouse moves.

You can still set a stroke to use to draw a line between two points. You should store the previous mouse position and interpolate between the last position and the current position to create a Line2D shape. Then create a stroke that has the desired width of your oval, and apply that stroke to Graphics context, then draw the line. This link has more information about strokes and shapes.

If you really want to continue drawing ovals, you could interpolate along the line between the start/end points and draw multiple ovals in a loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM