File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to Draw a Line in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Draw a Line in Java?" Watch "How to Draw a Line in Java?" New topic

How to Draw a Line in Java?

Avin Sinanan
Ranch Hand

Joined: Nov 07, 2001
Posts: 109
Ok how does one draw a simple line in java..
here is the cod ei have so far... i can draw a line but i am looking for a different method to draw it.. take a look at my code... its very short
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
class AvinLine
public static void main(String[] args)
JFrame frame = new JFrame();
JPanel pane = new JPanel();
Line2D myLine = new Line2D.Double(5,5,100,100);

Ok how do i draw the line without creating another class .. and the JPanel and JFrame must be in the same class.. I am trying to avoid making a new clas that extends JPanel. I do not want to add another class. The line must be added within this class. And i am also trying to avoid the "paint(Graphics g)" method. Is there anyway to add the line to the JPanel and then add the panel to the JFrame.
Because I find it dificult to believe that Java is such a powerful language that you have to call a "paint(Graphics g)" method and create another class just to draw a simple line.
Thanks for the help in advance..

yours respectfully<br />Avin Sinanan
Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
To draw to the screen, you must have a graphics context (object). To get this object to do it's work you have use it to draw a line (using it's draw line method). Perhaps you should find a good primer tutorial on Java's awt api in order to get a better understanding of the underlying mechanisms behind it's implementation. This will go long way to helping you answer many of your questions.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Hi, don't know if you specifically need to draw a line a specific way, but as long as it can go entirely accross your panel, try using a Seperator(). It worked great for me.

Happy Coding,
Gregg Bolinger

GenRocket - Experts at Building Test Data
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
Would this work, a sort of inner class ?

I agree. Here's the link:
subject: How to Draw a Line in Java?
It's not a secret anymore!