1

そこで、2つのJSliderを使用して長方形のサイズを変更するアプレットを作成しようとしています。縦型JSliderの外観に合わせて、下から上に向かって高さを調整したいと思います。

私が遭遇している唯一の問題は、Javaで描画するために、描画が両方とも0にあるxとyからのものであり、xを左(西)に移動し、yが南に向かっていることです。幅を増減するために単一のパラメーターを持つメソッドを使用しているだけなので、幅のサイズ変更は簡単です。高さがあるので、上に動かすのは問題ありません(これは、私が滞在したいベースであるため、yを340に設定しています)。JSliderを下に動かすと、下に移動しますが、以前と同じ高さになりません。JSliderと同じように長方形が動いているように見せたいです。

メインアプレットページ(JSlidersが配置されている場所):

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class ResizeRectangle extends JApplet {
    private JSlider sliderHeight;
    private JSlider sliderWidth;
    private JLabel title;
    private Contents content;
    private int oldValue = 0;
    public void init(){

        setLayout(new BorderLayout());

        title = new JLabel("Rectangle Resizer");
        content = new Contents();

        content.setBackground(Color.WHITE);

        //Slider for increasing the height
        sliderHeight = new JSlider(SwingConstants.VERTICAL, 10, 100, 10);
        sliderHeight.setMajorTickSpacing(10);
        sliderHeight.setPaintTicks(true);

        sliderHeight.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        //If sliderHeight is greater than oldValue pass true for increase
                        if(sliderHeight.getValue() > oldValue){
                            content.setHeight(sliderHeight.getValue(), true);
                        }
                        //Else if sliderHeight is less than oldValue pass false for increase
                        else if(sliderHeight.getValue() < oldValue){
                            content.setHeight(sliderHeight.getValue(), false);
                        }
                        //Sets the value of sliderHeight to the value of oldValue to compare later
                        oldValue = sliderHeight.getValue();
                    }
                }
        );
        //Slider for increasing the widht
        sliderWidth = new JSlider(SwingConstants.HORIZONTAL, 10, 100, 10);
        sliderWidth.setMajorTickSpacing(10);
        sliderWidth.setPaintTicks(true);

        sliderWidth.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        content.setWidth(sliderWidth.getValue());
                    }
                }
        );

        content.setBackground(Color.WHITE);
        add(title, BorderLayout.NORTH);
        add(content, BorderLayout.CENTER);
        add(sliderWidth, BorderLayout.SOUTH);
        add(sliderHeight, BorderLayout.EAST);

    }
}

これは、高さ(および幅)を設定するためのコードです。

import java.awt.Component;
import java.awt.Graphics;



public class Contents extends Component {

    private int height = 10;
    private int width = 10;
    //Initialize y to 340.
    private int y = 340;

    //Draw the rectangle
    public void paint(Graphics g){
        g.fillRect(5, y, width, height);
    }

    public void setHeight(int h, boolean increase){
        //If the slider is increasing, decrease y and increase height to give it the
        // look of rising 
        if(increase){
            y = y - h;
            height = height + h;
        }
        //else do the opposite
        else{
            y = y + h;
            height = height - h;
        }
        //For debugging purposes
        System.out.println("h = "+h);
        System.out.println("y = "+y);
        System.out.println("height = "+height+"\n\n");
        //Repaint the rectangle
        repaint();

    }
    //Set the width and repaint
    public void setWidth(int w){
        width = w;
        repaint();
    }
}

これはJavaを学ぶ3か月目であり、フィードバックをいただければ幸いです。前もって感謝します。

4

1 に答える 1

1

コードに論理的な問題があります。変更リスナー内の高さを変更するために使用される呼び出しは

content.setHeight(sliderHeight.getValue(), ...);

一見合理的に見えます。ただし、このメソッドsetHeightは、名前が示すように(つまり、高さを設定する)実行せず、代わりに、最初の引数として指定された量だけ高さを増減します。

if(increase){
    y = y - h;
    height = height + h;
} else {
    y = y + h;
    height = height - h;
}

したがって、スライダーが10から20に変わり、10に戻ると、次の値の変化があります。

// initial values
y = 340
height = 10     

// change from 10 to 20, thus h=20 and increase as arguments for the method
y -> 340 - 20 = 320
height -> 10 + 20 = 30


// change from 20 back to 10, i.e. h=10 and decrease
y -> 320 + 10 = 330
height -> 30 - 10 = 20

代わりにメソッドが行うべきことは、実際に高さを設定することです。

y = 340 - h;
height = h;
于 2012-03-30T17:13:21.523 に答える