2012-03-30 44 views
1

所以我想制作一个使用两个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和我会非常感激任何反馈。先谢谢你。

+0

*“这是我learnin的第三个月g Java“*自1999年以来我一直在使用Java,并且仍在学习。 :) – 2012-03-30 16:20:23

+0

'extends Component' ...'public void paint(Graphics g){'*** No!***对于Swing,使用'extends JComponent'&'public void paintComponent(Graphics g){' – 2012-03-30 16:22:16

+0

@Andrew汤普森一切都取决于遗传事件,勤奋和承诺,但13t。年 – mKorbel 2012-03-30 18:22:02

回答

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; 
+0

非常感谢。我对如何在逻辑中表达我想要的东西感到非常困惑,但这对我来说有很大的帮助。我已经更新了代码,以便不再在content.setHeight()中包含布尔值,并且仅将h用作参数。你的代码示例只需点击一下即可。 – 2012-03-30 18:56:57