Allow different colors

master
Jonas Thiel 2013-03-11 18:16:46 +01:00
rodzic b63eec304e
commit d19d604110
7 zmienionych plików z 121 dodań i 47 usunięć

Wyświetl plik

@ -4,4 +4,6 @@ outputFile=out.png
width=500
height=636
minSize=4
maxSize=38
maxSize=38
colors=#000000,#3a3a3a,#787878,#b2b2b2
background=#ffffff

Wyświetl plik

@ -0,0 +1,10 @@
import java.awt.*;
public class ColorHelper {
public static int transform(Color c){
return (c.getAlpha() << 24) | (c.getRed() << 16) | (c.getGreen() << 8) | c.getBlue();
}
public static int decode(String c){
return transform(Color.decode(c));
}
}

Wyświetl plik

@ -16,9 +16,10 @@ public class Configuration {
public int getHeight(){ return Integer.parseInt(prop.getProperty("height")); }
public int getMinSize(){ return Integer.parseInt(prop.getProperty("minSize")); }
public int getMaxSize(){ return Integer.parseInt(prop.getProperty("maxSize")); }
public String getColors(){ return prop.getProperty("colors"); }
public String getBackgroundColor(){return prop.getProperty("background"); }
public boolean isDebug(){
return prop.getProperty("debug") == "true";
return prop.getProperty("debug") != null && prop.getProperty("debug").equals("true");
}
public void load(){

Wyświetl plik

@ -0,0 +1,42 @@
import wordcram.Word;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class InputWords {
private String file;
public InputWords(String file){
this.file = file;
}
public Word[] getWords(){
ArrayList<Word> words = new ArrayList<Word>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
return new Word[0];
}
String line = null;
try {
while((line = reader.readLine()) != null){
String[] values = line.split(",");
if(values.length == 2){
words.add( new Word(values[0].trim(), Float.parseFloat(values[1].trim())));
}
}
} catch (IOException e) {
e.printStackTrace();
return new Word[0];
}
return words.toArray(new Word[words.size()]);
}
}

Wyświetl plik

@ -1,3 +1,6 @@
/*
* Copied from: http://wordcram.org/2013/02/13/shapes-for-wordcram/
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
@ -18,13 +21,12 @@ import processing.core.PVector;
import wordcram.Word;
import wordcram.WordNudger;
import wordcram.WordPlacer;
class ShapeBasedPlacer implements WordPlacer, WordNudger {
public static int TOLERANCE = 5;
public static int TOLERANCE = 5;
public static boolean PRECISE = false;
public static int GLYPH_SIZE = 500;
public static int FONT_STYLE = Font.PLAIN;
public static int GLYPH_SIZE = 500;
public static int FONT_STYLE = Font.PLAIN;
Area area;
float minX;

Wyświetl plik

@ -2,6 +2,7 @@ import processing.core.*;
import wordcram.WordCram;
import java.awt.*;
import java.awt.image.PackedColorModel;
import java.io.*;
import java.util.ArrayList;
@ -16,65 +17,55 @@ public class Tree extends PApplet {
public void setup(){
Configuration config = Configuration.getInstance();
size(config.getWidth(),config.getHeight());
background(255);
background(ColorHelper.decode(config.getBackgroundColor()));
ShapeBasedPlacer placer = ShapeBasedPlacer.fromFile(config.getShapeFile(), Color.black);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(config.getInputFile()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ArrayList<Word> words = new ArrayList<Word>();
String line = null;
try {
while((line = reader.readLine()) != null){
String[] values = line.split(",");
if(values.length == 2){
words.add( new Word(values[0].trim(), Float.parseFloat(values[1].trim())));
}
}
} catch (IOException e) {
e.printStackTrace();
}
ShapeBasedPlacer placer = ShapeBasedPlacer.fromFile(config.getShapeFile(), Color.black);
InputWords input = new InputWords(config.getInputFile());
TreeColorer colorer = new TreeColorer(config.getColors());
cram = new WordCram(this)
.fromWords(words.toArray(new Word[words.size()]))
.fromWords(input.getWords())
.withColorer(colorer)
.withPlacer(placer)
.withNudger(placer)
.sizedByWeight(config.getMinSize(),config.getMaxSize());
}
public void draw() {
if(this.draw){
System.out.println("Start drawing tag cloud...");
cram.drawAll();
this.draw = false;
System.out.println("Finished drawing");
//tell me what didnt get drawn
if(Configuration.getInstance().isDebug()){
int noSpace = 0;
int tooSmall = 0;
Word[] skippedWords = cram.getSkippedWords();
Word[] placedWords = cram.getWords();
for (Word skipped: skippedWords) {
if (skipped.wasSkippedBecause() == WordCram.NO_SPACE) {
noSpace++;
} else if (skipped.wasSkippedBecause() == WordCram.SHAPE_WAS_TOO_SMALL) {
tooSmall++;
}
}
System.out.println("Total placed Words: " + placedWords.length);
System.out.println("Total Skipped Words: " + skippedWords.length);
System.out.println("Skipped because no Space: " + noSpace);
System.out.println("Skipped because too small: " + tooSmall);
System.out.println("Finished");
printDebug();
}
save(Configuration.getInstance().getOutputFile());
exit();
}
}
private void printDebug(){
//tell me what didnt get drawn
int noSpace = 0;
int tooSmall = 0;
Word[] skippedWords = cram.getSkippedWords();
Word[] placedWords = cram.getWords();
for (Word skipped: skippedWords) {
if (skipped.wasSkippedBecause() == WordCram.NO_SPACE) {
noSpace++;
} else if (skipped.wasSkippedBecause() == WordCram.SHAPE_WAS_TOO_SMALL) {
tooSmall++;
}
}
System.out.println("Total placed Words: " + placedWords.length);
System.out.println("Total Skipped Words: " + skippedWords.length);
System.out.println("Skipped because no Space: " + noSpace);
System.out.println("Skipped because too small: " + tooSmall);
System.out.println("Finished");
}
}

Wyświetl plik

@ -0,0 +1,26 @@
import wordcram.Word;
import wordcram.WordColorer;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class TreeColorer implements WordColorer {
private ArrayList<Integer> colors = new ArrayList<Integer>();
private Random randomGenerator = new Random();
public TreeColorer(String colorString){
for(String c : colorString.split(",")){
colors.add(ColorHelper.decode(c));
}
if(colors.isEmpty()){
colors.add(ColorHelper.decode("#000000"));
}
}
@Override
public int colorFor(Word word) {
return colors.get(randomGenerator.nextInt(colors.size()));
}
}