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←");
精彩评论