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

时间:2015-01-08 18:30:12

标签: 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.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;
    }
}

}

1 个答案:

答案 0 :(得分:2)

修改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);
    }

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