/* * MyCanvas.java * * Created on 2002Äê11ÔÂ10ÈÕ, ÏÂÎç6:45 */ //package jaxbdemo; import java.awt.*; import java.awt.event.*; //from jaxb import java.math.*; import java.util.*; import javax.xml.bind.*; import epri.jaxb.*; import java.io.*; /** * * @author user1 */ public class MyCanvas extends Canvas implements MouseListener{ private String currentColor = "red"; private String currentgraph= "circle"; private int currentX; private int currentY; private ObjectFactory objectFactory; private ShapeContainer myContainer; public void setObjectFactory(ObjectFactory myFactory){ this.objectFactory = myFactory; } public void setShapeContainer(ShapeContainer container){ this.myContainer = container; } public MyCanvas() { this.addMouseListener(this); setForeground(Color.red); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { currentX = e.getX(); currentY = e.getY(); if (this.myContainer == null) { try { this.objectFactory = new ObjectFactory(); this.myContainer = objectFactory.createShapeContainer(); } catch( JAXBException je ) { je.printStackTrace(); } } java.util.List shapeList = myContainer.getShape(); try { ShapeType newType = this.objectFactory.createShapeType(); newType.setShapecolor(this.currentColor); newType.setShapename(this.currentgraph); newType.setXposition(this.currentX); newType.setYposition(this.currentY); shapeList.add(newType); } catch( JAXBException je ) { je.printStackTrace(); } repaint(); } public void mouseReleased(MouseEvent e) { } public void setColor(String color) { currentColor = color; } public void setGraph(String graph) { currentgraph = graph; } public void paint(Graphics g) { if (myContainer !=null){ java.util.List shapeList = myContainer.getShape(); //this.setIgnoreRepaint(true); Color currentcolor = this.getForeground(); for( Iterator iter = shapeList.iterator(); iter.hasNext(); ) { ShapeType myshape = (ShapeType)iter.next(); String shapename = myshape.getShapename(); String shapeColor = myshape.getShapecolor(); int xposition = myshape.getXposition(); int yposition = myshape.getYposition(); if (shapeColor.equals("red")) g.setColor(Color.red); if (shapeColor.equals("green")) g.setColor(Color.green); if(shapename.equals("circle")) g.drawOval(xposition-25,yposition-25,50,50); if(shapename.equals("rect")) g.drawRect(xposition-25,yposition-25,50,50); this.setForeground(currentcolor); } } } }