8 Replies - 1212 Views - Last Post: 10 May 2009 - 11:20 PM Rate Topic: -----

#1 kevgliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 09

Program Dying in Event Dispatch Thread

Posted 09 May 2009 - 10:31 AM

Hello everyone, I am trying to finish up our last project of the year, but I am getting it is crashing without console output and I don't know why. Are there any glaring errors? It fails after I call the restore() method. It goes through my code fine but fails somewhere in what eclipse says is the Event Dispatch Thread.

This is the class that is having the problem but I will also add the other classes, I know this is a lot of code to go through but the eclipse debugger is giving me nothing to go on. Thanks for any guidance!


/**
 * @author Kevin Glisson
 * 
 * 
 */



import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.*;


public class GUI extends JPanel {
	
	private JTextField removeManyField;
	private JPanel buttonPanel;
	
	public static final int WIDTH = 800;
	public static final int HEIGHT = 800;
	
	
	private final int dotSIZE = 10; //radius of each dot
	public LucasList<MyPoint> MyPointList = new LucasList<MyPoint>();
	public TreeMap<Double, MyPoint> MyPointMap = new TreeMap<Double, MyPoint>();
	public Stack<MyPoint> RemovedStack = new Stack<MyPoint>();
	
	
	private final int[]initial_xCoord = {150,168,225,378,278,250,145,30};
	private final int[]initial_yCoord = {50, 142,183,225,250,410,332,464};


	 /**
	  * GUI creates the GUI, it creates a drawing area and several Buttons 
	  * to act on that GUI
	  */
	 public GUI()
	 {
		
		loadInitialValues();
		
		setBackground (Color.black);
		
		setPreferredSize (new Dimension(800, 600));
		
		setLayout(new BorderLayout());
		
		//ButtonPanel
		
		JPanel buttonPanel = new JPanel();
		
		buttonPanel.setLayout(new FlowLayout());
		
		buttonPanel.setBackground(Color.BLACK);
		
		addMouseListener (new DotsListener());
	   
		//Remove Button

		JButton removeButton = new JButton("Remove");

		removeButton.setBackground(Color.BLACK);
		
		removeButton.addActionListener( new ButtonListener() );
		  
		buttonPanel.add(removeButton);


		//Restore Button

		JButton restoreButton = new JButton("Restore");

		restoreButton.setBackground(Color.BLACK);

		restoreButton.addActionListener( new ButtonListener() );
		  
		buttonPanel.add(restoreButton);

		  
		//Restore all
		
		JButton restoreAllButton = new JButton("Restore All");

		restoreAllButton.setBackground(Color.BLACK);

		restoreAllButton.addActionListener( new ButtonListener() );

		buttonPanel.add(restoreAllButton);

		//Remove Many Button
		
		JButton removeManyButton = new JButton("Remove Many");
		
		removeManyButton.setBackground(Color.BLACK);
		
		removeManyButton.addActionListener( new ButtonListener() );
		
		buttonPanel.add(removeManyButton);
		
		//RemoveMany Field


		removeManyField = new JTextField(25);
		
		removeManyField.setText("Enter how many points you wish to remove");
		
		buttonPanel.add(removeManyField);
		
		this.add(buttonPanel, BorderLayout.SOUTH);
	   
	 }

	

	/**
	 * calculateImportance takes a point and determines how important it is
	 * based on its left and right point
	 * @param next is the point that needs its importance calculated
	 * @return a importance value
	 */
	private Double calculateImportance(MyPoint next) {
		MyPoint right = MyPointList.getLeft(next.getListPosition());
		
		MyPoint left = MyPointList.getRight(next.getListPosition());
		
		Double importance =  distance(left , next) + 
										distance(next, right) - distance(left, right);
		
		return importance;
	}
	
	/**
	 * distance takes two points and calculates the distance between them using 
	 * a^2 = b^2 + c^2 formula.
	 * @param pointA
	 * @param pointB
	 * @return the distance between the two points
	 */
	private Double distance(MyPoint pointA, MyPoint pointB){
		if(pointA != null && pointB != null){
			MyPoint temp = new MyPoint(pointA.getXCoord(), pointB.getYCoord(), 0.0);
		
			double side1 = pointA.getYCoord() - temp.getYCoord();
		
			double side2 = pointB.getXCoord() - temp.getXCoord();
		
			Double distanceOfThirdSide = Math.sqrt(Math.pow(side2, 2) + Math.pow(side1, 2));
		
			return distanceOfThirdSide;
		}else
			return 0.0;
	}


