Problem With Assignment, Need Tips

Hi, i'll try and keep the code to a minimum. My assignment (at UNI) is to create a JUG Program (Java Ultimate Graphist) which works like a paint program.

So far ive done most of it. But im having problems with combo boxes. Basically its required to have a combo box at the bottom where you choose your shape (rectangle, line, ellipse etc etc). When you choose your shape, and click on the screen, it draws that shape.

This is the bit thats troubling me. The way ive programmed it, (we're using frames) I have one file "Application.java" which runs everything. Another file "Settings.java" which holds most of my methods as well as the control panel (for combo boxes) at the bottom, and a menu bar at the top.

The final file is the actual drawing panel - "PaintPanel.java". This has the paint method etc in it.

Now the problem im having is getting the combo box (Prefrences.java) to interact with the "PaintPanel.java". If i could put all this in one file it would probably work but i just get errors and it just wont work.

Does anyone know how i can make an action listener, listen for my choice in the combo box, then change the shape drawn in the paint panel.

I can post the entire code if you want it

Thanks!

[1262 byte] By [0rtona] at [2007-10-2 5:28:05]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ClassCommunication

{

DrawingCode drawCode;

EventCode eventCode;

public ClassCommunication()

{

// pass a reference of/for/to the DrawingCode instance

// to EventCode that EventCode can use to call methods

// in the DrawingCode class

drawCode = new DrawingCode();

eventCode = new EventCode(drawCode);

}

private JPanel getCenterPanel()

{

return drawCode;

}

private JPanel getSouthPanel()

{

return eventCode.getUIPanel();

}

public static void main(String[] args)

{

ClassCommunication app = new ClassCommunication();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(app.getCenterPanel());

f.getContentPane().add(app.getSouthPanel(), "South");

f.setSize(300,175);

f.setLocation(200,200);

f.setVisible(true);

}

}

class DrawingCode extends JPanel

{

String s;

public DrawingCode()

{

s = "hello world";

}

public void setString(String s)

{

this.s = s;

repaint();

}

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setFont(g.getFont().deriveFont(18f));

g.drawString(s, 100, 50);

}

}

class EventCode implements ActionListener

{

DrawingCode drawCode;

public EventCode(DrawingCode dc)

{

drawCode = dc;

}

public void actionPerformed(ActionEvent e)

{

JComboBox cb = (JComboBox)e.getSource();

String s = (String)cb.getSelectedItem();

drawCode.setString(s);

}

public JPanel getUIPanel()

{

String[] items = { "rectangle", "ellipse", "line" };

JComboBox combo = new JComboBox(items);

combo.addActionListener(this);

JPanel panel = new JPanel();

panel.add(combo);

return panel;

}

}

74philipa at 2007-7-16 1:29:45 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
hi! thanks, its a lot clearer now. it makes sense now how i can make both panels interact with eachother.thanks
0rtona at 2007-7-16 1:29:45 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...