0

I have this exercise in my textbook:

In this exercise, you will explore a simple way of visualizing a Rectangle object. The setBounds method of the JFrame class moves a frame window to a given rectangle. Complete the following program to visually show the translate method of the Rectangle class:

import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TranslateDemo
{
public static void main(String[] args)
{
// Construct a frame and show it
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Your work goes here: Construct a rectangle and set the frame bounds
JOptionPane.showMessageDialog(frame, "Click OK to continue");
// Your work goes here: Move the rectangle and set the frame bounds again
}
}****

I tried this but it's not working:

**import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TranslateDemo
{
    public static void main(String[] args)
    {
        // Construct a frame and show it
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        // Your work goes here: Construct a rectangle and set the frame bounds

        Rectangle box = new Rectangle(10, 20, 30, 40);
        System.out.println("");
        System.out.println(box);
        System.out.println("");

        JFrame f=new JFrame();
        f.setBounds(50, 50, 200 ,200);


        JOptionPane.showMessageDialog(frame, "Click OK to continue");
        // Your work goes here: Move the rectangle and set the frame bounds again
        box.translate(0,30);
        System.out.println(box);
        JOptionPane.showMessageDialog(frame, "Click OK to continue");
    }
}**

Can you please help me ?

  • Don't use `setBounds()` instead of that use layout managers. we have so many layouts. – SatyaTNV Feb 20 '16 at 04:45
  • @Satya The OP is trying to position and size the `JFrame`, is a valid use of `setBounds` – MadProgrammer Feb 20 '16 at 04:48
  • I dont think she used it on purpose... I think its the question itself – Vaibhav G Feb 20 '16 at 04:48
  • No its not a valid use. `setSize()` and `setLocation()`. @MadProgrammer – SatyaTNV Feb 20 '16 at 04:48
  • 3
    @Satya A `JFrame` is not under the control of layout manager (it's contents should be) and if you dig through the code, `setSize` and `setLocation` both call `setBounds`, so, it's a valid use in this context (although I'd prefer `setSize` and `pack`, but given the context, `setBounds` is okay) – MadProgrammer Feb 20 '16 at 04:51
  • If somebody is working through an exercise to learn about `X`, it is less than zero help to say "don't use `X` all the cool kids use `Y`" – John Hascall Feb 20 '16 at 14:31
  • @JohnHascall Unless `X` would lead you to shooting yourself (or other people) in the foot, then `Y` (like don't point the gun at your feet) would be helpful ;) – MadProgrammer Feb 21 '16 at 06:22

1 Answers1

2

Let's start with the fact that you've created two instance of JFrame...

// First instance
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Your work goes here: Construct a rectangle and set the frame bounds

// Second instance
JFrame f=new JFrame();
f.setBounds(50, 50, 200 ,200);

The first is what's visible on the screen and the second is not.

Maybe something like...

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Your work goes here: Construct a rectangle and set the frame bounds

Rectangle box = new Rectangle(10, 20, 30, 40);
f.setBounds(box);

will work better.

Any changes you make to box won't change the frame, as JFrame uses the properties of Rectangle to set its properties and doesn't maintain a reference to the original Rectangle, instead, you will need call setBounds again in order to update frame

As a general rule though, you shouldn't be setting the size the frame itself, but instead, rely on pack, but since this is an exercise, I can overlook it ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366