John Meehan

Greenhorn
+ Follow
since Oct 28, 2011
John likes ...
Eclipse IDE Firefox Browser Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
3
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Meehan

Winston Gutkowski wrote:
Fine, but I doubt you'll have much joy basing it on LinkedList because it hides Nodes from you.



That was part of my frustration. I did actually find a way to do it with linked lists and doing element manipulation using LinkedList.set, but I had to initialize the array with this block:


Furthermore, that class's implementation may be overkill since it is doubly-linked, whereas an LL-ring-based queue only really needs a forward pointer (oddly enough, I've been working on that very thing myself recently ).



If you're using a singly-linked list, how do you add a to the end of the queue without traversing the entire list? Does your sentinel point to both ends simultaneously? Doesn't exactly make it a real singly-linked list then.

Well, that link appears to have an explanation of how to do that too. One nice thing about an array is that it's simple and fast - and furthermore, if it's size is a power of 2, you can use bit masking to get your "circular index", which is about as fast as it gets in Java. On the other hand, it's more prone to concurrency bottlenecks.



The only issue there is that there were two problems, one explicitly asking us to use arrays and one linked lists. So I had to do it with linked lists, and I had to do it without removing nodes, which is why the entire program was awkward.

Like I said I did eventually get it to work, but I'd never use this for anything reasonable ever.
11 years ago
Sorry for the confusion, I should have said it's a circular queue implemented with linked lists.

A Circular queue is more easily explained with an array. With a regular queue in an array, if you remove an element from the start you have to shift all the remaining elements one to the left (decrement the indices). With a circular queue, you have two pointers, one for the head and one for the tail. Instead of the data moving, you move the pointers to the new start or new end when you add or remove an element from the queue. It's called "circular" because the pointers are able to wrap around the ends of the array when you add or remove data. It lets you use an array for a queue with constant-time dequeue methods instead of n-time, since you only have to shift one pointer instead of all the elements in the array.

There's a decent wiki explanation with pictures: http://en.wikipedia.org/wiki/Circular_queue#How_it_works

With linked lists, it would make sense to have a queue where the front and the end points to the same sentinel node, so queuing and de-queuing is just a matter of putting new nodes on one side of the queue and removing them from the other side. But for some strange reason my professor wants something that looks like the array version instead. And my code just looks like moose droppings.
11 years ago
Thanks for that.

An additional question though... Is it just me or is this an awful way to use a linked list? Circular queues make sense for arrays, but for linked lists it just seems silly.
11 years ago
I figured that's what I'd have to do. Is it possible with the LinkedList API though? Methods like LinkedList.add and LinkedList.remove add and remove the node as well as the data.
11 years ago
I've got a programming assignment where I have to create a circular queue linked list and I'd like to use the LinkedList api. The problem is my professor says "Note that dequeue something just means remove the data not the node." Yet everything I've seen with the LinkedList api has nodes being added or removed in tandem with the data. If I had my way I'd just make a counter with the max size of the queue and not add more data when you get past that, but I'm certain that's not in the spirit of the assignment.

Is there a way to keep x number of nodes in the queue and just manipulate the elements on those nodes using the LinkedList api, or will I have to write my own?

Thanks for the help.
11 years ago
Ok, nevermind. I figured it out. I guess I'll leave this here with the answer in case someone happens along.

I had to change PowerPaintPanel.getHeight() to this.getHeight().

I feel silly now.
12 years ago
So I'm trying to make a mini-Paint style program, and one of the components is creating a grid across the JPanel that lies on top of all the actual drawings, which is turned on and off by a JCheckBoxMenuItem. So I'm trying to use this code:



PowerPaintPanel is the name of the panel, and GRID_SPACING is the space between each line in the grid.

When I try to use this code, the PowerPaintPanel.getWidth() and PowerPaintPanel.getHeight() calls give me an error "Cannot make a static reference to the non-static method getHeight() from the type JComponent" (and one for getWidth()). When I substitute the hard-coded default height and width for the frame, it works beautifully, except it doesn't expand if I resize the JPanel (which is what I need it to do).

I'm at a loss of how to fix this problem. Anyone have any ideas?

This is the entire code for the Panel thus far:

12 years ago
I did mean an enum, sorry about that.

Jesper de Jong wrote:What do you mean by "asserting an enumeration"?

You can create a list that holds enum instances just like you would create a list that holds any other kind of objects:



So in this case, the enums would be the 2d boolean arrays, then I'd create a list for each object and add the appropriate enums? Would I be able to call this from another class like normal?

I'd like to look at the BitSet later, but for now I've already started coding the rest using 2d boolean arrays.
12 years ago
Hi guys, I'm working on a new project and I'm having trouble getting started. I'm trying to represent 2d objects with a 2d array of booleans (showing true if there's a pixel/block there, false if not). These objects need to be rotated by the user in 90 degree increments, so I figured I'd represent the object itself with an ArrayList of these 2d booleans, then every time the object was rotated I'd just focus on the next or previous element in the array, depending on whether the object was rotated left or right. Finally I'm trying to stick all of these in an enumeration, since I've got a limited number of separate objects that need to be represented, and this way I can (hopefully) create instances of each object in a single class instead of one for each type of object.

But, I'm running into a wall. Normally you'd have to create the ArrayList, then do Object.add() for each 2D array element of the array list, but as this is an enumeration, I don't really see how to create the ArrayLists for the enumerations. Do I assert the enumerations first, then do the ArrayList? Do I have to create the ArrayLists as I assert the enumerations? The method I'm using makes sense to me, I'm just not sure how to put it in code.

Thanks for any help.
12 years ago
Actually, I think part of my problem is I'm going too hog wild on the helper classes. I'll try to make things a little more contained, see if that helps.

Edit: nevermind, I've got it now. Instead of adding to panel in SnapShopPanel I should have just added. Oh well, at least things will go quicker now.
12 years ago
Ah, I new it was something simple I was missing. How would I go about doing that? Do I just add another pack(); at the end of SnapShopPanel, then call add(SnapShopPanel.panel); instead of add(panel);?
12 years ago
Hi everyone, I'm having some issues with a programming assignment. I'm trying to implement a GUI for some backend code our professor gave us, but I can't seem to get anything to show. Basically the GUI is supposed to have some image manipulation buttons (implemented by the backend) up top, open/save buttons at the bottom, and an image in the center using ImageIcon, which only appears after an image has been opened. I'm trying to make a big JPanel with a Border Layout, and smaller JPanels with FlowLayout to contain the buttons and image icon. When I run the code the JPanel opens, but there's nothing inside. I'm thinking the smaller panels are implementing, but not getting placed into the larger one. Any way you guys could help would be appreciated.



I'm not trying to implement the backend yet, just get the GUI to look correct. The ImageIcon for the center hasn't been implemented yet either, I'm working on that now.
12 years ago