简体   繁体   English

Java Paint程序

[英]Java Paint Program

I have started a Java Paint program that seems to be working fine... There is just one problem. 我已经启动了一个Java Paint程序,该程序似乎运行正常……只有一个问题。 In my program I have it set up so that it repaint()'s ovals using MouseListener methods and overrides paintComponent(Graphics g). 在我的程序中,我对其进行了设置,以便使用MouseListener方法重新绘制()的椭圆并覆盖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. PS请记住,我更喜欢使用fillOval方法而不是drawLine,因为我仍然想设置笔触。

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. 您应该存储鼠标的前一个位置,并在最后一个位置和当前位置之间进行插值以创建Line2D形状。 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. 如果您确实要继续绘制椭圆,则可以沿起点/终点之间的线进行插值,并在一个循环中绘制多个椭圆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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