How do I draw various shapes in Java ? Which library should I use? [closed]

筅森魡賤 提交于 2019-12-01 09:40:44

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.

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
     }
}
Starkey

The Java2D API has what you are looking for.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!