| Author |
How to transfere ActionListener to anothe Class
|
Joseph Yingst
Greenhorn
Joined: Mar 06, 2004
Posts: 1
|
|
Hi Everyone! I have a problem with the folowing code: As the program it is like this, it automatically draws(load) objects while it runs. This is what I do not want, I want it load each object (drawing ) when I clicked Generate PacketButton from second this class. I mean this button should call a method from second class. package rainbowservice; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.borland.jbcl.layout.*; import javax.swing.border.*; public class Area2 extends JFrame { JPanel contentPane; Nodes nodes1 = new Nodes(); Hosts h = new Hosts(); XYLayout xYLayout1 = new XYLayout(); JPanel jPanelControl = new JPanel(); XYLayout xYLayout2 = new XYLayout(); Border border1; TitledBorder titledBorder1; static Button GPackets = new Button(); //Construct the frame public Area2() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); border1 = new EtchedBorder(EtchedBorder.RAISED,Color.red,Color.blue); titledBorder1 = new TitledBorder(border1,"Control_Panel"); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(789, 488)); this.setTitle("Simulation of Resource Resavation from Rainbow Service Architecture "); jPanelControl.setLayout(xYLayout2); jPanelControl.setBorder(titledBorder1); GPackets.setLabel("Generate_Packet"); GPackets.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { GPackets_actionPerformed(e); } }); contentPane.add(nodes1, new XYConstraints(170, 4, 613, 323)); contentPane.add(jPanelControl, new XYConstraints(2, 4, 167, 323)); jPanelControl.add(GPackets, new XYConstraints(4, 6, 110, 24)); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void GPackets_actionPerformed(ActionEvent e) { ??? ??? } } //// The next following class draws rectangles and I want it draw them on request from the action event of: void GPackets_actionPerformed(ActionEvent e) { } of the above program package rainbowservice; import java.awt.*; import java.awt.Graphics2D; import java.awt.*; import java.awt.Point; import java.awt.GradientPaint; import javax.swing.Timer; public class Hosts { static int mx; static int my; private String fhostName, shostName; private int bufferSize; private int numberOfPackets; private int interfaces; static int pWidth, pHeight; static int bWidth, bHeight; public Hosts() { fhostName = "Host 1"; shostName = "Host 2"; bufferSize = 50; numberOfPackets = 5; interfaces = 2; pWidth = 10; pHeight =10; bWidth = 55; bHeight = 20; mx =5; my =10; } public void drawSelfHost1( Graphics2D g,int x, int y) { mx = x; my = y; g.setPaint(Color.blue); g.setStroke(new BasicStroke(3.0f)); g.drawRect(mx,my,80,60); } public void drawSelfHost2( Graphics2D g,int x, int y) { mx = x; my = y; g.setPaint(Color.blue); g.setStroke(new BasicStroke(3.0f)); g.drawRect(mx,my,80,60); } public void drawBuffer(Graphics2D g,int x, int y) { mx = x; my = y; g.setPaint(Color.black); g.setStroke(new BasicStroke(3.0f)); g.drawRect(mx,my,bWidth,bHeight); } public void Packet(Graphics2D g,int x, int y) { mx = x; my = y; g.setPaint(Color.red); g.drawRect(mx,my, pWidth, pHeight); } } This third class is responsible for setting the location of object to be drawn package rainbowservice; import javax.swing.JPanel; import javax.swing.JPanel; import java.awt.*; import javax.swing.Timer; import java.lang.Thread; import java.util.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Nodes extends JPanel implements ActionListener { private Timer tm; private Hosts hs; static int x, y,x1,y1; public Nodes( ) { hs = new Hosts(); x = 5; y = 7; this.setBackground(Color.yellow); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 =(Graphics2D)g; drawAllStations(g2); } //DRAW ALL HOSTS public void drawAllStations(Graphics2D g) { //Host One on the left hs.drawSelfHost1(g,80,60); //Draw Buffer for Host 1 hs.drawBuffer(g,x+90,y+70); // Host One on the Right hs.Packet(g,x+127,y+77); //Host Two on the Right hs.drawSelfHost2(g,520,60); } public void actionPerformed(ActionEvent e) { GCR(); } public void GCR(){ if(x == 198 ) { tm.stop(); } else repaint(); } }
|
 |
 |
|
|
subject: How to transfere ActionListener to anothe Class
|
|
|