Added SVG/PNG export
This commit is contained in:
parent
86a2ee195b
commit
2af3448a51
9
.idea/libraries/org_jfree_svg_5_0_5.xml
Normal file
9
.idea/libraries/org_jfree_svg_5_0_5.xml
Normal file
|
@ -0,0 +1,9 @@
|
|||
<component name="libraryTable">
|
||||
<library name="org.jfree.svg-5.0.5">
|
||||
<CLASSES>
|
||||
<root url="jar://$PROJECT_DIR$/lib/org.jfree.svg-5.0.5.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
0
.idea/misc.xml
Executable file → Normal file
0
.idea/misc.xml
Executable file → Normal file
124
.idea/uiDesigner.xml
Normal file
124
.idea/uiDesigner.xml
Normal file
|
@ -0,0 +1,124 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
|
@ -7,5 +7,6 @@
|
|||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="org.jfree.svg-5.0.5" level="project" />
|
||||
</component>
|
||||
</module>
|
BIN
lib/org.jfree.svg-5.0.5.jar
Normal file
BIN
lib/org.jfree.svg-5.0.5.jar
Normal file
Binary file not shown.
|
@ -1,5 +1,12 @@
|
|||
import org.jfree.svg.SVGGraphics2D;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Main program layout
|
||||
|
@ -57,11 +64,12 @@ public class Chess {
|
|||
public static JMenuBar createMenuBar() {
|
||||
menuBar = new JMenuBar();
|
||||
|
||||
JMenu menu = new JMenu("Game");
|
||||
JMenu menuGame = new JMenu("Game");
|
||||
JMenu menuExport = new JMenu("Export");
|
||||
|
||||
JMenuItem startNewGame = new JMenuItem("Start new game");
|
||||
startNewGame.addActionListener(l -> newGame(Chessboard.fromFEN(DEFAULT_FEN)));
|
||||
menu.add(startNewGame);
|
||||
menuGame.add(startNewGame);
|
||||
|
||||
JMenuItem newGameFromFEN = new JMenuItem("Load from FEN");
|
||||
newGameFromFEN.addActionListener(l -> {
|
||||
|
@ -71,16 +79,25 @@ public class Chess {
|
|||
if(c != null) newGame(c);
|
||||
else JOptionPane.showMessageDialog(window, "Invalid FEN!");
|
||||
});
|
||||
menu.add(newGameFromFEN);
|
||||
menuGame.add(newGameFromFEN);
|
||||
|
||||
JMenuItem toggleBlindMode = new JMenuItem("Toggle blind mode");
|
||||
toggleBlindMode.addActionListener(l -> {
|
||||
chessboard.blindMode = !chessboard.blindMode;
|
||||
chessboard.repaint();
|
||||
});
|
||||
menu.add(toggleBlindMode);
|
||||
menuGame.add(toggleBlindMode);
|
||||
|
||||
menuBar.add(menu);
|
||||
JMenuItem exportAsSvg = new JMenuItem("Export as SVG");
|
||||
exportAsSvg.addActionListener(l -> exportSVG());
|
||||
menuExport.add(exportAsSvg);
|
||||
|
||||
JMenuItem exportAsPng = new JMenuItem("Export as PNG");
|
||||
exportAsPng.addActionListener(l -> exportPNG());
|
||||
menuExport.add(exportAsPng);
|
||||
|
||||
menuBar.add(menuGame);
|
||||
menuBar.add(menuExport);
|
||||
|
||||
return menuBar;
|
||||
}
|
||||
|
@ -100,4 +117,31 @@ public class Chess {
|
|||
repaintWindow();
|
||||
}
|
||||
|
||||
static void exportSVG() {
|
||||
SVGGraphics2D g2 = new SVGGraphics2D(1000, 1000);
|
||||
chessboard.paint(g2, 1000, 1000);
|
||||
String svg = g2.getSVGElement();
|
||||
try {
|
||||
FileWriter bw = new FileWriter("chess.svg");
|
||||
bw.append(svg);
|
||||
bw.close();
|
||||
JOptionPane.showMessageDialog(chessboard, "Exported SVG file 'chess.svg' was saved to program's folder.");
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(chessboard, "Saving SVG file failed!");
|
||||
}
|
||||
}
|
||||
|
||||
static void exportPNG() {
|
||||
BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = image.createGraphics();
|
||||
chessboard.paint(g, 1000, 1000);
|
||||
g.dispose();
|
||||
try {
|
||||
ImageIO.write(image, "png", new File("chess.png"));
|
||||
JOptionPane.showMessageDialog(chessboard, "Exported PNG file 'chess.png' was saved to program's folder.");
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(chessboard, "Saving PNG file failed!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import org.jfree.svg.SVGGraphics2D;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
|
@ -164,21 +166,30 @@ public class Chessboard extends JPanel {
|
|||
|
||||
/**
|
||||
* Paint the chessboard
|
||||
* @param g the <code>Graphics</code> context in which to paint
|
||||
* @param g Graphics context
|
||||
*/
|
||||
public void paint(Graphics g) {
|
||||
paint(g, this.getWidth(), this.getHeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Paint the chessboard
|
||||
* @param g Graphics context
|
||||
* @param panelWidth chessboard width in px
|
||||
* @param panelHeight chessboard height in px
|
||||
*/
|
||||
public void paint(Graphics g, double panelWidth, double panelHeight) {
|
||||
|
||||
Graphics2D g2 = getGraphics2D(g);
|
||||
|
||||
double cx = this.getWidth() / 2.0;
|
||||
double cy = this.getHeight() / 2.0;
|
||||
double cx = panelWidth / 2.0;
|
||||
double cy = panelHeight / 2.0;
|
||||
|
||||
boardScale = Math.min(this.getWidth(), this.getHeight())/ BOARD_WIDTH;
|
||||
boardScale = Math.min(panelWidth, panelHeight)/BOARD_WIDTH;
|
||||
|
||||
startX = cx-(BOARD_WIDTH/2.0*boardScale);
|
||||
startY = cy-(BOARD_WIDTH/2.0*boardScale);
|
||||
|
||||
AffineTransform beforeBoard = g2.getTransform();
|
||||
g2.translate(startX, startY);
|
||||
g2.scale(boardScale, boardScale);
|
||||
|
||||
|
|
Loading…
Reference in a new issue