Good afternoon,
Guys I'm trying to finish a project that simulates a queue through graphical interface. I have a project of someone who did AVL tree that way. At first, I thought it would be easy to make the adjustment because as an AVL tree had all ready (most difficult), this would make a queue pretty easy. Well, I was wrong. Is ready my linked queue, it works perfectly, furthermore, most of the Applet is already mounted with the events of the operating knobs and so forth. But the applet's drawing methods are impossible to understand. I'm several weeks trying to figure out what this guy did, but no progress yet. Soon there's no way I build my queue, if not before understand how the methods of the original painting. Someone experienced can teach me the business rules involved in these methods?
public void paint(Graphics g) { try { for (int i = 1; i < showTree.length; i++) g.drawLine(showTree[i][2], showTree[i][1]+50, showTree[showTree[i][4]][2], showTree[showTree[i][4]][1]+50); for (int i = 0; i < showTree.length; i++) { int margin=-13; if(showTree[i][0]<-99) margin = -13; else if(showTree[i][0]<-9) margin = -10; else if(showTree[i][0]<0) margin = -5; else if(showTree[i][0]<10) margin = -2; else if(showTree[i][0]<100) margin = -7; else if(showTree[i][0]<1000) margin = -10; if((char)showTree[i][3] == 'L') g.setColor(Color.RED); else if((char)showTree[i][3] == 'R') g.setColor(Color.GREEN); else g.setColor(Color.YELLOW); g.fillOval(showTree[i][2]-12, showTree[i][1]+38,25,25); g.setColor(Color.BLACK); g.drawString(Integer.toString(showTree[i][0]), showTree[i][2]+margin, showTree[i][1]+54); } }catch(NullPointerException e) { } }public synchronized void pause(String process) { showTree = tree.toCoordinates(25, getWidth(),true); showStatus(process); update(getGraphics()); try { wait(delay); } catch (InterruptedException e) { } }Of course it is not 100% object-oriented and organized as it should, for example, the method public int[][] toCoordinates (int h, int w, boolean fixH) it uses an array of 5 columns of information for guard positions X and Y that the lines should be drawn. Well if I understand the business rules, the logic of functioning of this tree, I can certainly mount a queue, which I believe is much easier to do. Attached is the Eclipse project these methods, you can see that the original project it works perfectly even when you increase the screen size gets better visualization of the tree. In the package modificado is my attempt to make such a row, yet fail because of this graphical interface..public int[][] toCoordinates(int h, int w, boolean fixH) { int c[][] = new int[size][5]; int i[] = { 0 }; int hStep = (height > 1) ? h / (height - 1) : 0; hStep = fixH ? h : hStep; traverseToCoordinates(root, i, c, 0, w / 2, hStep, w / 4, -1); return c; } private void traverseToCoordinates( TreeEntry e, int i[], int c[][], int y, int x, int h, int w, int p) { int curI = i[0]; if (e == null) return; if (e.element instanceof ElementObject) { c[i[0]][0] = ((ElementObject) (e.element)).val; // integer value c[i[0]][1] = y; // vertical position c[i[0]][2] = x; // horizontal position c[i[0]][3] = (int) e.balanceFactor; // balance factor c[i[0]][4] = p; // index of parent in c[][] i[0]++; } traverseToCoordinates(e.left, i, c, y + h, x - w, h, w / 2, curI); traverseToCoordinates(e.right, i, c, y + h, x + w, h, w / 2, curI); }
Thanks for all the explanations.