This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.
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)