Elsewhere there are things that we all miss, yet it takes just one to notice...

Just a fabulous piece of Java code

Now and then when coding you come up with something just awesome.

Although Java is not exactly my strong point, I’ve become more and more familiar with it to the point I can now write classes with multiple sub-classes with inheritance, etc. The best thing about this now, is I can imagine the whole thing and create just the one class to cover everything. Just like I do in C++.

Here’s a snippet:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/*
* Below is a fabulous piece of code that does wonders the universe has never seen before.
* All keys on the keyboard where gently pressed throughout as not to cause any
* disturbance whatsoever within the vast cosmos.
* Of course the old code will soon be on its way to code heaven in the shredded pieces
* it was coded in, un-loved and forgotten for all time. Sad really with a brilliant ending.
*/
public class EventData implements Serializable {
private static final long serialVersionUID = 1792205787159647135L;
public static final String ModBG = "BG";
public static final String ModRES = "RES";
public static final String ModORIENT = "ORIENT";
public static final String ModCOLORBAR = "COLORBAR";
public static final String ModSTATICIMG = "STATICIMG";
public static final String ModSLIDESHOW = "SLIDESHOW";
public static final String ModSIMPLETIME = "SIMPLETIME";
public static final String ModSIMPLETICK = "SIMPLETICK";
public static final int ModBG_size = 4; // r, b, g
public static final int ModRES_size = 3; // w, h
public static final int ModORIENT_size = 2; // orient
public static final int ModCOLORBAR_size = 21; // x,y,x,y,r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a
public static final int ModSTATICIMG_size = 13; // x,y,x,y,c,f,w,h,r,g,b,a
public static final int ModSLIDESHOW_size = 7; // x,y,x,y,w,h, [...]
public static final int ModSIMPLETIME_size = 17; // x,y,x,y,w,h,d,d,t,fs,fa,fn,r,g,b,a
public static final int ModSIMPLETICK_size = 15; // x,y,x,y,w,h,list,font,size,speed,r,g,b,a
public static final String OrientLandscape = "land";
public static final String OrientPortrait = "port";
public String eventname = "";
public List items = new ArrayList();
public EventData() {}
public EventData(String name) {
eventname = name;
}
public static class EventSize implements Serializable {
private static final long serialVersionUID = 6876826929649517879L;
public int width = 800, height = 600;
public EventSize() {}
}
public EventSize getEventSize() { // should be at index 1 in all cases after BG
EventSize size = new EventSize();
for (ModuleBase item : items) {
if (item instanceof ModuleRES) {
ModuleRES res = (ModuleRES)item;
size.width = res.wid;
size.height = res.hgt;
}
if (item instanceof ModuleORIENT) {
ModuleORIENT ori = (ModuleORIENT)item;
if (ori.orient.equals("port") && size.width > size.height) {
int temp = size.width;
size.width = size.height;
size.height = temp;
}
}
}
return size;
}
public void swap(int one, int two) { // used for moving items up and down
int size = items.size();
if (one < 0 || one >= size || two < 0 || two >= size || one == two) return;
ModuleBase temp = items.get(one);
items.set(one, items.get(two));
items.set(two, temp);
}
public void remove(int index) { // removes item from list
int size = items.size();
if (index < 0 || index >= size) return;
items.remove(index);
}
public String getString() {
String result = "";
for (ModuleBase item : items) {
result += item.getString() + "\n";
}
return result;
}
public List getStrings() {
List list = new ArrayList();
for (ModuleBase item : items) {
list.add(item.getString());
}
return list;
}
public void fromStrings(List all) {
if (all == null) return; // sanity check
items.clear();
for (String mod : all) {
String[] module = mod.split(":");
String id = module[0];
// had to use string.equals(string) instead of a switch case
if (ModBG.equals(id)) {
ModuleBG bg = new ModuleBG();
bg.fromString(mod);
items.add(bg);
}
if (ModRES.equals(id)) {
ModuleRES res = new ModuleRES();
res.fromString(mod);
items.add(res);
}
if (ModORIENT.equals(id)) {
ModuleORIENT ori = new ModuleORIENT();
ori.fromString(mod);
items.add(ori);
}
if (ModCOLORBAR.equals(id)) {
ModuleCOLORBAR cb = new ModuleCOLORBAR();
cb.fromString(mod);
items.add(cb);
}
if (ModSTATICIMG.equals(id)) {
ModuleSTATICIMG img = new ModuleSTATICIMG();
img.fromString(mod);
items.add(img);
}
if (ModSLIDESHOW.equals(id)) {
ModuleSLIDESHOW slide = new ModuleSLIDESHOW();
slide.fromString(mod);
items.add(slide);
}
if (ModSIMPLETIME.equals(id)) {
ModuleSIMPLETIME time = new ModuleSIMPLETIME();
time.fromString(mod);
items.add(time);
}
if (ModSIMPLETICK.equals(id)) {
ModuleSIMPLETICK tick = new ModuleSIMPLETICK();
tick.fromString(mod);
items.add(tick);
}
}
}
public static class ModuleBase implements Serializable {
private static final long serialVersionUID = 7296202779507964140L;
public String ID = "";
public ModuleBase() {}
public ModuleBase(String name) { ID = name; }
public String getString() { return null; }
public void fromString(String mod) {}
}
private static String[] splitModuleString(String mod) {
return mod.split("[:,\\;]+");
}
public static class ModuleBG extends ModuleBase implements Serializable {
private static final long serialVersionUID = -7118503167269811353L;
public int R, G, B;
public ModuleBG() { 
super(ModBG); 
R = G = B = 0; 
}
public ModuleBG(int r, int g, int b) { 
super(ModBG);
R = r; G = g; B = b; 
}
@Override
public String getString() {
return ModBG + ":" + R + "," + G + "," + B + ";";
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length != ModBG_size) return;
R = Integer.parseInt(items[1]);
G = Integer.parseInt(items[2]);
B = Integer.parseInt(items[3]);
}
}
public static class ModuleRES extends ModuleBase implements Serializable {
private static final long serialVersionUID = 4781801123653088430L;
public int wid, hgt;
public ModuleRES() {
super(ModRES);
wid = 800; hgt = 600;
}
public ModuleRES(int width, int height) {
super(ModRES);
wid = width; hgt = height;
}
@Override
public String getString() {
return ModRES + ":" + wid + "," + hgt + ";";		
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length != ModRES_size) return;
wid = Integer.parseInt(items[1]);
hgt = Integer.parseInt(items[2]);
}
}
public static class ModuleORIENT extends ModuleBase implements Serializable {
private static final long serialVersionUID = -3716182347259139931L;
public String orient = "";
public ModuleORIENT() {
super(ModORIENT);
orient = OrientLandscape;
}
public ModuleORIENT(String ori) {
super(ModORIENT);
if (ori == null) { orient = OrientLandscape; return; }
if (ori.equals(OrientLandscape) || ori.equals(OrientPortrait)) {
orient = ori;
} else {
orient = OrientLandscape;
}
}
public ModuleORIENT(int ori) {
if (ori == 0) orient = OrientLandscape;
else orient = OrientPortrait;
}
@Override
public String getString() {
return ModORIENT + ":" + orient + ";";
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length != ModORIENT_size) return;
orient = items[1];
}
}
public static class ModuleCOLORBAR extends ModuleBase implements Serializable {
private static final long serialVersionUID = -8109805627179506701L;
public double x1 = 0, y1 = 0;
public double x2 = 0, y2 = 0;
public int r1 = 0, g1 = 0, b1 = 0, a1 = 0;
public int r2 = 0, g2 = 0, b2 = 0, a2 = 0;
public int r3 = 0, g3 = 0, b3 = 0, a3 = 0;
public int r4 = 0, g4 = 0, b4 = 0, a4 = 0;
public ModuleCOLORBAR() {
super(ModCOLORBAR);
}
public void setCoords(double xpos1, double ypos1, double xpos2, double ypos2) {
x1 = xpos1; y1 = ypos1;
x2 = xpos2; y2 = ypos2;
}
public void setRGBA(int id, int r, int g, int b, int a) {
switch (id) {
case 0:
r1 = r; g1 = g; b1 = b; a1 = a;
break;
case 1:
r2 = r; g2 = g; b2 = b; a2 = a;
break;
case 2:
r3 = r; g3 = g; b3 = b; a3 = a;
break;
case 3:
r4 = r; g4 = g; b4 = b; a4 = a;
break;
}
}
@Override
public String getString() {
return ModCOLORBAR + ":" + x1 + "," + y1 + "," + x2 + "," + y2 + "," +
r1 + "," + g1 + "," + b1 + "," + a1 + "," +
r2 + "," + g2 + "," + b2 + "," + a2 + "," +
r3 + "," + g3 + "," + b3 + "," + a3 + "," +
r4 + "," + g4 + "," + b4 + "," + a4 + ";";
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length != ModCOLORBAR_size) return;
x1 = Double.parseDouble(items[1]); // woo look at all them
y1 = Double.parseDouble(items[2]);
x2 = Double.parseDouble(items[3]);
y2 = Double.parseDouble(items[4]);
r1 = Integer.parseInt(items[5]);
g1 = Integer.parseInt(items[6]);
b1 = Integer.parseInt(items[7]);
a1 = Integer.parseInt(items[8]);
r2 = Integer.parseInt(items[9]);
g2 = Integer.parseInt(items[10]);
b2 = Integer.parseInt(items[11]);
a2 = Integer.parseInt(items[12]);
r3 = Integer.parseInt(items[13]);
g3 = Integer.parseInt(items[14]);
b3 = Integer.parseInt(items[15]);
a3 = Integer.parseInt(items[16]);
r4 = Integer.parseInt(items[17]);
g4 = Integer.parseInt(items[18]);
b4 = Integer.parseInt(items[19]);
a4 = Integer.parseInt(items[20]);
}
}
public static class ModuleSTATICIMG extends ModuleBase implements Serializable {
private static final long serialVersionUID = -3275145876622508634L;
public double x1 = 0, y1 = 0;
public double x2 = 0, y2 = 0;
public String category = "";
public String filename = "";
public int texture_wid = 1;
public int texture_hgt = 1;
public int R = 0, G = 0, B = 0, A = 0;
public ModuleSTATICIMG() {
super(ModSTATICIMG);
}
public void setCoords(double xpos1, double ypos1, double xpos2, double ypos2) {
x1 = xpos1; y1 = ypos1;
x2 = xpos2; y2 = ypos2;
}
public void setImage(String img_category, String img_filename) {
category = img_category;
filename = img_filename;
}
public void setTextureSize(int width, int height) {
texture_wid = width;
texture_hgt = height;
}
public void setColor(int r, int g, int b, int a) {
R = r; G = g; B = b; A = a;
}
@Override
public String getString() {
return ModSTATICIMG + ":" + x1 + "," + y1 + "," + x2 + "," + y2 + "," +
category + "," + filename + "," +
texture_wid + "," + texture_hgt + "," +
R + "," + G + "," + B + "," + A + ";";
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length != ModSTATICIMG_size) return;
x1 = Double.parseDouble(items[1]);
y1 = Double.parseDouble(items[2]);
x2 = Double.parseDouble(items[3]);
y2 = Double.parseDouble(items[4]);
category = items[5];
filename = items[6];
texture_wid = Integer.parseInt(items[7]);
texture_hgt = Integer.parseInt(items[8]);
R = Integer.parseInt(items[9]);
G = Integer.parseInt(items[10]);
B = Integer.parseInt(items[11]);
A = Integer.parseInt(items[12]);
}
}
public static class ModuleSLIDESHOW extends ModuleBase implements Serializable {
private static final long serialVersionUID = 7947214485112193194L;
public double x1 = 0, y1 = 0;
public double x2 = 0, y2 = 0;
public int texture_wid = 1;
public int texture_hgt = 1;
public List slide = new ArrayList();
public ModuleSLIDESHOW() {
super (ModSLIDESHOW);
}
public void setCoords(double xpos1, double ypos1, double xpos2, double ypos2) {
x1 = xpos1; y1 = ypos1;
x2 = xpos2; y2 = ypos2;
}
public void setTextureSize(int width, int height) {
texture_wid = width;
texture_hgt = height;
}
public void swap(int one, int two) { // move items up and down the list
int size = slide.size();
if (one < 0 || one >= size || two < 0 || two >= size || one == two) return;
SLIDEBase temp = slide.get(one);
slide.set(one, slide.get(two));
slide.set(two, temp);
}
public void remove(int index) {
int size = slide.size();
if (index < 0 || index >= size) return;
slide.remove(index);
}
@Override
public String getString() { // arcane black magic happens here
String result = ModSLIDESHOW + ":" + x1 + "," + y1 + "," + x2 + "," + y2 + "," +
texture_wid + "," + texture_hgt;
for (SLIDEBase item : slide) {
result += item.getString();
}
result += ";";
return result;
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length < ModSLIDESHOW_size) return;
slide.clear();
x1 = Double.parseDouble(items[1]);
y1 = Double.parseDouble(items[2]);
x2 = Double.parseDouble(items[3]);
y2 = Double.parseDouble(items[4]);
texture_wid = Integer.parseInt(items[5]);
texture_hgt = Integer.parseInt(items[6]);
int pos = 7;
while (pos < items.length) {
int left = items.length - pos;
int slideid = Integer.parseInt(items[pos++]);
switch(slideid) {
case SLIDEBase_ID:
if (left >= SLIDEBase_size) { // millis
slide.add(new SLIDEBase(Integer.parseInt(items[pos++])));
} else {
pos = items.length; // skip rest as error occurred in parsing
}
break;
case SLIDEImage_ID:
if (left >= SLIDEImage_size) { // millis, category, filename
slide.add(new SLIDEImage(Integer.parseInt(items[pos++]),
items[pos++],
items[pos++]));
} else {
pos = items.length;
}
break;
case SLIDEGif_ID:
if (left >= SLIDEGif_size) { // millis, category, filename, frame
slide.add(new SLIDEGif(Integer.parseInt(items[pos++]),
items[pos++],
items[pos++],
Integer.parseInt(items[pos++])));
} else {
pos = items.length;
}
break;
case SLIDEVideo_ID:
if (left >= SLIDEVideo_size) { // millis, category, filename, volume
slide.add(new SLIDEVideo(Integer.parseInt(items[pos++]),
items[pos++],
items[pos++],
Integer.parseInt(items[pos++])));
} else {
pos = items.length;
}
break;
}
}
}
// SLIDESHOW related classes
public static final int SLIDEBase_ID = 0;
public static final int SLIDEImage_ID = 1;
public static final int SLIDEGif_ID = 2;
public static final int SLIDEVideo_ID = 3;
public static final int SLIDEBase_size = 2;
public static final int SLIDEImage_size = 4;
public static final int SLIDEGif_size = 5;
public static final int SLIDEVideo_size = 5;
public static class SLIDEBase implements Serializable {
private static final long serialVersionUID = 3680086286602896502L;
public long millis = 1000;
public SLIDEBase() {};
public SLIDEBase(long m) { millis = m; }
public String getString() {
return "," + SLIDEBase_ID + "," + millis;
}
}
public static class SLIDEImage extends SLIDEBase implements Serializable {
private static final long serialVersionUID = -5054050601938346408L;
public String category = "";
public String filename = "";
public SLIDEImage() {}
public SLIDEImage(long mil, String cat, String file) {
millis = mil;
category = cat;
filename = file;
}
@Override
public String getString() {
return "," + SLIDEImage_ID + "," + millis + "," +
category + "," + filename;
}
}
public static class SLIDEGif extends SLIDEBase implements Serializable {
private static final long serialVersionUID = 7876907337095597526L;
public String category = "";
public String filename = "";
public long frame = 100;
public SLIDEGif() {}
public SLIDEGif(long millis, String cat, String file, long frame) {
this.millis = millis;
category = cat;
filename = file;
this.frame = frame;
}
@Override
public String getString() {
return "," + SLIDEGif_ID + "," + millis + "," +
category + "," + filename + "," + frame;
}
}
public static class SLIDEVideo extends SLIDEBase implements Serializable {
private static final long serialVersionUID = 2285077516372282725L;
public String category = "";
public String filename = "";
public int volume = 50;
public SLIDEVideo() {}
public SLIDEVideo(long millis, String cat, String file, int vol) {
this.millis = millis;
category = cat;
filename = file;
volume = vol;
}
@Override
public String getString() {
return "," + SLIDEVideo_ID + "," + millis + "," +
category + "," + filename + "," + volume;
}
}
}
public static class ModuleSIMPLETIME extends ModuleBase implements Serializable {
private static final long serialVersionUID = 1717777611339545587L;
public double x1 = 0, y1 = 0;
public double x2 = 0, y2 = 0;
public int texture_wid = 512, texture_hgt = 64;
public int format_day = 0; // none, sat or saturday
public int format_date = 0; // none, 01/01/01 or 1st jan 2016
public int format_time = 1; // none, 5:03pm, 17:03, 5:03:01pm, 17:03:01
public int font_size = 40;
public int font_align = 0; // left, center, right justify
public String font_name = "roboto";
public int R = 0, G = 0, B = 0, A = 0;
public ModuleSIMPLETIME() {
super(ModSIMPLETIME);
}
public void setCoords(double xpos1, double ypos1, double xpos2, double ypos2) {
x1 = xpos1; y1 = ypos1;
x2 = xpos2; y2 = ypos2;
}
public void setTextureSize(int width, int height) {
texture_wid = width;
texture_hgt = height;
}
public void setColor(int r, int g, int b, int a) {
R = r; G = g; B = b; A = a;
}
public void setFormat(int day, int date, int time) {
format_day = day;
format_date = date;
format_time = time;
}
public void setFont(String name, int size, int align) {
font_name = name;
font_size = size;
font_align = align;
}
@Override
public String getString() {
return ModSIMPLETIME + ":" + x1 + "," + y1 + "," + x2 + "," + y2 + "," +
texture_wid + "," + texture_hgt + "," +
format_day + "," + format_date + "," + format_time + "," +
font_size + "," + font_align + "," + font_name + "," +
R + "," + G + "," + B + "," + A + ";";
}
@Override
public void fromString(String mod) {
String items[] = splitModuleString(mod);
if (items.length != ModSIMPLETIME_size) return;
x1 = Double.parseDouble(items[1]);
y1 = Double.parseDouble(items[2]);
x2 = Double.parseDouble(items[3]);
y2 = Double.parseDouble(items[4]);
texture_wid = Integer.parseInt(items[5]);
texture_hgt = Integer.parseInt(items[6]);
format_day = Integer.parseInt(items[7]);
format_date = Integer.parseInt(items[8]);
format_time = Integer.parseInt(items[9]);
font_size = Integer.parseInt(items[10]);
font_align = Integer.parseInt(items[11]);
font_name = items[12];
R = Integer.parseInt(items[13]);
G = Integer.parseInt(items[14]);
B = Integer.parseInt(items[15]);
A = Integer.parseInt(items[16]);
}
}
public static class ModuleSIMPLETICK extends ModuleBase implements Serializable {
private static final long serialVersionUID = 2171555564226477834L;
public double x1 = 0, y1 = 0;
public double x2 = 0, y2 = 0;
public int texture_wid = 512, texture_hgt = 64;
public String list_name = "";
public String font_name = "roboto";
public int font_size = 48;
public int speed = 150;
public int R = 0, G = 0, B = 0, A = 0;
public ModuleSIMPLETICK() {
super(ModSIMPLETICK);
}
public void setCoords(double xpos1, double ypos1, double xpos2, double ypos2) {
x1 = xpos1; y1 = ypos1;
x2 = xpos2; y2 = ypos2;
}
public void setTextureSize(int width, int height) {
texture_wid = width;
texture_hgt = height;
}
public void setColor(int r, int g, int b, int a) {
R = r; G = g; B = b; A = a;
}
public void setTicker(String list, String font, int size, int speed) {
list_name = list;
font_name = font;
font_size = size;
this.speed = speed;
}
@Override
public String getString() {
return ModSIMPLETICK + ":" + x1 + "," + y1 + "," + x2 + "," + y2 + "," +
texture_wid + "," + texture_hgt + "," +
list_name + "," + font_name + "," + font_size + "," + speed + "," +
R + "," + G + "," + B + "," + A + ";";
}
@Override
public void fromString(String mod) {
String[] items = splitModuleString(mod);
if (items.length != ModSIMPLETICK_size) return;
x1 = Double.parseDouble(items[1]);
y1 = Double.parseDouble(items[2]);
x2 = Double.parseDouble(items[3]);
y2 = Double.parseDouble(items[4]);
texture_wid = Integer.parseInt(items[5]);
texture_hgt = Integer.parseInt(items[6]);
list_name = items[7];
font_name = items[8];
font_size = Integer.parseInt(items[9]);
speed = Integer.parseInt(items[10]);
R = Integer.parseInt(items[11]);
G = Integer.parseInt(items[12]);
B = Integer.parseInt(items[13]);
A = Integer.parseInt(items[14]);
}
}
// static vars pertaining to the modules strings and default values
public static String[] ModuleStrings = {
/* 0, 1, 2 */		"BG",			"Background Colour",	"BG:0,0,0;",
/* 3, 4, 5 */		"RES",			"Screen Resolution",	"RES:1024,768;",
/* 6, 7, 8 */		"ORIENT",		"Orientation",			"ORIENT:land;",
/* 9, 10, 11 */		"COLORBAR",		"Colour Bar",			"COLORBAR:0.0,0.0,0.5,0.3,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255;",
/* 12, 13, 14 */	"STATICIMG",	"Static Image",			"STATICIMG:0.0,0.0,0.5,0.3,na,na,128,128,255,255,255,255;",
/* 15, 16, 17 */	"SLIDESHOW",	"Slide Show",			"SLIDESHOW:0.0,0.0,0.5,0.3,128,128;",
/* 18, 19, 20 */	"SIMPLETIME",	"Simple Time/Date",		"SIMPLETIME:0.0,0.0,0.5,0.1,512,64,0,0,1,40,0,roboto,255,255,255,255;",
/* 21, 22, 23 */	"SIMPLETICK",	"Simple Ticker",		"SIMPLETICK:0.0,0.0,0.5,0.1,512,64,none,roboto,48,100,255,255,255,255;"
};
}
Scroll to top