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:
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:
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:
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:
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");
sineBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
fireGraphEvent(new GraphEvent(this));
}
});
cosineBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
fireGraphEvent(new GraphEvent(this));
}
});
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.anchor = GridBagConstraints.CENTER;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.ipadx = 40;
gc.ipady = 40;
gc.gridx = 0;
gc.gridy = 0;
add(sineBtn, gc);
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);
}
}
}
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:
public class GraphEvent extends EventObject {
public GraphEvent(Object source) {
super(source);
System.out.println("Source : " + source);
}
}
6) GraphListener Interface:
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.