	/**
	 * loadInitialValues takes the points in the two initial arrays and then
	 * calculates various data for each of the new MyPoint objects including:
	 * its importance value, its position in the linked list. It also adds 
	 * the newly created object to a custom LinkedList called LucasList, and 
	 * adds it to a Map of Points
	 * 
	 */
	private void loadInitialValues() {
		
		for(int i = 0; i < initial_xCoord.length; i++){
			
			MyPoint newPoint = new MyPoint(initial_xCoord[i], initial_yCoord[i], 0.0);
			
			LucasList<MyPoint>.ListPosition pointPosition = MyPointList.add(newPoint);
			
			newPoint.setListPosition(pointPosition);
			
			MyPoint prevPosition = MyPointList.getLeft(pointPosition);
			
			if(prevPosition != null)
			
				newPoint.setPredecessor(prevPosition.getListPosition());
			
			else
			;
				
			
		   }
		
		Iterator <MyPoint> getPoint = MyPointList.iterator();
		
		while(getPoint.hasNext()){
			
			MyPoint newPoint = getPoint.next();
			
			Double importance = calculateImportance(newPoint);
			
			newPoint.setImportance(importance);
			
			MyPointMap.put(importance, newPoint); 
		}
		
		
	}
	
	/**
	 * ButtonListener is an inner class that deal with the button
	 * clicked even, it then handles which method should be called 
	 * depending on which button was clicked.
	 *
	 */

	public class ButtonListener implements ActionListener {

		 

			public void actionPerformed(ActionEvent e)

			{

				String buttonString = e.getActionCommand( );

				if (buttonString.equals("Remove")) remove();

				else if (buttonString.equals("Restore All")) restoreAll();
				
				else if(buttonString.equals("Restore")) restore();
				
				else if(buttonString.equals("Remove Many")) removeMany(Integer.parseInt(removeManyField.getText()));
				

			}
	 }

	 /**
	  * restore is to pop the stack in order to obtain the last removed 
	  * point. It then adds that point back into both MyPointList and MyPointMap.
	  * 		
	  */
	 private void restore() {
		 	if(!RemovedStack.isEmpty()){
		 		MyPoint addDot = RemovedStack.pop();
			
		 		MyPointMap.put(addDot.getImportance(), addDot);
			
		 		MyPointList.spliceIn(addDot, addDot.getPredecessor());
			
		 		repaint();
		 	}else
		 		JOptionPane.showMessageDialog(null, "There is nothing to restore!");
			
	 }
	 
	 /**
	  * restoreAll does the same thing as restore except that it does it until
	  * the remove stack is empty
	  */
	 private void restoreAll() {
		
			if(!RemovedStack.isEmpty()){
				while(RemovedStack.size() != 0){
					MyPoint addDot = RemovedStack.pop();
				
					MyPointMap.put(addDot.getImportance(), addDot);
				
					MyPointList.spliceIn(addDot, addDot.getPredecessor());
					}
			
			repaint();
			}else
				JOptionPane.showMessageDialog(null, "There is nothing to restore!");
	 }
	 
	 /**
	  * remove takes the least important point and removes it from
	  * both the linked list and the Map.
	  * It will only remove a point when there are more than 3 points
	  * left in the list.
	  */
	 private void remove() {
			if(MyPointList.size() > 3){
				Double removedKey = MyPointMap.firstKey(); 
			
				MyPoint removeDot = MyPointMap.get(removedKey);
			
				RemovedStack.push(removeDot);
			
				MyPointList.spliceOut(removeDot.getListPosition());
			
				MyPointMap.remove(removedKey);
			
				repaint();
			}else{
				JOptionPane.showMessageDialog(null, "You cannot remove any more points there are only three left!");
			}
			
	 }
	 
	 /**
	  * removeMany takes in the number of points you wish to remove and the 
	  * @param x number of points to be removed.
	  */
	 private void removeMany(int x){
		 int i = 0;
		 if(RemovedStack.size() - x > 3){
			 while(i < x){
				remove();
				i++;
			 }
		 
			 repaint();
		 }else
			 JOptionPane.showMessageDialog(null, "You are removing to many points! You must always have" +
			 		" atleast 3 points");
		 
	 }

		  

	/**
	 * DotsListener listens for any mouse click on the frame, when there
	 * are some it then takes the x and y coordinates of that click and creates
	 * a new point, it then adds that new point into the linkedList and the Map, 
	 * while calculating it importance. 
	 *
	 */
	private class DotsListener implements MouseListener

	   {



		  public void mousePressed (MouseEvent event)

