开发者

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

开发者 https://www.devze.com 2022-12-21 09:17 出处:网络
How can I write a Java prog开发者_Go百科ram to draw a box, an oval, and an arrow?You\'ll find what you\'re looking for in the Java 2D Graphics Tutorial.The section on Drawing Geometric Primitives show

How can I write a Java prog开发者_Go百科ram to draw a box, an oval, and an arrow?


You'll find what you're looking for in the 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:

sample 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. It would help you to understand what's going on.

Finally for "advanced" painting these article is a must read: 2D graphics


System.out.println("■O←");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号