在Java中的JFrame上拖动矩形
我想根据 mousedrag 事件绘制矩形。 如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增大或减小。 我有以下代码。
在下面的代码中,我使用 SelectionArea 类,它扩展了我正在其上执行绘图操作的画布。 我在此类中使用图像变量进行双缓冲,以减少闪烁并保存小程序的先前状态(即绘制小程序的内容),
但如果我绘制第一个矩形,则代码工作正常。 如果我开始绘制第二个矩形,则先前绘制的矩形就会消失。 我希望先前绘制的矩形出现在屏幕上,
请告诉我如何解决这个问题。
I want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates.
i have the following code.
in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous state(i.e drawing content of applet)
but the code is working fine if i draw first rectangle. if i start to draw second rectangle the previously drawn rectangle is disappearing. i want the previously drawn rectangle to be on the screen
plz tell me how to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要做的是将之前绘制的矩形保存在某种数据结构中,以便稍后可以再次绘制。
这段代码(抱歉长度太长,将执行与您所描述的类似的操作。
要使用它,只需将
JPanel
放入JFrame
中即可。What you need to do, is save the previously drawn rectangle in some sort of data structure, so you can draw it again later.
This code (sorry about the length, will do something similar to what you are describing.
To use it, just slap the
JPanel
inside of aJFrame
.自定义绘画方法展示了执行此操作的两种技术。
Custom Painting Approaches shows two techniques for doing this.