tags:

views:

140

answers:

5

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

A: 

Start here :

http://java.sun.com/docs/books/tutorial/uiswing/

Amir Afghani
+3  A: 

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.

Bill the Lizard
A: 

A paintbrush, some ink and some careful and loving application of ink to monitor and you'll be set for life.

devilcrack
+1  A: 

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

novalis
+1 Not useful, but you made my day
Helper Method
-1 Not useful, but you made my day too :)
OscarRyz
+1 Not useful on some consoles, but quite useful in a `JComponent`.
trashgod
+3  A: 

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

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

OscarRyz