		  {
			  
			 double x = event.getPoint().getX();
			 
			 double y =  event.getPoint().getY();
			 
			 MyPoint newPoint = new MyPoint(x,y, 0.0);
			 
			 LucasList<MyPoint>.ListPosition pointPosition =  MyPointList.add(newPoint);
			 
			 newPoint.setListPosition(pointPosition);
				
			 Double importance = (Double) calculateImportance(newPoint);
			 
			 newPoint.setImportance(importance);
			 
			 MyPointMap.put(importance , (MyPoint) newPoint);
			 
			 repaint();

		  }





		public void mouseClicked (MouseEvent event) {}

		  public void mouseReleased (MouseEvent event) {}

		  public void mouseEntered (MouseEvent event) {}

		  public void mouseExited (MouseEvent event) {}






	   }
	
	 /**
	  * paintComponent draws every point on the screen and connecting 
	  * each of them with a line in order to create the picture.
	  */
	 public void paintComponent (Graphics page)
	 {
		  
		  super.paintComponent(page);
		  
		  MyPoint prevPoint = null;
		  
		  int x1 = 0;
		  int y1 = 0;
		  
		  page.setColor (Color.green);   

		  Iterator <MyPoint> draw =  MyPointList.iterator();
		  
		  while(draw.hasNext()){
			 MyPoint point = draw.next();
			 
			 int x = (int) point.getXCoord();
			 
			 int y = (int) point.getYCoord();
 			 
			 page.fillOval (x - dotSIZE, y - dotSIZE, dotSIZE*2, dotSIZE*2);
			 

			 if(prevPoint == null){

				 MyPoint previousPoint = MyPointList.getLeft(point.getPredecessor());
				  
				 x1 = (int) previousPoint.getXCoord();
				 
				 y1 = (int) previousPoint.getYCoord();
				 
			 }else{
			 	 x1 = (int) prevPoint.getXCoord();
			 		
			 	 y1 = (int) prevPoint.getYCoord(); 
			 	}
			 
			 
			 page.drawLine(x, y, x1, y1);
			 
			 prevPoint = point;
			 
			 }
	   }
}






import javax.swing.JFrame;


public class StartGUI {

	public static void main (String [] args){
		JFrame frame = new JFrame ("StartGUI");
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setSize(400,400);
		
		frame.getContentPane().add(new GUI());
		
		frame.pack();
		frame.setVisible(true);
	}
}



import java.util.*;



public class LucasList < E >  implements Iterable {



  /** A reference to the header of the list. */

  private Node < E > header;



  /** The number of items in the list. */

  private int size;

  

  public LucasList ( )

  {

	  header = new Node ( null );

	  header.next = header;

	  header.prev = header;

	  size = 0;

  }

	

  public Iterator iterator()  {  

	  return  ( new LucasListIterator( ) );  

  }

	 

  public class LucasListIterator implements Iterator  {

  

	 private boolean hasNext;

	 

	 // predecessor of the next item to be returned by this Iterator

	 private Node<E> predOfNext;	 

	  

	 /**  Constructor if the iterator */

	 public LucasListIterator ( ) {

		 predOfNext = header;

		 if ( predOfNext.next == header )

		   hasNext = false;

		 else

		   hasNext = true;

	 }

  

	 public boolean hasNext ( ) {

		 return hasNext;

	 }

  

	 /** return the "next" data in the list  */

	 public E next() {

		 if ( ! hasNext )

		   throw ( new NoSuchElementException() );

		 predOfNext = predOfNext.next;

		 if ( predOfNext.next == header )

		   hasNext = false;

		 return ( predOfNext.data );

	 }

	 

	 public void remove ( ) {

		 throw ( new UnsupportedOperationException() );

	 }

  }   

  



  /** Appends the specified element to the end of this list.

	* @param  item   element to be appended to this list. 

	* @return position of this item in the list

	*/

  public ListPosition add( E item ) {

	

	  Node<E>  newItem = new Node<E> ( item );

	  

	  newItem.next = header;

	  newItem.prev = header.prev;

	  

	  header.prev.next = newItem;

	  header.prev = newItem;

	  

	  size++; 

	  

	  return ( new ListPosition ( newItem ) );

  }

  

  

   /** Remove from the List the item at the given location

	  @param killLocation - position of the item to be deleted

	  @return The last element in the list.

   */

  public void spliceOut( ListPosition killLocation ) {  
	  
	  
	  killLocation.finger.next.prev = killLocation.finger.prev;
	  
	  killLocation.finger.prev.next = killLocation.finger.next;
	  
	  
	  size--;
	  

  }

  

  

   /** Insert into the List the given item after the given location

	   @param item - data to be added to the list

	   @param predPosition - position of the item which will become the predecessor of this item

	   @return The ListLocation where this item was inserted

   */

