简体   繁体   English

如何编写Java程序来绘制方框,椭圆形和箭头?

[英]How can I write a Java program to draw a box, an oval, and an arrow?

如何编写Java程序来绘制方框,椭圆形和箭头?

You'll find what you're looking for in the Java 2D Graphics Tutorial . 您可以在Java 2D Graphics Tutorial中找到所需的内容。 The section on Drawing Geometric Primitives shows how to draw rectangles and ovals, and you should be able to put an arrow together after reading the Drawing Arbitrary Shapes section. 绘制几何基元 ”部分显示了如何绘制矩形和椭圆形,并且在阅读“ 绘制任意形状”部分后应该可以将箭头放在一起。

I understand it may be hard to start. 我了解可能很难开始。

So here it goes. 所以就到这里。

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

public class Homework {

     public static void main( String [] args ) {
         JFrame frame = new JFrame();
         frame.add( new JComponent() {
             public void paintComponent( Graphics g ) {
                // invoke "Graphics" methods here
             }
         });
         frame.setVisible( true );  
     }
}

Now, you just have to invoke the methods you need from the Graphics object and that will take charge of displaying the shapes in the screen: 现在,您只需要从Graphics对象调用所需的方法,它将负责在屏幕上显示形状:

sample http://img535.imageshack.us/img535/8784/capturadepantalla201002y.png 样本http://img535.imageshack.us/img535/8784/capturadepantalla201002y.png

That would help to start with something useful. 这将有助于从有用的东西开始。

To gain deeper understanding continue with this article: Painting in AWT and Swing . 为了获得更深入的了解,请继续阅读本文: AWT和Swing中的绘画 It would help you to understand what's going on. 这将帮助您了解发生了什么。

Finally for "advanced" painting these article is a must read: 2D graphics 最后,对于“高级”绘画,这些文章是必读的: 2D图形

System.out.println(“■O←”);

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

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