简体   繁体   中英

How to draw vertical line in Swing

I am able to draw a horizontal line but unable to draw a vertical line. Please help me.

 import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;

class Success extends JFrame{

    public Success(){
        JPanel panel=new JPanel();
        getContentPane().add(panel);
        setSize(450,450);

        JButton button =new JButton("press");
        panel.add(button);
    }

    public void paint(Graphics g) {
        super.paint(g);  // fixes the immediate problem.
        Graphics2D g2 = (Graphics2D) g;
        Line2D lin = new Line2D.Float(20, 40, 850, 40);
        g2.draw(lin);
    }

    public static void main(String []args){
        Success s=new Success();
        s.setVisible(true);
    }
}

Thanks in advance.

Keep the x co-ordinates the same and change value of y co-ordinates as shown below

Line2D lin = new Line2D.Float(20, 40, 20, 150);

First two values are the (x1,y1) value of the starting point of the line and last two values (x2,y2) end point of the line. Now I hope you understand why your code produced a horizontal line and what needs to be done to draw vertical line.

I noticed a couple things, some of them were already pointed out:

  1. To answer your question directly, this is what the (x, y) coordinates look like for Swing components 这就是Swing组件的(x,y)坐标 keep x coordinates the same for a vertical line. If you don't know where the x coordinates are in your line constructor when you create it, look at the documentation for the constructor. If you're using Eclipse, this means you should just hover your mouse over the code that contains the constructor.
  2. Your line goes outside the range of your JFrame; instead, if you want it to go from end to end, use the getWidth() and getHeight() methods.
  3. You shouldn't be creating a new line every time you repaint your components. Instead, you should create the line somewhere in you Success class, implement ActionListener so you can update your code every frame, and in that update, resize your line, then leave just the repainting to paintComponent .
  4. You shouldn't override JFrame in this case, and you usually shouldn't have to.
  5. You should override the paintComponent method, not the paint method.
  6. I don't think you're double-buffering correctly, but I can't help you there.
  7. Overriding the getPreferredSize method of JPanel is handy if you want to control its size, but it's not even necessary in this case, because adding it to the JFrame will automatically size it for you.

There's a lot of stuff that goes on in Swing behind the scenes, and it can get confusing because normally you have to say stuff explicitly, but keep playing with this example, and you should be safe for a while.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;

class Success extends JPanel implements ActionListener{

    private final Timer timer = new Timer(20, this);  // Create a timer that will go off every 20 ms
    Line2D horizontalLine; // Declare your variables here, but don't initialize them
    Line2D verticalLine;   // That way, they can be accessed later in actionPerformed and repaint

    // You might want to try frame.setResizable(false) if you want your frame
    // and your panel to stay the same size.
    private final Dimension prefPanelSize = new Dimension(450, 450);

    public Success(){
        super();    // Call the constructor of JPanel, the class this subclasses.
        JButton button =new JButton("press");
        this.add(button);
        this.setSize(prefPanelSize);
        horizontalLine = new Line2D.Float(0, 40, prefPanelSize.width, 40);
        verticalLine = new Line2D.Float(prefPanelSize.width / 2, 0,
                prefPanelSize.width / 2, prefPanelSize.height);

        timer.start();  // Start the timer
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // fixes the immediate problem.
        Graphics2D g2 = (Graphics2D) g;

        g2.draw(horizontalLine);
        g2.draw(verticalLine);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return prefPanelSize;
    }

    public static void main(String []args){
        Success s = new Success();
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(new Dimension(450, 450));
        frame.add(s);
    }

    // This method is called ever 20 ms because of the timer.
    @Override
    public void actionPerformed(ActionEvent e) {
        int currWidth = getWidth();
        int currHeight = getHeight();

        horizontalLine.setLine(0, 40, currWidth, 40);
        verticalLine.setLine(currWidth / 2, 0, currWidth / 2, currHeight);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM