そこで、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か月目であり、フィードバックをいただければ幸いです。前もって感謝します。