How to Draw a Horizontal Line in Java Swing [closed]
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this questionHow does 开发者_如何学编程one draw a horizontal line using the Java Swing library? I know that I need to override paint(Graphics g)
but I am not sure what to put in the method.
@Override
public void paint(Graphics g)
{
// What goes here?
}
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 (such as JSeparator)
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);
}
}
Just like drawing any other line, except the value for the y
axis doesn't change.
精彩评论