Initial code commit

This commit is contained in:
Ninetailed 2016-02-03 14:26:31 +00:00
parent a20e416d52
commit fdb940f689
6 changed files with 353 additions and 8 deletions

12
.gitignore vendored
View file

@ -1,12 +1,10 @@
# Compiled Java files and packages
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# Script-generated library files
lib/filters
lib/Filters.zip

View file

@ -1,2 +1,4 @@
# icongen
Procedural icon generator
Procedural icon generator. Released under MPL-2.0 (see LICENSE).
Requires Java Image Filters (Apache License) from http://jhlabs.com/ip/filters/

39
build.xml Normal file
View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="icongen" default="jar">
<description>Procedural icon generator</description>
<property name="version" value="0.1"/>
<path id="lib.path">
<fileset refid="lib.jars"/>
</path>
<fileset id="lib.jars" dir="lib">
<include name="filters/dist/Filters.jar"/>
</fileset>
<target name="clean">
<delete dir="bin"/>
</target>
<target name="lib">
<mkdir dir="lib"/>
<!-- JH Labs Image Filters -->
<get dest="lib/Filters.zip" src="http://jhlabs.com/ip/filters/Filters.zip"/>
<mkdir dir="lib/filters"/>
<unzip src="lib/Filters.zip" dest="lib/filters"/>
<ant dir="lib/filters"/>
</target>
<target name="compile" depends="lib">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" includeAntRuntime="no" classpathref="lib.path"/>
</target>
<target name="jar" depends="compile">
<jar jarfile="${ant.project.name}-${version}.jar" basedir="bin">
<zipgroupfileset refid="lib.jars"/>
</jar>
</target>
</project>

View file

@ -0,0 +1,108 @@
package uk.ninetailed.icongen;
import com.jhlabs.image.GaussianFilter;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Icon
extends Canvas
{
public static final int DEFAULT_NUM_LINES = 3;
public static final float DEFAULT_GLOW_LEVEL = 6.0F;
private static final long serialVersionUID = 1L;
private static final Random RANDOM = new Random();
private final BufferedImage image;
private boolean symmetry = true;
private float glowLevel = 6.0F;
private int numLines = 3;
private boolean drawn = false;
public Icon(int width, int height)
{
Dimension size = new Dimension(width, height);
setMinimumSize(size);
setPreferredSize(size);
setMaximumSize(size);
this.image = new BufferedImage(width, height, 2);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(this.image, 0, 0, Color.BLACK, this);
}
public void draw()
{
Dimension size = getPreferredSize();
int width = size.width - 1;
int height = size.height - 1;
BufferedImage src = new BufferedImage(width, height, 2);
Graphics2D g = src.createGraphics();
g.setColor(Color.getHSBColor(RANDOM.nextFloat(), 0.5F, 1.0F));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (int line = 0; line < this.numLines; line++)
{
int x1 = RANDOM.nextInt(width);
int x2 = RANDOM.nextInt(width);
int y1 = RANDOM.nextInt(height);
int y2 = RANDOM.nextInt(height);
g.drawLine(x1, y1, x2, y2);
if (this.symmetry) {
g.drawLine(width - x1, y1, width - x2, y2);
}
}
new GaussianFilter(this.glowLevel).filter(src, this.image);
Graphics2D g2 = this.image.createGraphics();
g2.drawImage(this.image, 0, 0, null, null);
g2.drawImage(src, 0, 0, null, null);
this.drawn = true;
}
public boolean isSymmetry()
{
return this.symmetry;
}
public void setSymmetry(boolean symmetry)
{
this.symmetry = symmetry;
}
public float getGlowLevel()
{
return this.glowLevel;
}
public void setGlowLevel(float glowLevel)
{
this.glowLevel = glowLevel;
}
public int getNumberOfLines()
{
return this.numLines;
}
public void setNumberOfLines(int numLines)
{
this.numLines = numLines;
}
public BufferedImage getImage()
{
if (!this.drawn) {
draw();
}
return this.image;
}
}

View file

@ -0,0 +1,24 @@
package uk.ninetailed.icongen;
import java.awt.Dimension;
import java.awt.Toolkit;
public class IconGen
{
private static final int MAX_WIDTH = 800;
private static final int MAX_HEIGHT = 600;
public static void main(String[] args)
{
MainWindow mainWindow = new MainWindow();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = Math.min(screenSize.width * 3 / 4, 800);
int height = Math.min(screenSize.height * 3 / 4, 600);
mainWindow.setSize(width, height);
mainWindow.setLocation(screenSize.width / 2 - width / 2, screenSize.height / 2 - height / 2);
mainWindow.setVisible(true);
}
}

View file

