import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import java.io.*; import java.awt.*; import java.awt.event.*; import org.w3c.dom.*; public class XMLJoonis2 extends Frame implements ActionListener, MouseListener, MouseMotionListener{ Button nupp = new Button("Lae"); Button salvesta=new Button("Salvesta"); Button tryki=new Button("Trüki"); Button kustuta=new Button("Kustuta"); String failinimi="joonistusandmed.xml"; Choice kujundivalik=new Choice(); Document d; Node juur, kujund; public XMLJoonis2(){ setLayout(new FlowLayout()); add(nupp); add(salvesta); add(tryki); add(kustuta); add(kujundivalik); nupp.addActionListener(this); salvesta.addActionListener(this); tryki.addActionListener(this); kustuta.addActionListener(this); addMouseListener(this); addMouseMotionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt){ System.exit(0); } }); kujundivalik.add("Joon"); kujundivalik.add("Ristkülik"); alusta(); setSize(400,300); setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource()==nupp){ try{ d=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(failinimi); juur=(Element)d.getFirstChild(); repaint(); } catch(Exception viga){System.out.println("Probleem lugemisel: "+viga); } } if(e.getSource()==salvesta){ try{ Transformer t=TransformerFactory.newInstance().newTransformer(); FileOutputStream valja=new FileOutputStream(failinimi); t.transform(new DOMSource(d), new StreamResult(valja)); valja.close(); } catch(Exception viga){ viga.printStackTrace(); } } if(e.getSource()==tryki){System.out.println(d.getFirstChild());} if(e.getSource()==kustuta){alusta();} } public void paint(Graphics g){ NodeList jooned=((Element)d.getFirstChild()).getElementsByTagName("joon"); for(int i=0; ix2){ int abi=x1; x1=x2; x2=abi; } if(y1>y2){ int abi=y1; y1=y2; y2=abi; } g.drawRect(x1, y1, x2-x1, y2-y1); } } public void mousePressed(MouseEvent e) { if(kujundivalik.getSelectedIndex()==0){ kujund=d.createElement("joon"); } if(kujundivalik.getSelectedIndex()==1){ kujund=d.createElement("ristkylik"); } juur.appendChild(kujund); lisaPunkt(e.getX(), e.getY()); } public void mouseDragged(MouseEvent e){ if(kujundivalik.getSelectedIndex()==0){ lisaPunkt(e.getX(), e.getY()); } } public void mouseReleased(MouseEvent e) { if(kujundivalik.getSelectedIndex()==1){ lisaPunkt(e.getX(), e.getY()); } repaint(); } void lisaPunkt(int x, int y){ Element punkt=d.createElement("punkt"); Element px=d.createElement("x"); px.appendChild(d.createTextNode(x+"")); punkt.appendChild(px); Element py=d.createElement("y"); py.appendChild(d.createTextNode(y+"")); punkt.appendChild(py); kujund.appendChild(punkt); } public void alusta(){ try{ d=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); juur=d.createElement("koordinaadid"); d.appendChild(juur); repaint(); } catch(Exception viga){System.out.println("Viga dokumendi loomisel: "+viga);} } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e){} public static void main(String argumendid[]) throws Exception{ new XMLJoonis2(); } }