tags:

views:

117

answers:

2

how to draw horizontal line in java swing?

A: 

Just like drawing any other line, except the value for the y axis doesn't change.

Noel M
Actually this is only true in euclidean space :)
Romain Hippeau
+2  A: 

Depending on your use case, one of these tutorials should help you:

  • The Java Graphics API Tutorial => drawing in a swing component
  • The Java Swing Tutorial => using swing components

Here an example class which draws a Black line

public class MyLine extends JPanel()
{

     @Override public void paint(Graphics g)
     {
         //Get the current size of this component
         Dimension d = this.getSize();

         //draw in black
         g.setColor(Color.BLACK);
         //draw a centered horizontal line
         g.drawLine(0,d.height/2,d.width,d.height/2);
     }
}
josefx