如何调整从 g.drawLine() 方法绘制的线条的粗细



我希望调整您在评论中找到的"MyPanel"类下第 75 行绘制的线条的粗细。我对这个程序的总体意图是在单击按钮时在屏幕上绘制字母。谢谢

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;
    }
}

}

修改你的paintComponent方法,如下所示:

    @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);
    }

您可以设置各种笔触。 基本描边是实心粗线。

相关内容

  • 没有找到相关文章

最新更新