java - Constructing Rectangle - Stack Overflow
java - Constructing Rectangle - Stack Overflow
Asked 9 years, 2 months ago Modified 7 years, 7 months ago Viewed 45k times
I am constructing a Rectangle class. I tried many times and watched numerous tutorials, but
my program is not working. This is my code so far:
5
public class Rectangle {
java
Share Improve this question edited Oct 2, 2015 at 18:01 asked Sep 14, 2015 at 17:41
Follow Peter Mortensen S.Dovra
31.6k 22 109 133 89 1 2 6
1 Please clarify your specific problem or add additional details to highlight exactly what you need. As
it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help
clarifying this question. – Suresh Atta Sep 14, 2015 at 17:43
You're trying all sorts of things without understanding what the code you're writing is intended to do.
I'll break down what you have, then point out what is needed and what you need to do, in an answer.
– Shotgun Ninja Sep 14, 2015 at 17:51
Assumptions
114 I'm assuming you're unfamiliar with the syntax of the Java language, and trying to piece
together your first Java class.
1. A definition of a Rectangle .
3. The properties width and height , indicating the Rectangle 's dimensions.
define your Rectangle class. This serves as a unit of code, and a template for making objects.
Classes have defined methods (behavior) and fields/properties (information), which serve to
indicate how objects of that class can operate and describe what data is accessible on each
object.
In short, this defines a general type of thing that can be created; in your case, you're defining
what it means for an object to be a Rectangle in your code.
defines a static method called main . There's a lot of stuff here to explain, but for now, just
think of this as an entry point into a Java program. If you were to run this class as a Java
program, this method would be (almost) the first code which is run.
is what you would do to declare a Rectangle named box , then create a new Rectangle object
and assign the object to the variable named box . In other words, you're making a new
Rectangle whose name is box .
The keyword new means that you're making a new object of type Rectangle . The numbers in
the parentheses are arguments, which define some things about the object being created.
These get passed into a constructor method of the Rectangle class, which puts those
arguments into the newly-created object.
Right now, though, there is no constructor, so your code won't work right in that regard. I'll
write one up further down, so keep reading.
Right now, you're passing it a new Rectangle object, which doesn't really make sense. A
Rectangle isn't text, nor is it a basic variable (like an integer, a byte, a decimal number, a
single character of text, or a true/false value), so if you try to print that new Rectangle object
to the screen, you'll see some gobbledy-gook text provided by Java that looks like this:
Rectangle@1a2fc866
However, we have a nifty little feature that lets us tell Java how to convert objects of a specific
class into a string of text; I'll show you that below.
There! We've defined a Rectangle class, for making Rectangle objects. Now, it needs some
properties. We'll say these are integer numbers, called x , y , width , and height . Our class
now looks like this:
Okay, cool. Now, how do we make a Rectangle ? Use a constructor method! If we didn't need
information about the Rectangle when we make a new one, we can leave this out and use a
default constructor which Java provides for us, but we do need the information about the
rectangle's position, width, and height. Our code grows to this:
Now we have the ability to make a Rectangle object and specify its position, width, and
height. But we have no program, since there is no entry point. Let's add one like you did
originally, but fix some of the issues your original code had:
// This is our program's entry point. It's static, and belongs to the
Rectangle class, and NOT to the Rectangle objects.
public static void main(String[] args) {
// Inside our main method, let's make a new rectangle object.
// Based on our constructor, the position is {5, 10}, the width is 20, and
the height is 30.
Rectangle box = new Rectangle ( 5 , 10 , 20 , 30 );
// Now that it's created and named "box", let's print it out!
// We pass the box variable as an argument to System.out.println,
// and that method prints the box as if it were a string.
System.out.println(box);
}
}
We now have an entry point to our program, that does a little bit of stuff. We create a
Rectangle, which we assign to the Rectangle variable called box . Then, we try to print box as
a string to the console window. As mentioned earlier, if we run the program right now, we will
end up with console output like this:
Rectangle@1a2fc866
This isn't what we want; we want to see what the Rectangle's position, height and width are!
So, let's change how Java turns our Rectangle objects into strings of text, by adding a
toString() method. Our Rectangle class now looks like this:
// We can "add" strings together to form a bigger string with the contents
mashed next to each other (aka "concatenated").
stringValue = stringValue + this .x + "," ;
// Remember, we don't need to use "this" when the name is not ambiguous,
but it typically makes it clearer that some data is coming from the object instead
of a local variable.
stringValue += ", height: " + height;
Now, when we run our program, it will print out the following:
We can clean up the toString method a lot here, so our class looks like this:
@Override
public String toString() {
return "Rectangle with location {" + x + "," + y + "}, width: " + width +
", height: " + height;
}
}
Going Further
Now, what we need to do is to get our Rectangle to show up on the screen. In order to do this,
we're going to need to bring in the AWT framework (Advanced Window Toolkit) and the
Swing framework. This isn't the only way to get it to show up on screen, but it works for our
purposes.
We need to add some imports to the top of the file, and start modifying our main method so
that it sets up AWT and Swing and uses them to draw our rectangle. Add this to the top of the
file:
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
These bring in the classes you'll need to create a program window and draw your rectangle on
it. I won't get into how AWT and Swing work in detail, but I'll show you what you need.
In order to get your Rectangle to appear on-screen, we need to create a window, set its size,
and fill the window's content pane with a component which will draw the rectangle onto the
screen. Afterwards, we just make our window visible, and Swing handles all the dirty work,
keeping our program alive until the window is closed.
// Tell Swing to exit the program when the program window is closed.
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get the content pane of the window, and give it our own custom component.
window.getContentPane().add( new JComponent () { // Not a typo - this is some
advanced magic called an "anonymous class".
Rectangle myBox = box; // Give the component a reference to our box
object.
public void paint(Graphics g) {
g.drawRect(myBox.x, myBox.y, myBox.width, myBox.height);
}
});
This is a little sloppy and hard to follow, so you'll have to excuse my hand-waving. I feel I've
explained some of the tricky bits in the comments (like the anonymous class - that's a pretty
advanced Java feature), but in general, this is the type of code that the Swing documentation
shows, and much of it doesn't need to be understood completely to be used. If you want, you
can do your own research on the Swing framework, the AWT Graphics class, or Java
Anonymous classes through the examples at the links I've provided.
Share Improve this answer Follow edited May 23, 2017 at 12:34 answered Sep 14, 2015 at 18:57
Community Bot Shotgun Ninja
1 1 2,540 3 26 32
There's a few things that needs to be pointed out. First off, your code won't compile. Try this.
16 import java.awt.Rectangle;
You need a ; semicolon at the end of the Rectangle line instead of a bracket { . Also, it will
be a good idea to rename your class to something different than Rectangle to prevent some
compatibility issues for when you want to call on the Rectangle class. Also, when you are
printing out the rectangle, you want to use the box reference instead of constructing a new
Rectangle.
2nd, this won't draw a rectangle on your screen or in a window, this will only print out
Rectangles toString method. Which according to the javadoc will only print out a String
representing this Rectangle object's coordinate and size values. .
If you want to actually draw a rectangle, you need to look into something like a JFrame or
looking into paint , with something similar to this
http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/DrawRectangle.htm
Share Improve this answer Follow edited Sep 17, 2015 at 11:53 answered Sep 14, 2015 at 17:49
Andro Austin
2,232 1 30 42 4,909 6 35 54
OP is writing their main method in a class named Rectangle ... Unless he's written his own
toString method (or is explicitly using java.awt.Rectangle), it's just going to use the default
Object version. – Mage Xy Sep 14, 2015 at 17:52
import java.awt.Rectangle; public class Rectangle { public static void main(String[] args) { Rectangle
box= new Rectangle(5,10,20,30); System.out.println(box); } } – S.Dovra Sep 14, 2015 at 18:19
import java.awt.Rectangle; public class Rectangle { – S.Dovra Sep 14, 2015 at 18:29
1 @S.Dovra Post your whole file if you could please. You shouldn't give up yet, programming is not
easy and everyone started somewhere like you right now. – Austin Sep 14, 2015 at 18:44
import java.awt.Graphics;
import javax.swing.JComponent;
0 import javax.swing.JFrame;
Share Improve this answer Follow edited Oct 2, 2015 at 17:58 answered Sep 14, 2015 at 17:57
Peter Mortensen Kanad Jadhav
31.6k 22 109 133 1 1
1 You should add some comment to show explain how the code works – Luc Sep 14, 2015 at 18:10
Thanks bro and at the same time sorry for mistyping my problem. I need not draw actual rectangle.
– S.Dovra Sep 14, 2015 at 18:24