如何在Java中绘制各种形状?我应该使用哪个库? [英] How do I draw various shapes in Java ? Which library should I use?

查看:128
本文介绍了如何在Java中绘制各种形状?我应该使用哪个库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个程序,可以绘制我分配给它的任何类型的形状,如

I want to write a program which can draw any type of shape that I assign to it like


  1. Circle

  2. Square

  3. 矩形

我应该使用哪个库,以及如何使用我是用Java做的吗?

Which library should I use , and how do I go about it in Java ?

我是一名python编码器,因此难以应对Java。

I am a python coder , thus finding it difficult to cope with Java .

推荐答案

当然可以使用Swing来做到这一点。您可能需要查看Java的 Shape 库。

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

或者你可以简单地覆盖Component的paint方法,如下所示。

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

这篇关于如何在Java中绘制各种形状?我应该使用哪个库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