This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Creating new rectangles at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating new rectangles at runtime" Watch "Creating new rectangles at runtime" New topic
Author

Creating new rectangles at runtime

Jon Mattson
Greenhorn

Joined: Feb 07, 2006
Posts: 19
I want to make a program in which you can create rectangles and specify their width and length and then move them around with the mouse. How should I do this? How can I make new objects at runtime? I have heard people talking about linked lists, what are they and how do I use them?

Thanks in advance
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Jon Mattson:
I want to make a program in which you can create rectangles and specify their width and length and then move them around with the mouse. How should I do this? How can I make new objects at runtime? I have heard people talking about linked lists, what are they and how do I use them?

Thanks in advance


A linked list is an abstract data structure where you store data in object called nodes and have in the class definition for a node, a reference to an object of the same type.

There is a class in the API that implements LinkedList that can give you more information.

There are several collections in the API that let you store data and can grow and shrink dynamically.

Take a look at the classes in the event APIs, java.awt.event and javax.swing.event, and may see some classes such as MouseMotionListener that you can use to do what you want.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
actualy LinkedList is neither abstract nor an interface!

LinkedList is one of the implementations of the List interface (ArrayList and Vector are some of the other implementations)
java.util.List

but back to the rectangle problem:

you need to:
- create a Object (or reuse the Rectangle class of java) that represents a rectangle
- keep a list of rectangles that you need to draw
- everytime you need to draw your rectangles, you would loop over this list and draw each rectangle
- then, as Keith sugested, look at some GUI concepts such as MouseEvents, MouseListener, MouseMotionListener which will help you to capture mouseclicks, mouse dragging and so on.
- find a way how user can interact with your GUI so he can specify the dimensions of the rectangle (e.g. first click set x1/y1, second click sets x2/y2) and when the rectangle is defined then you create a rectangle object and add it to the List of rectangles.
- find a way how user can select/drag rectangles (e.g. user clicks, you loop the list of existing rectangles to find the rectangle he clicked, then draw it in a different color and allow dragging.... this is a little bit more complex since there could be several rectangles at the same position and so on)


pascal
[ May 01, 2006: Message edited by: pascal betz ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by pascal betz:
actualy LinkedList is neither abstract nor an interface!


I wasn't referring in that part of the post to the class LinkedList. I was referring to the concept of a linked list.
 
 
subject: Creating new rectangles at runtime
 
Similar Threads
GUI
paint JTextPane in java.awt.Rectangle
painting hundreds of Rectangles in JPanel
How paint non-rectangular java.awt.geom.Area or break Area into Rectangles?