Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I am creating a GraphGUI. I have attached 2 panels a GraphPanel(for graph sheet) and a COntrolPanel(for button controls). I placed two buttons a 'sine' and 'cosine' which should display it's corresponding wave on GraphPanel upon clicking them. Right now I am doing event handling for my GUI and I am having problem there.
Here is the code:
1) Main Class:
Java
 public class GraphGUI extends JFrame {
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame f = new MainFrame("GraphGUI");
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.setVisible(true);
				f.setSize(900, 725);
				f.setResizable(false);
			}
		});
	}
} 

2)Frame Class:
Java
 public class MainFrame extends JFrame {
	private GraphPanel gPanel;
	private ControlPanel pPanel;	
	public MainFrame(String title) {
		super(title);		
		pPanel = new ControlPanel();
		gPanel = new GraphPanel();		
		Container con = getContentPane();		
		con.add(pPanel, BorderLayout.EAST);
		con.add(gPanel, BorderLayout.WEST);		
		pPanel.addGraphListener(new GraphListener() {
			@Override
			public void graphEventOccured(GraphEvent event) {
				System.out.println("hi");				
			}			
		}, gPanel);
	}
} 

3) GraphPanel Class:
Java
public class GraphPanel extends JPanel {
	public GraphPanel() {
		super();
		setBorder(BorderFactory.createLineBorder(Color.black));		
		Dimension size = getPreferredSize();
		size.width = 666;
		setPreferredSize(size);
		setBackground(Color.black);
	}	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		final int h = getHeight();
		final int w = getWidth();
		System.out.println("h = " + h + "\tw = " + w);
		g2.setColor(Color.RED);
		g2.drawLine(300, 0, 300, h);
		g2.drawLine(0, 340, w, 340);
		g2.setColor(Color.GREEN);
		int i = 1;
		int z  = 1;
		while(z > 0) {
			z = 300 - (i*20);
			g2.drawLine(z, 0, z, h);
			i++;
		}
		i = 1;
		while(z < w) {
			z = 300 + (i*20);
			g2.drawLine(z, 0, z, h);
			i++;
		}
		i = 1;
		while(z > 0) {
			z = 340 - (i*20);
			g2.drawLine(0, z, w, z);
			i++;
		}
		i = 1;
		while(z < h) {
			z = 340 + (i*20);
			g2.drawLine(0, z, w, z);
			i++;
		}
	}
}

4) ControlPanel Class:
Java
 public class ControlPanel extends JPanel {
	private JButton sineBtn;
	private JButton cosineBtn;	
	private EventListenerList listenerList = new EventListenerList();
	public ControlPanel() {
		super();
		setBorder(BorderFactory.createLineBorder(Color.green));
		setLayout(new GridBagLayout());
		Dimension size = getPreferredSize();
		size.width = 300;
		setPreferredSize(size);
		setLayout(new GridBagLayout());
		
		sineBtn = new JButton("Sine");
		cosineBtn = new JButton("Cosine");
		
		//action listener for sineBtn
		sineBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				fireGraphEvent(new GraphEvent(this));//exception here when Sine Button is clicked
			}			
		});
		//action listener for cosineBtn
		cosineBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				fireGraphEvent(new GraphEvent(this));//exception here when Cosine Button is clicked				
			}
		});		
		setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();		
		//adding buttons
		gc.anchor = GridBagConstraints.CENTER;
		gc.weightx = 0.5;
		gc.weighty = 0.5;
		//changing the default size of the buttons
		gc.ipadx = 40;
		gc.ipady = 40;
		//adding sineBtn
		gc.gridx = 0;
		gc.gridy = 0;
		add(sineBtn, gc);
		//adding cosineBtn
		gc.gridx = 0;
		gc.gridy = 1;
		add(cosineBtn, gc);
	}	
	public void fireGraphEvent(GraphEvent event) {
		Object[] listeners = listenerList.getListenerList();
		for(int i = 0; i < listeners.length; i++) {
			if(listeners[i] == GraphListener.class) {
				((GraphListener)listeners[i]).graphEventOccured(event);//exception here for both events
			}
		}
	}
	public void addGraphListener(GraphListener listener, GraphPanel gPanel) {
		listenerList.add(GraphListener.class, listener);
	}	
	public void removeGraphListener(GraphListener listener) {
		listenerList.remove(GraphListener.class, listener);
	}
} 

5)GraphEvent Class:
Java
public class GraphEvent extends EventObject {
    public GraphEvent(Object source) {
        super(source);
        System.out.println("Source : " + source);
    }
}

6) GraphListener Interface:
Java
public interface GraphListener extends EventListener {
    public void graphEventOccured(GraphEvent event);
}


Right now I am not at the stage of plotting waves, I am just checking whether my events are occurring as desired and expected. Exceptions are occurring in ControlPanel class as mentioned in the code. Please show me where am I erring and point me in the right direction for plotting sine waves and cosine waves in my GUI.

Thank you for your valuable time.
Posted
Updated 29-Aug-12 1:11am
v3
Comments
Mehdi Gholam 29-Aug-12 2:51am    
What exceptions are you getting?
stib_markc 29-Aug-12 2:56am    
This is what the log shows:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Class cannot be cast to GraphListener
at ControlPanel.fireGraphEvent(ControlPanel.java:74)
at ControlPanel$2.actionPerformed(ControlPanel.java:46)
Richard MacCutchan 29-Aug-12 4:09am    
In the statement ((GraphListener)listeners[i]).graphEventOccured(event);, your cast is not valid.
stib_markc 29-Aug-12 4:15am    
Yes, the problem is I wrote the code similarly in another application and it worked, but I can't understand why this is not valid here. Of-course in that application no graph was involved, but here also before involving my GraphPanel, the exceptions arouse.
Richard MacCutchan 29-Aug-12 4:46am    
If you are using casts in your code without understanding them then you are on the road to some major problems in your development. Take a look at the section on it in this tutorial.

1 solution

The comparison in the if statement is wrong, you want to know if the item in the listener array can be cast to a GraphListener. This works:

Java
if (GraphListener.class.isAssignableFrom(listeners[i].getClass())) {
 
Share this answer
 
Comments
stib_markc 29-Aug-12 6:18am    
Thank you, it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900