A więc czy to może tak wyglądać? Czy proponujecie jakieś lepsze rozwiązanie?
package programdografikiwektorowej;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class ProgramDoGrafikiWektorowej {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new OknoGlowne();
//frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
frame.setLocationRelativeTo(null);
frame.setTitle("XMLWriteTest");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
package programdografikiwektorowej;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.beans.EventHandler;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class OknoGlowne extends JFrame{
public int szerokoscPlotna = 700;
public int wysokoscPlotna = 600;
public int Xpocz, Ypocz, Xkon, Ykon;
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItemZapisz;
private JFileChooser chooser;
public Rectangle comp2;
public Ellipse comp3;
public Line comp1;
//enum Tool {Line, Rectangle, Ellipse}
//Tool wybraneNarzedzie;
public OknoGlowne()
{
super("Program do grafiki wektorowej");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
comp1 = new Line();
add(comp1);
comp2 = new Rectangle();
add(comp2);
comp3 = new Ellipse();
add(comp3);
menuBar = new JMenuBar();
menu = new JMenu("File");
menuBar.add(menu);
menuItemZapisz = new JMenuItem("Zapisz");
menu.add(menuItemZapisz);
menuItemZapisz.addActionListener(EventHandler.create(ActionListener.class, this,"saveDocument"));
chooser = new JFileChooser();
Box lewyBox = Box.createVerticalBox();
ButtonGroup radioGroup = new ButtonGroup();
final JRadioButton rysujLinieRadioB = new JRadioButton("Rysuj linię");
final JRadioButton rysujKwadratRadioB = new JRadioButton("Rysuj kwadrat");
final JRadioButton rysujKoloRadioB = new JRadioButton("Rysuj koło");
radioGroup.add(rysujLinieRadioB);
radioGroup.add(rysujKwadratRadioB);
radioGroup.add(rysujKoloRadioB);
rysujLinieRadioB.setSelected(true);
lewyBox.add(rysujLinieRadioB);
lewyBox.add(rysujKwadratRadioB);
lewyBox.add(rysujKoloRadioB);
Box prawyBox = Box.createVerticalBox();
JPanel panelRysowania=new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
for (int i = 0; i < Line.lines.size(); i++)
{
g2.draw(Line.lines.get(i));
}
for (int i = 0; i < Rectangle.rects.size(); i++)
{
g2.fill(Rectangle.rects.get(i));
}
for (int i = 0; i < Ellipse.ellipse.size(); i++)
{
g2.fill(Ellipse.ellipse.get(i));
}
}
};
panelRysowania.setBackground(Color.white);
panelRysowania.setPreferredSize(new Dimension(szerokoscPlotna, wysokoscPlotna));
prawyBox.add(panelRysowania);
Box polaczenieLP = Box.createHorizontalBox();
polaczenieLP.add(lewyBox);
polaczenieLP.add(prawyBox);
Box glownyBox = Box.createVerticalBox();
glownyBox.add(menuBar);
glownyBox.add(polaczenieLP);
panelRysowania.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Xpocz=e.getX();
Ypocz=e.getY();
}
});
panelRysowania.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(rysujLinieRadioB.isSelected()==true)
{
Xkon=e.getX();
Ykon=e.getY();
Line.lines.add(new Line2D.Double(Xpocz,Ypocz,Xkon,Ykon));
}
else if(rysujKwadratRadioB.isSelected()==true)
{
Xkon=e.getX()-Xpocz;
Ykon=e.getY()-Ypocz;
Rectangle.rects.add(new Rectangle2D.Double(Xpocz,Ypocz,Xkon,Ykon));
}
else if(rysujKoloRadioB.isSelected()==true)
{
Xkon=e.getX()-Xpocz;
Ykon=e.getY()-Ypocz;
Ellipse.ellipse.add(new Ellipse2D.Double(Xpocz,Ypocz,Xkon,Ykon));
}
repaint();
}
});
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(menuBar, BorderLayout.PAGE_START);
content.add(glownyBox, BorderLayout.CENTER);
this.pack();
setVisible(true);
}
public void saveDocument() throws TransformerException, IOException
{
if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return;
File file = chooser.getSelectedFile();
Document doc = comp1.buildDocument();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
"http://www.w3.org/TR/2000/CR-SVG-20000802/DTD/svg-20000802.dtd");
t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD SVG 20000802//EN");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
t.transform(new DOMSource(doc), new StreamResult(Files.newOutputStream(file.toPath())));
}
}
package programdografikiwektorowej;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Shape extends JComponent {
public static DocumentBuilder builder;
public int szerokoscPlotna = 700;
public int wysokoscPlotna = 600;
public Document buildDocument()
{
String namespace = "http://www.w3.org/2000/svg";
Document dokument = builder.newDocument();
Element svgElement = dokument.createElementNS(namespace, "svg");
dokument.appendChild(svgElement);
svgElement.setAttribute("width", "" + szerokoscPlotna );
svgElement.setAttribute("height", "" + wysokoscPlotna);
for (int i = 0; i <Line.lines.size(); i++)
{
Line2D e=Line.lines.get(i);
Element lineElement = dokument.createElementNS(namespace, "line");
lineElement.setAttribute("x1", "" + e.getX1());
lineElement.setAttribute("y1", "" + e.getY1() );
lineElement.setAttribute("x2", "" + e.getX2());
lineElement.setAttribute("y2", "" + e.getY2());
lineElement.setAttribute("style", "stroke:black ;stroke-width:1" );
lineElement.setAttribute("fill", "#000000");
svgElement.appendChild(lineElement);
}
for (int i = 0; i < Rectangle.rects.size(); i++)
{
Rectangle2D r = Rectangle.rects.get(i);
Element rectElement = dokument.createElementNS(namespace, "rect");
rectElement.setAttribute("x", "" + r.getX());
rectElement.setAttribute("y", "" + r.getY());
rectElement.setAttribute("width", "" + r.getWidth());
rectElement.setAttribute("height", "" + r.getHeight());
rectElement.setAttribute("fill", "#000000");
svgElement.appendChild(rectElement);
}
for (int i = 0; i < Ellipse.ellipse.size(); i++)
{
Ellipse2D e=Ellipse.ellipse.get(i);
Element ellipseElement = dokument.createElementNS(namespace, "ellipse");
ellipseElement.setAttribute("cx", "" + (e.getX()+e.getWidth()/2) );
ellipseElement.setAttribute("cy", "" + (e.getY()+e.getHeight()/2) );
ellipseElement.setAttribute("rx", "" + e.getWidth()/2);
ellipseElement.setAttribute("ry", "" + e.getHeight()/2);
ellipseElement.setAttribute("fill", "#000000");
svgElement.appendChild(ellipseElement);
}
return dokument;
}
}
class Line extends Shape{
public static java.util.List<Line2D> lines;
public Line( ) {
lines = new ArrayList<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try
{
Shape.builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
}
}
class Rectangle extends Shape{
public static java.util.List<Rectangle2D> rects;
public Rectangle( ){
rects = new ArrayList<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try
{
Shape.builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
}
}
class Ellipse extends Shape{
public static java.util.List<Ellipse2D> ellipse;
public Ellipse( ){
ellipse = new ArrayList<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try
{
Shape.builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
}
}