Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:59:27+00:00 2026-06-01T20:59:27+00:00

So I am trying to make a Applet that uses two JSliders to resize

  • 0

So I am trying to make a Applet that uses two JSliders to resize a rectangle. I would like to have the height to go from bottom to top to match with what a Vertical JSlider looks like.

The only problem I am running into is that to draw in Java the drawing is from x & y which are both located at 0, moving x left(West), and y is going south. Resizing the width is simple since I am just using a method with a single parameter to increase or decrease width. With height, moving it up is fine (I have y set at 340 since that is the base I’d like to stay at). When I move the JSlider down, it moves down but will not reach the same height it was once at. I would really like to make is seem like the rectangle is moving the same way the JSlider is moving.

Main Applet Page (Where the JSliders are located):

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);

    }
}

This is the code for setting the height (as well as width):

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();
    }
}

This is my third month of learning Java and I would greatly appreciate any feedback. Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:59:30+00:00Added an answer on June 1, 2026 at 8:59 pm

    You have a logic issue with your code. The call used to change the height inside the change listener is

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

    which looks reasonable on first sight. However the method setHeight does not do what the name suggest (i.e. setting the hight) but instead increases or decreases the height by the amount given as first argument:

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

    Thus if the slider changes from 10 to 20 and back to 10 you have the following change of values:

    // 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
    

    What the method should do instead is indeed set the height, i.e.

    y = 340 - h;
    height = h;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make modal frame in Java Applet such like shown here: http://www.java2s.com/Tutorial/Java/0240__Swing/Showthegivenframeasmodaltothespecifiedowner.htm
I'm trying to make photo album like photo.app in iPhone. I know We have
I'm trying to write an applet that would sign e-mail with S/MIME. Obviously I
I'm trying to make an iOS 5 app that features real-time things coming from
I am trying to make an app that uses a radial dial. As an
I'm trying to make an applet which I can simply drag an image. And
Trying to make a custom :confirm message for a rails form that returns data
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make simple minesweeper game in python, but have one problem. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.