• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating new rectangles at runtime

 
Jon Mattson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic