如何在Java中绘制各种形状?我应该使用哪个库?

发布于 2024-10-10 00:25:41 字数 1536 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

茶花眉 2024-10-17 00:25:41

当然,您可以使用 Swing 来做到这一点。您可能想研究一下 Java 的 Shape 图书馆。

或者,您可以简单地重写组件的绘制方法,如下所示。

替代文本

import javax.swing.*;
import java.awt.*;

public class ShapeTest extends JFrame{
     public ShapeTest(){
          setSize(400,400);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocationRelativeTo(null);
          setVisible(true);
     }

     public static void main(String a[]){
         new ShapeTest();
     }

     public void paint(Graphics g){
          g.drawOval(40, 40, 60, 60); //FOR CIRCLE
          g.drawRect(80, 30, 200, 200); // FOR SQUARE
          g.drawRect(200, 100, 100, 200); // FOR RECT
     }
}

Sure you can do that using Swing. You may want to look into Java's Shape library for that.

Alternatively you can simply override the Component's paint method as shown below.

alt text

import javax.swing.*;
import java.awt.*;

public class ShapeTest extends JFrame{
     public ShapeTest(){
          setSize(400,400);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocationRelativeTo(null);
          setVisible(true);
     }

     public static void main(String a[]){
         new ShapeTest();
     }

     public void paint(Graphics g){
          g.drawOval(40, 40, 60, 60); //FOR CIRCLE
          g.drawRect(80, 30, 200, 200); // FOR SQUARE
          g.drawRect(200, 100, 100, 200); // FOR RECT
     }
}
香橙ぽ 2024-10-17 00:25:41

Java2D API 可以满足您的需求。

The Java2D API has what you are looking for.

凉月流沐 2024-10-17 00:25:41

查看自定义绘画方法了解一些想法。 DrawOnComponent 更接近您想要的。需要更改它才能将自定义形状对象添加到列表中。

Check out Custom Painting Approaches for a couple of ideas. The DrawOnComponent is closer to what you want. It would need to be changed to add your custom shape objects to the list.

自此以后,行同陌路 2024-10-17 00:25:41

GraphPanel 是一个对象绘制程序的简单示例,其功能可移动、可调整大小、由边缘连接的彩色节点。

GraphPanel is a simple example of an object drawing program that features moveable, resizable, colored nodes connected by edges.

叶落知秋 2024-10-17 00:25:41

Java 2D 是您绘制图形所需要的(即,绘制彩色矩形、圆形、线条并为它们设置动画)。

Java 2D is what you neede drawing graphics (i.e., draw colored rectangles, circles, lines, and animate them).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文