Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何调整从g.drawLine()方法绘制的线的厚度_Java_Graphics - Fatal编程技术网

Java 如何调整从g.drawLine()方法绘制的线的厚度

Java 如何调整从g.drawLine()方法绘制的线的厚度,java,graphics,Java,Graphics,我希望调整在“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.set

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

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

g.setStroke(新的基本行程(6f))将其作为答案并加以解释。就是这样。@WhyCry我很惊讶这不是一个复制品。如果事情紧急,我只是想帮忙。吉尔伯特的解决方案很好,只是投了更高的票。是的,我只是没有把它标记为xD