Aarti
Posts: 6
Nickname: aarti
Registered: Apr, 2002
|
|
Re: events in java
|
Posted: Apr 18, 2002 11:47 AM
|
|
thank u for ur replies.... this is the code....i get the GUI to display but the events are not called..keep in mind that it is part of a big application........ package webprinter.collect.dbcollect;
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*;
import webprinter.collect.DocumentBoundaryCollector;
class GenerateTree extends Component implements MouseListener,ActionListener { private int pageCount; private Vector co_Ordinates = new Vector(); private ArrayList pageList = new ArrayList(); private ArrayList pages = new ArrayList(); private Hashtable coodTable = new Hashtable(); private String label = "1"; private DBCollectorImpl collector; private Graphics g;
GenerateTree(int count,DBCollectorImpl collector) { //adds listeners to the ovals this.addMouseListener(this); this.collector = collector;
//assigns count to page count to be used later for the number of ovals pageCount = count; Frame frame = new Frame("Tree Diagram of webpages") ; frame.add(this); //to close the window frame.addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent we) { System.exit(1); } } );
Button okButton = new Button("OK"); okButton.setActionCommand("OK"); okButton.addActionListener(this); Button cancelButton = new Button("Cancel"); cancelButton.setActionCommand("Cancel"); cancelButton.addActionListener(this); Panel buttonPanel = new Panel(); FlowLayout flow = new FlowLayout(FlowLayout.CENTER); buttonPanel.setLayout(flow); flow.setHgap(25); buttonPanel.add(okButton); buttonPanel.add(cancelButton);
frame.add(buttonPanel,"South"); frame.setSize( 400, 600 ); System.out.println("end of contrcutor"); //update(getGraphics()); frame.setVisible(true) ; paint(); }
public void paint() { g = getGraphics(); int centerX = 200, centerY = 80, i; int height = 80, width = 100, x = 190, x1 = 150; System.out.println("in paint loop"); for( i = 1;i<=pageCount;i++) { g.setColor(Color.black); g.drawOval(centerX, centerY, 40, 40); g.setColor(Color.gray); g.fillOval(centerX, centerY, 40, 40); //add the co-ordinates with in a vector for comparison later co_Ordinates.add(new OvalCord(centerX, centerY)); g.setColor(getForeground()); label = label.valueOf(i); coodTable.put(new Integer(centerY),new Integer(label)); g.drawString(label.valueOf(i) , centerX+17, centerY+22 ); if(i<pageCount) { g.drawLine (centerX + 20,centerY+40,centerX+20,centerY+80); g.drawLine (centerX + 20,centerY+80,centerX+30,centerY+70); g.drawLine (centerX + 20,centerY+80,centerX+10,centerY+70); //for drawings arcs on both sides if( i % 2 ==0 ) { g.drawArc(x, 100, width , height, -90, 180 ); } else { g.drawArc(x1, 100, width , height, 90, 180 ); } }
height = height + 80; width = width + 60; centerY = centerY + 80; x = x - 30; x1 = x1 - 30; } System.out.println("end of paint loop"); } public Dimension getPreferredSize() { Font f = getFont(); if( f != null ) { FontMetrics fm = getFontMetrics( f ); int max = Math.max( fm.stringWidth(label)+10, fm.getHeight()+10 ); return new Dimension( max, max ); } else { return new Dimension( 100, 100 ); } }
//mouse events public void mouseClicked(MouseEvent me) { int x = me.getX(); int y = me.getY(); boolean doBreak = false; for(int i=0; i<co_Ordinates.size(); i++) { OvalCord OC = (OvalCord) co_Ordinates.get(i); if ( x<OC.x+40 && x>OC.x) { if (y<OC.y+40 && y>OC.y) { Graphics g = getGraphics(); g.setColor( Color.black); g.fillOval(OC.x,OC.y,40,40); //get the number of the webpage that is clicked Integer webpagesLabel = getLabels(new Integer(OC.y)); //add it to a vector pages.add(webpagesLabel); } } } } public void mouseEntered(MouseEvent me) { //System.out.println("SUCESS2"); }
public void mouseExited(MouseEvent me) { //System.out.println("SUCESS3"); }
public void mousePressed(MouseEvent me) { //System.out.println("SUCESS4"); }
public void mouseReleased(MouseEvent me) { //System.out.println("SUCESS5"); }
//action events for the 2 buttons public void actionPerformed(ActionEvent ae) { String actionCommand = ae.getActionCommand(); if(actionCommand.compareTo( "Cancel" ) == 0 ) { repaint(); } if(actionCommand.compareTo( "OK" ) == 0 ) { pageList = pages; synchronized(collector){ collector.notify();} } }
//returns the label associated with the co-ordinates. public Integer getLabels( Integer coods ) { Integer number = (Integer)coodTable.get(coods); return number; }
public synchronized ArrayList getPagesList() { try { wait(); } catch(InterruptedException ie) { ie.printStackTrace(); } return pageList; } }
class OvalCord { public int x, y;
public OvalCord(int x, int y) { this.x = x; this.y = y; }
public String toString() { return "centerX = "+x+" centerY = "+y+ " | "; } };
|
|