Java Swing 自定义形状(2D 图形)
我需要绘制自定义形状。现在,当用户单击面板上的几个点时,我使用多边形创建一个形状。
public void mouseClicked(MouseEvent e) {
polygon.addPoint(e.getX(), e.getY());
repaint();
}
但我不知道这是否是绘制自定义形状的最佳方法。
应该可以编辑绘制的形状:
- 调整大小、
- 更改其填充颜色、
- 更改描边颜色、
- 复制/粘贴、
- 移动多边形的单个点
- ...
我见过人们创建一个自己的类来实现Shape 类并使用 GeneralPath。但我再次不知道这是否是一个好方法。
现在我可以使用多边形(或使用 GeneralPath)创建自己的形状,但我不知道如何将所有编辑功能附加到我自己的形状(编辑功能是指从上面调整大小、移动等) 。
我希望有人可以向我展示一种方法来做到这一点,或者编写一些代码来演示这一点。
提前致谢!!
I need to draw custom shapes. Now when a user clicks on several points on the panel I create a shape using a polygon.
public void mouseClicked(MouseEvent e) {
polygon.addPoint(e.getX(), e.getY());
repaint();
}
But I don't know if this is the best way to draw custom shapes.
It should be possible to edit a drawn shape:
- resize
- change its fill color
- change the stroke color
- copy/paste it
- move a single point of the polygon
- ...
I have seen people creating an own class implementing the Shape class and using a GeneralPath. But again I have no idea if this is a good way.
Now I can create my own shape with a polygon (or with a GeneralPath) but I have no clue how to attach all the edit functions to my own shape (the edit functions I mean the resize, move, etc from above).
I hope somebody could show me a way to do this or maybe write a little bit of code to demonstrate this.
Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在回答你的问题时,我肯定会做你所描述的 AWT 解决方案——这样你就可以跟踪创建的对象并能够允许用户将它们重新加载到编辑画布上,并且很可能每个形状用户创建的将是一个“层”,而不是 Layer Swing 容器,而是一个可以存储和跟踪绘制的形状并能够重绘它们的对象——要记住的主要事情是“绘制顺序”。基本上,您可以通过将“形状”的每个对象或对象组指定为 Z=[0-100] 等(100,可以是任何数字)来完成此操作,这决定了每个对象/形状的顺序被吸引,因此它们如何相互重叠。
基本上你需要一个形状类来存储
您概述了一个存储对象/管理器,它将枚举创建的形状类对象/实例。这些类或多或少地包含在 java.awt.Canvas 容器中,该容器将实际处理所有图形。
大多数情况下,您希望使用 awt 而不是 Swing,因为 Swing 不是线程安全的——这样您就不会在设计的早期“把自己逼到角落”。另一个原因是这是一个需要以用户习惯的方式进行响应和交互的实现。 Swing 是在 AWT 基础上构建的,并增加了很多像这样的应用程序不需要的复杂性。总而言之,您将创建一个类自定义组件,这正是 Canvas 对象的含义
如果 Sun 能够早点遵守这一策略,他们就不会进入
Swing 原来是一团糟……开发人员社区(包括我自己)正在努力创建 Swing 提供的许多“流畅”和基于组件的设计,但我们正在构建的完全是基于 AWT 的。当 Swing 出现时,Java 作为 GUI 平台变得非常复杂,并让 Sun 和 Java 走上了一条危险的道路……
此外,您还必须决定您最终想要什么,以控制您在这里创建的内容。如果您快速需要它并且并不真正关心将来修改它,那么有很多开源示例可以做到这一点 - 大多数是免费的。如果您想自己动手,那么希望我上面讨论的内容和下面的橡皮筋代码足以帮助您实现这一目标,并对 Java 作为 GUI 有更深入的了解。我个人希望你自己承担这个责任——这种语言迫切需要更多真正理解该语言及其设计的“核心”人员,而不仅仅是如何“工作”像 Hibernate 和 Spring 等框架......
祝你好运,希望这会有所帮助,
WM
就“橡皮筋”选择代码而言,这是我过去使用过的,只需考虑它 GLP 并根据需要使用它...
首先是监听器接口:
这是一个自定义 AWT 组件的类 - 它应该可以在 Swing/AWT 甚至 SWT 中使用
In answer to your question, I would definitely do what you describe as an AWT solution--that way you can track the objects created and be able to allow the user to reload them onto an edit Canvas and more than likely each of the shapes the user creates would be a "layer" not the Layer Swing Container, but an object that would store and track what shapes are drawn and be able to redraw them--the main thing to keep in mind is "Draw order". Basically you can do this by assigning each object or group of object that are your "shapes" to have a Z=[0-100] etc. (100, can be any number) which determine what order each of the object/shapes are drawn in and thus how they over lay each other.
Basically you are going to need a shape class that stores the
you outlined and a storage object/manager that will enumerate the shape class objects/instances that are created. This classes will more or less be contained by a java.awt.Canvas container that will actually handle all the graphics.
Mostly you want to use awt over Swing due to the fact Swing is not Thread safe--that way you do not "paint yourself in the corner" early on in your design. Another reason is this is an implementation that needs to respond and interact in a way that a user is used to. Swing is built over the AWT and adds a great deal of complexity that an application like this doesn't need. Over all you are going to be creating a class Custom component that is exactly what the Canvas object was mean to
provide and if Sun would have kept this tact earlier on they would not have gotten into the
mess that Swing turned out to be... The Developer community--myself included--were well on the way to creating a lot of what Swing offered in "slickness" and component based design, but what we were building was totally AWT based. When Swing entered the scene, Java as a GUI platform was greatly complicated and started Sun and Java down a slippery path...
Also you have to decide what you ultimately want as far as control over what you are creating here. If you need it fast and do not really care about modifying it in the future, then there are lots of open-source examples that can do this--most for free. If you want to do it yourself, then hopefully what I have talked about above and the rubber band code below will be enough to get you there and have a deeper understanding of Java as a GUI. I personally hope you take it on yourself--this language desperately needs more "core" people that truly understand the Language and its design and not just how to "work" frameworks like Hibernate and Spring among others...
Good luck hope this helps,
WM
As far as "Rubber-band" select code goes, this is mine I have used in the past, just consider it GLP and use it as you need to...
First is the Listener interface:
Here is the class which is a custom AWT component--it should be fine in either Swing/AWT probably even SWT
ImageJ 项目对 ImageJ 项目有一个特别好的实现。 nih.gov/ij/docs/tools.html" rel="nofollow noreferrer">具有可调整顶点的多边形工具,如此处 和此处。
The ImageJ project has a particularly nice implementation of a Polygon tool with adjustable vertices, as seen here and here.
你看过java中的Graphics类吗(还有一个Polygon类)?有绘制和填充多边形方法,每种方法都可以接受 x 坐标和 y 坐标数组。通过使用这些数组,您应该能够相当轻松地更改点的位置。就像您可以平等地更改所有这些点以重新调整大小一样,或者通过平等地移动所有点来复制和粘贴。更改颜色就像填充新颜色并重新绘制一样简单。
Have you looked at the Graphics class in java (there is also a Polygon class)? There are draw and fill polygon methods, and each can take in an array of x-coordinates and y-coordinates. By using these arrays, you should be able to change the positions of the points fairly easily. Like you could change all of them equally for a re-size, or copy and paste by just moving all points equally. Changing the color is as easy as filling it with a new color and repainting.