gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:36:07 25 4
gpt4 key购买 nike

我希望调整在“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);
}

您可以设置各种笔划。基本笔划是一条粗实线。

关于java - 如何调整从 g.drawLine() 方法绘制的线条的粗细,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27847099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com