SunDont Shine
SunDont Shine

Reputation: 9

How can you adjust the thickness of a line drawn from g.drawLine() method

I am looking to adjust the thickness of the line drawn at line 75 under the "MyPanel" Class you will find in the comments. My overall intent of this program is to draw letters on the screen when buttons are clicked. Thanks

public class Painttest extends JFrame {
private Timer timer;
private JPanel panel1, panel2;
private  MyPanel center;
private MyButton stop, A, B;

public Painttest() {


    this.setSize(650, 700);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);

    panel1 = new JPanel();
    panel2 = new JPanel();

    center = new MyPanel();
    center.setBackground(Color.cyan);


    stop = new MyButton("Stop");
        stop.setName("stop button");
    A = new MyButton("A");
        A.setName("A Button");
    B = new MyButton("B");
        B.setName("B Button");

    panel1.add(A);
    panel1.add(B);

    panel2.add(stop);

    this.add(panel1, BorderLayout.WEST);
    this.add(center, BorderLayout.CENTER);
    this.add(panel2, BorderLayout.EAST);

    timer = new Timer(50, center);

    this.setVisible(true);

    System.out.println(center.getSize()); // size of center panel

}

public static void main(String[] args) {
    new Painttest();
}


///////////////////////////////////////////////////////////////////////
// MyPanel class  : This panel will be used to draw on
////////////////////////////////////////////////////////////////////////
private class MyPanel extends JPanel implements ActionListener {
    private int startingx, startingy;
    private int endingx, endingy;


    MyPanel() {
        startingx =  110;
        startingy = 330;
        endingx = startingx ;
        endingy = startingy ;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
       // g.fillOval(startingx, startingy, 30, 30);
        g.drawLine(startingx, startingy, endingx,endingy);  // draws a single line
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        startingx = startingx + 1;
        startingy = startingy - 3;

        repaint();

    }

}

///////////////////////////////////////////////////////////////////////
// MyButton class  : When Clicked, I want it to draw something on the MyPanel (center)
////////////////////////////////////////////////////////////////////////
private class MyButton extends JButton implements ActionListener {
    String name;

    MyButton(String title) {
        super(title);
        addActionListener(this);
        name = "NOT SET";
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Pressed a button " + name);
        ////////////////////////////////////
        // if Statements to control the timer
        ////////////////////////////////////

        // STOP TIMER
        if (e.getSource() == stop) {
            timer.stop();
        }
        if(e.getSource() == A){
            timer.start();
        }
        if(e.getSource() == B){
            timer.start();
        }

    }

    public void setName(String newname) {
        name = newname;
    }

    public String getName() {
        return name;
    }
}

}

Upvotes: 1

Views: 2914

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51505

Modify your paintComponent method something like this:

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setStroke(new BasicStroke(6.0F));
       // g.fillOval(startingx, startingy, 30, 30);
        g2d.drawLine(startingx, startingy, endingx, endingy);
    }

You can set various kinds of strokes. A basic stroke is a solid thick line.

Upvotes: 2

Related Questions