@ -0,0 +1,174 @@
package uk.ninetailed.icongen;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JViewport;
import javax.swing.SpinnerNumberModel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MainWindow
extends JFrame
{
private static final long serialVersionUID = 1L;
private static final String TITLE = "Icons!";
private static final int DEFAULT_NUM_ICONS = 100;
private static final int ICON_FRAME_WIDTH = 64;
private static final int ICON_FRAME_HEIGHT = 64;
private static final JFileChooser FILE_CHOOSER = new JFileChooser();
private final JPanel iconPanel;
private final SpinnerNumberModel numIconsModel;
private final SpinnerNumberModel numLinesModel;
private final SpinnerNumberModel glowLevelModel;
private final JCheckBox symmetry;
private int numIcons;
static
{
FILE_CHOOSER.setFileFilter(new FileNameExtensionFilter("PNG file", new String[] { "png" }));
}
public MainWindow()
{
super("Icons!");
final GridLayout iconGrid = new GridLayout(0, 20);
this.iconPanel = new JPanel(iconGrid);
final JScrollPane iconScroller = new JScrollPane(this.iconPanel, 22,
31);
iconScroller.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
int newCols = iconScroller.getViewport().getWidth() / 64;
if (iconGrid.getColumns() != newCols)
{
iconGrid.setColumns(newCols);
Dimension panelSize = new Dimension(iconScroller.getViewport().getWidth(), 64 * (
MainWindow.this.numIcons / newCols + 1));
MainWindow.this.iconPanel.setPreferredSize(panelSize);
MainWindow.this.iconPanel.setMaximumSize(panelSize);
}
}
});
JPanel controlPanel = new JPanel(new GridLayout(1, 0));
this.numIconsModel = new SpinnerNumberModel(100, 0, Integer.MAX_VALUE, 1);
this.numLinesModel = new SpinnerNumberModel(3, 1, 16, 1);
this.glowLevelModel = new SpinnerNumberModel(6.0D, 0.0D, 3.4028234663852886E38D, 0.5D);
JButton generateButton = new JButton("Generate!");
generateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MainWindow.this.generateIcons();
}
});
this.symmetry = new JCheckBox("Symmetry");
this.symmetry.setSelected(true);
controlPanel.add(new JLabel("Number of Icons:"));
controlPanel.add(new JSpinner(this.numIconsModel));
controlPanel.add(new JLabel("Number of Lines:"));
controlPanel.add(new JSpinner(this.numLinesModel));
controlPanel.add(new JLabel("Glow Level:"));
controlPanel.add(new JSpinner(this.glowLevelModel));
controlPanel.add(this.symmetry);
controlPanel.add(generateButton);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(iconScroller, "Center");
contentPane.add(controlPanel, "South");
generateIcons();
setDefaultCloseOperation(2);
}
private void generateIcons()
{
this.numIcons = this.numIconsModel.getNumber().intValue();
Icon[] icons = new Icon[this.numIcons];
for (int iconNumber = 0; iconNumber < this.numIcons; iconNumber++)
{
Icon icon = new Icon(64, 64);
icon.setGlowLevel(this.glowLevelModel.getNumber().floatValue());
icon.setNumberOfLines(this.numLinesModel.getNumber().intValue());
icon.setSymmetry(this.symmetry.isSelected());
icon.draw();
icon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if ((e.getButton() == 1) &&
(MainWindow.FILE_CHOOSER.showSaveDialog(MainWindow.this) == 0)) {
try
{
File f = MainWindow.FILE_CHOOSER.getSelectedFile();
if (!f.getName().toLowerCase().endsWith(".png")) {
f = new File(f.getParent(), f.getName() + ".png");
}
if (f.exists())
{
int option = JOptionPane.showConfirmDialog(MainWindow.this, "Overwrite " + f.getName() +
"?", "Confirm Overwrite", 0);
if (option != 0) {
return;
}
}
BufferedImage image = ((Icon)e.getComponent()).getImage();
ImageIO.write(image, "PNG", f);
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(MainWindow.this, "Error saving: " + ex.getMessage(), "Error",
0);
}
}
}
});
icons[iconNumber] = icon;
}
this.iconPanel.removeAll();
for (Icon icon : icons)
{
this.iconPanel.add(icon);
}
Dimension size = this.iconPanel.getPreferredSize();
size.height = (64 * ((GridLayout)this.iconPanel.getLayout()).getColumns());
this.iconPanel.setPreferredSize(size);
this.iconPanel.setMaximumSize(size);
Icon programIcon = new Icon(32, 32);
programIcon.setGlowLevel(this.glowLevelModel.getNumber().floatValue());
programIcon.setNumberOfLines(this.numLinesModel.getNumber().intValue());
programIcon.setSymmetry(this.symmetry.isSelected());
programIcon.draw();
setIconImage(programIcon.getImage());
validate();
}
}