  public ListPosition spliceIn( E item, ListPosition predPosition ) {
	  Node<E>  addedItem = new Node<E> ( item );
	  
	  addedItem.next = predPosition.finger.next;
	  
	  addedItem.prev = predPosition.finger; 
	  
	  predPosition.finger.next.prev = addedItem;
	  
	  predPosition.finger.next = addedItem.prev;
	  
	  size++;
	  return ( null );

  }  

  
  public MyPoint getRight(ListPosition currentPosition){
	  MyPoint rightNeighbor;
	  if(currentPosition.finger.next.data == null){
		  rightNeighbor = (MyPoint) header.next.data;
	  }else{
		  rightNeighbor = (MyPoint) currentPosition.finger.next.data;
	  }
	  return rightNeighbor;
  }
  
  public MyPoint getLeft(ListPosition currentPosition){
	  MyPoint leftNeighbor;
	  if(currentPosition.finger.prev.data == null){
		  leftNeighbor = (MyPoint) header.prev.data;
	  }else{
		  leftNeighbor = (MyPoint) currentPosition.finger.prev.data;
	  }
	  return leftNeighbor;
  }
  

  public boolean isEmpty(  ) {

	  if ( size == 0 )

		return true;

	  else

		return false;

  }

  

  public int size(  ) {

	  return size;

  }  






  // Inner Classes

  /** A Node is the building block for a double-linked list. */

  private static class Node < E > {

	  

	/** The data value. */

	private E data;



	/** The link to the next node. */

	private Node < E > next = null;



	/** The link to the previous node. */

	private Node < E > prev = null;



	/** Construct a node with the given data value.

		@param dataItem The data value

	 */

	private Node(E dataItem) {

	  data = dataItem;

	  

	}

  }

  

  

  public class ListPosition {

	  

	/** MyPointer to a node in the list (not the header node) */

	private Node < E > finger;



	public ListPosition ( Node<E> ref ) { finger = ref; }

	

	public Node<E> getPosition()  { return finger; }

  } 

}







public class MyPoint {
	public double xCoord;
	public double yCoord;
	public double importance;
	public LucasList<MyPoint>.ListPosition predecessor;
	public LucasList<MyPoint>.ListPosition listPosition;
	
	
	public MyPoint(double x, double y, double importance){
		this.importance = importance;
		this.xCoord = x;
		this.yCoord = y;
		
	}
	
	public Double getImportance(){
		return importance;
	}
	
	public void setImportance(double a){
		importance = a;
	}
	
	public LucasList<MyPoint>.ListPosition getListPosition(){
		return listPosition;
	}
	
	public void setListPosition(LucasList<MyPoint>.ListPosition position){
		listPosition = position;
	}
	
	
	public double getXCoord(){
		return xCoord;
	}
	
	public double getYCoord(){
		return yCoord;
	}
	
	public void setPredecessor(LucasList<MyPoint>.ListPosition prev){
		predecessor = prev;
	}
	
	public LucasList<MyPoint>.ListPosition getPredecessor(){
		return predecessor;
	}
	
   
	
   

}



Is This A Good Question/Topic? 0
  • +

Replies To: Program Dying in Event Dispatch Thread

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Program Dying in Event Dispatch Thread

Posted 09 May 2009 - 01:41 PM

Try calling validate() before repaint
Was This Post Helpful? 0
  • +
  • -

#3 kevgliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 09

Re: Program Dying in Event Dispatch Thread

Posted 09 May 2009 - 04:16 PM

Tried... it still freezing in the same spot, thanks though, any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Program Dying in Event Dispatch Thread

Posted 09 May 2009 - 08:54 PM

What do you call a "crash out"
Do you have any error message ?
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Program Dying in Event Dispatch Thread

Posted 10 May 2009 - 12:48 AM

Quote

Tried... it still freezing in the same spot, thanks though, any other suggestions?


Works for me, doing remove()
Was This Post Helpful? 0
  • +
  • -

#6 kevgliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 09

Re: Program Dying in Event Dispatch Thread

Posted 10 May 2009 - 05:53 AM

It just freezes up and needs to be forced quit. I know the remove function works, its the restore that has the issues, I will probably take it to my professor to she if she can help. Thanks guys
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Program Dying in Event Dispatch Thread

Posted 10 May 2009 - 07:22 AM

You have something causing infinite recursion in paintComponent but i'm not sure what it is
Was This Post Helpful? 1
  • +
  • -

#8 kevgliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 09

Re: Program Dying in Event Dispatch Thread

Posted 10 May 2009 - 05:28 PM

hmmm thats weird... well thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Program Dying in Event Dispatch Thread

Posted 10 May 2009 - 11:20 PM

No problem. What you need to look for is something you're doing in paintComponent that causes a repaint of the component
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1