Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Moving a shape across a panel

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Moving a shape across a panel

    Since I am pretty new to Java I have encountered some very trivial problems. I am working harder than I thought I would just to stay up with my class, but beyond that I need some help determining why my program is not working right. I am to create a graphics panel with two JSliders (one vertical, one horizontal) which will move a ball with the thumbnail of the slider. I have created a circle, but when I try to move the circle across the panel, instead of it moving, the size of the circle gets bigger, smaller, bigger, and so on. It appears to resize diagonally across the panel. Please let me know if my listener needs a loop, do I put a loop in the paintComponent method, do I create a new method to move the shape? Here is my code:

    import java.awt.Color;
     
     
    public class Week06 {
    	private JFrame myFrame;
    	private final GPanel gP = new GPanel();
    	private final JSlider vSlider = new JSlider(SwingConstants.VERTICAL, 0, 100, 0);
    	private final JSlider hSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
    	private final JButton btnExit = new JButton("Exit");
    	private int x_pos = 10;
    	private int y_pos = 100;
    	private int radius = 10;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Week06 window = new Week06();
    					window.myFrame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public Week06() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		myFrame = new JFrame();
    		myFrame.setTitle("06");
    		myFrame.setBounds(100, 100, 780, 495);
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myFrame.getContentPane().setLayout(null);
     
    		//JPanel pnlBallMover = new JPanel();
    		gP.setBackground(new Color(0, 206, 209));
    		gP.setBorder(new MatteBorder(5, 5, 5, 5, (Color) new Color(222, 184, 135)));
    		gP.setBounds(10, 11, 714, 374);
    		myFrame.getContentPane().add(gP);
     
    		//JButton btnExit = new JButton("Exit");
    		btnExit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				System.exit(0);
    			}
    		});
    		btnExit.setFont(new Font("Lucida Calligraphy", Font.BOLD, 16));
    		btnExit.setBounds(644, 413, 110, 33);
    		myFrame.getContentPane().add(btnExit);
     
    		hSlider.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent arg0) {
    				gP.repaint();
     
    			}
    		});
     
     
    		hSlider.setBounds(0, 0, 714, 23);
    		hSlider.setMajorTickSpacing(5);
    		hSlider.setPaintTicks(true);
    		myFrame.getContentPane().add(hSlider);
     
    		//JSlider vSlider = new JSlider();
    		vSlider.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent e) {
    				gP.repaint();
     
     
    			}
    		});
    		vSlider.setOrientation(SwingConstants.VERTICAL);
    		vSlider.setBounds(x_pos, y_pos, 30, 374);
    		vSlider.setMajorTickSpacing(5);
    		vSlider.setPaintTicks(true);
    		vSlider.setInverted(true);
    		myFrame.getContentPane().add(vSlider);
    	}
     
    	private class GPanel extends JPanel {
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			Graphics2D g2 = (Graphics2D) g;
     
    			g2.setColor(Color.ORANGE);
    			g2.fillOval(x_pos - radius, y_pos - radius, radius*2, radius*2);
    			gP.setD(radius*2);
     
    			//gP.repaint();
     
    			int fw = myFrame.getWidth();
    			int fh = myFrame.getHeight();
    			if((fw - 66 > 10) && (fh - 121 > 10)) {
    				btnExit.setBounds(fw - 136, fh - 82, 110, 33);
    				hSlider.setBounds(10, fh - 110, fw - 66, 23);
    				vSlider.setBounds(fw - 56, 11, 30, fh - 121);
    				setBounds(10, 11, fw - 66, fh - 121);
    			}
    		}
    		public void setD(int newD) {
    			if(newD >= 0) {
    				radius = newD;
    			} else {
    				radius = 10;
    			}
    		}
    		public Dimension getPreferredSize() {
    			return new Dimension(100, 100);
    		}
    		public Dimension getMinimumSize() {
    			return getPreferredSize();
    		}
    	}
    }
    Last edited by havinFun; May 15th, 2012 at 10:11 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Moving a shape across a panel

    instead of it moving, the size of the circle gets bigger, smaller, bigger,
    If you want the size of the shape not to change, try defining the radius as final and remove all code that tries to change its value . Only change the x and y position values.


    BTW The code you posted does not compile without many errors. Its missing several import statements.
    Last edited by Norm; May 16th, 2012 at 06:18 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Moving a shape across a panel

    Sorry, didn't realize that my imports were missing, here is the complete code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.SwingConstants;
    import javax.swing.border.MatteBorder;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
     
     
    public class Week06 {
    	private JFrame myFrame;
    	private final GPanel gP = new GPanel();
    	private final JSlider vSlider = 
    			new JSlider(SwingConstants.VERTICAL, 0, 100, 0);
    	private final JSlider hSlider = 
    			new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
    	private final JButton btnExit = 
    			new JButton("Exit");
    	private int x_pos = 10;
    	private int y_pos = 100;
    	private int radius = 10;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Week06 window = new Week06();
    					window.myFrame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the application.
    	 */
    	public Week06() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		myFrame = new JFrame();
    		myFrame.setTitle("Week06");
    		myFrame.setBounds(100, 100, 780, 495);
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myFrame.getContentPane().setLayout(null);
     
    		//JPanel pnlBallMover = new JPanel();
    		gP.setBackground(new Color(0, 206, 209));
    		gP.setBorder(new MatteBorder(5, 5, 5, 5, 
    				(Color) new Color(222, 184, 135)));
    		gP.setBounds(10, 11, 714, 374);
    		myFrame.getContentPane().add(gP);
     
    		//JButton btnExit = new JButton("Exit");
    		btnExit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				System.exit(0);
    			}
    		});
    		btnExit.setFont(new Font("Lucida Calligraphy", Font.BOLD, 16));
    		btnExit.setBounds(644, 413, 110, 33);
    		myFrame.getContentPane().add(btnExit);
     
    		hSlider.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent arg0) {
    				gP.repaint();
    			}
    		});
     
    		hSlider.setBounds(0, 0, 714, 23);
    		hSlider.setMajorTickSpacing(5);
    		hSlider.setPaintTicks(true);
    		myFrame.getContentPane().add(hSlider);
     
    		vSlider.addChangeListener(new ChangeListener() {
    			public void stateChanged(ChangeEvent e) {
    				gP.repaint();
    			}
    		});
    		vSlider.setOrientation(SwingConstants.VERTICAL);
    		vSlider.setBounds(x_pos, y_pos, 30, 374);
    		vSlider.setMajorTickSpacing(5);
    		vSlider.setPaintTicks(true);
    		vSlider.setInverted(true);
    		myFrame.getContentPane().add(vSlider);
    	}
     
    	private class GPanel extends JPanel {
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			Graphics2D g2 = (Graphics2D) g;
     
    			g2.setColor(Color.ORANGE);
    			g2.fillOval(x_pos, y_pos, radius*2, radius*2);
    			gP.setD(radius*2);
     
    			int fw = myFrame.getWidth();
    			int fh = myFrame.getHeight();
    			if((fw - 66 > 10) && (fh - 121 > 10)) {
    				btnExit.setBounds(fw - 136, fh - 82, 110, 33);
    				hSlider.setBounds(10, fh - 110, fw - 66, 23);
    				vSlider.setBounds(fw - 56, 11, 30, fh - 121);
    				setBounds(10, 11, fw - 66, fh - 121);
    			}
    		}
    		public void setD(int newD) {
    			if(newD >= 0) {
    				radius = newD;
    			} else {
    				radius = 10;
    			}
    		}
    		public Dimension getPreferredSize() {
    			return new Dimension(100, 100);
    		}
    		public Dimension getMinimumSize() {
    			return getPreferredSize();
    		}
    	}
    }

    When I tried to declare "radius" as a final, I couldn't get to it in the setD method to work, when I try to remove this method then nothing happens when I move the slider. Do I set the sliders to (0, 0, ...,...)? or leave the x_pos and y_pos in there as well as in the set bounds for the oval?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Moving a shape across a panel

    What is the setD method supposed to do? Why does it change the value of radius? Do you know what a circle's radius is?

    If you want the sliders to control the position of the shape, then you need to get the sliders values and use them to set the shape's x,y location values.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Moving a shape across a panel

    the setD is to make sure that the radius is never a negative number and if it is then default to a radius of 10, but I see now that I am hard coding the size of the circle so I should need to have this method. If I need to get the slider value, am I creating a method getValue() that will get the value of the veritcal and horizontal slider and then put that variable in the fillOval()?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Moving a shape across a panel

    make sure that the radius is never a negative number
    Does the size of the drawn shape change?

    Where do you change the value of the x and y position of the shape? Is its position supposed to change when a slider is moved?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Moving a shape across a panel

    I do not want the size to ever change. I am not sure where I want the value to change of the shape, am I going to reference the center of the shape or the x and y position (top left corner of shape)? The position is supposed to move with the slider, if the vertical slider is moved down, the shape should also move down and stop where the thumbnail is stopped, does that make sense?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Moving a shape across a panel

    I do not want the size to ever change.
    Make radius final and you won't be able to change it.
    The position is supposed to move with the slider
    Your code is not using any values from the slider to change the x & y values of the shape's location.
    Look at the JSlider class's API doc for methods to use to get its value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Moving a shape across a panel

    OK, so I looked at the API doc for JSlider, from what I get I am supposed to create a method getValue() create an int that will grab slider.getValue. Am I on the right track? Then I will return that int. I will then need to create a setValue(int) method that will place the slider at a value?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Moving a shape across a panel

    To help you see what the JSlider methods do, add a println statement in the listener method and print out the values that the JSlider class methods return as you move the slider around.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating Callout shape window
    By vrof in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2012, 05:13 AM
  2. Printing a Diamond shape Problem.
    By Sev in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 23rd, 2011, 09:05 AM
  3. Shape error?
    By xTommy24 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 20th, 2011, 08:46 AM
  4. Shape Calculation Program
    By TH1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2011, 03:54 PM
  5. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM