0

下面是我目前用于绘图类的代码,我一直在寻找教我如何在屏幕上创建 3 个形状的文档。

我有一个 jframe,带有用于选择形状(见下文)等的菜单。

if (clickedMenu.getText().equals("Square")){                    
value = pane.returnslider();
shape = new ASquare(value);

所以我的问题是:我如何编辑下面的类来创建一个出现在我的 Jframe 上的 2D 正方形,它根据滑块值改变大小?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment;

import java.awt.Graphics;

/**
 *
 * @author Steven
 */
public class MyDrawing extends javax.swing.JPanel {

/**
 * Creates new form MyDrawing
 */
 @Override public void paintComponent(Graphics g) {
     super.paintComponent(g);    // paints background


  }   
}

我的 jpanel 中的一些代码可能会帮助您回答我的问题:

public class MyChangeAction implements ChangeListener {

    //complete code here
    @Override
    public void stateChanged(ChangeEvent ce) {
        double value = slider.getValue();
        String str = Double.toString(value);
        sliderLabel.setText(str);
        DecimalFormat df = new DecimalFormat("0.0");

        boundary_length.setText("" +     df.format(MyFrame.shape.computeBoundaryLength(value)));
        area.setText("" + df.format(MyFrame.shape.computeArea(value)));
    }
} // end class

public double returnslider() {
    return slider.getValue();
}

我的广场课:

package assignment;

import java.awt.event.ActionListener;

/**
 *
 * @author b00560806
 */
public class ASquare extends MyShape {

double value;

public ASquare(double value) {
    this.value = value;
}


@Override
public double computeBoundaryLength(double Length) 
{
    thelength=(4*Length);
    return thelength;
}

@Override
public double computeArea(double Length) 
{
    thearea=(Length*Length);
    return thearea;
}

}
4

1 回答 1

1

尝试这个:

@Override 
public void paintComponent(Graphics g) {
    super.paintComponent(g);    // paints background

    Graphics2D g2 = (Graphics2D)g;
    g2.setStroke(new Stroke((int)returnslider));
    // rest of drawing.
}  
于 2012-11-02T19:06:09.930 回答