简体   繁体   English

使用Java 2D和Swing绘制图形

[英]Draw Graph using Java 2D and Swing

I want to know if it is easy to draw a graph (circle and arrows to link each circles) using Java2D and Swing. 我想知道使用Java2D和Swing绘制图形(用圆圈和箭头链接每个圆圈)是否容易。 And I want some tutorials or just small examples. 我想要一些教程或只是一些小例子。

Well, the API is quite easy to use, so it really depends on what you will be drawing... 好吧,API非常易于使用,因此它实际上取决于您要绘制的内容...

See this website: http://docs.oracle.com/javase/tutorial/2d/ 请访问以下网站: http : //docs.oracle.com/javase/tutorial/2d/

Well it depends on what you're doing. 好吧,这取决于您在做什么。 If you're only doing things like drawing a line from a point to another point using the click of a mouse, then it is quite simple. 如果您只是在做诸如通过单击鼠标在一个点到另一个点之间画一条线的事情,那么这很简单。 For something like that, all you would need to do is implement the 对于这样的事情,您所需要做的就是实现

 public void drawComponent(Graphics g){
      g.drawLine(/*xPosition of click*/,/*yPosition of click*/, null);
 }

method inside your class, and then implement MouseListener for the mouse click. 类中的方法,然后为鼠标单击实现MouseListener。 Just implement by doing 只需执行即可

 import ...
 public class ClassName implements MouseListener

and then the mouse listener onto whatever component you're using such as a JFrame, JPanel, or anything such as those. 然后将鼠标侦听器移到您正在使用的任何组件(例如JFrame,JPanel或诸如此类的组件)上。

Hope that helps 希望能有所帮助

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

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