开发者

How to Draw a Horizontal Line in Java Swing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

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 question

How 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