所以我想制作一个使用两个JSliders来调整矩形大小的Applet。我希望从底部到顶部的高度与Vertical JSlider的外观相匹配。如何从下到上动态调整矩形大小。 Java
我遇到的唯一问题是,在Java中绘制图形是从x & y,它们都位于0,向左移动x(西),y向南。调整宽度很简单,因为我只是使用一个参数来增加或减少宽度。随着身高,移动它是好的(我已经设定为340,因为这是我想留在的基地)。当我向下移动JSlider时,它向下移动但不会达到它曾经在的高度。我真的很想让这个矩形看起来像JSlider移动的方式一样。
主要Applet的页面(其中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和我会非常感激任何反馈。先谢谢你。
*“这是我learnin的第三个月g Java“*自1999年以来我一直在使用Java,并且仍在学习。 :) – 2012-03-30 16:20:23
'extends Component' ...'public void paint(Graphics g){'*** No!***对于Swing,使用'extends JComponent'&'public void paintComponent(Graphics g){' – 2012-03-30 16:22:16
@Andrew汤普森一切都取决于遗传事件,勤奋和承诺,但13t。年 – mKorbel 2012-03-30 18:22:02