Skip to content

Commit

Permalink
Add support for ANSI/US symbol standard
Browse files Browse the repository at this point in the history
  • Loading branch information
dikkadev authored and kristian committed Jun 17, 2022
1 parent cc53ce1 commit dd6e0b2
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 117 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.0 (2022-06-18)
### Added
- US / ANSI style gate symbols and option to switch in the menu, thanks to Sett17

## 2.1.0 (2022-01-25)
### Fixed
- Bumped launch4j to version 2.1.0
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>lc.kra.jds</groupId>
<artifactId>jds</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>

<name>JDigitalSimulator</name>
<url>http://kra.lc/projects/jdigitalsimulator/</url>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/lc/kra/jds/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ private void paintComponents(Graphics graphics, Collection<Component> components
wire.paint(graphics);
}
private List<Wire> paintComponent(Graphics graphics, Component component) {
component.checkSymbolStandard();
Point location = component.getLocation();
Dimension size = component.getSize();
graphics.setColor(Color.BLACK);
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/lc/kra/jds/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
package lc.kra.jds;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.JFileChooser;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
Expand Down Expand Up @@ -45,19 +50,14 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.JFileChooser;

public final class Utilities {
public static enum TranslationType { TEXT, ALTERNATIVE, TITLE, EXTERNAL, TOOLTIP, DESCRIPTION; }

public static final String
CONFIGURATION_LOCALIZATION_LANGUAGE = "localization.language",
CONFIGURATION_LOOK_AND_FEEL_CLASS = "lookandfeel.class",
CONFIGURATION_LOOK_AND_FEEL_NAME = "lookandfeel.name",
CONFIGURATION_ANSI_SYMBOLS = "symbols.ansi",
CONFIGURATION_WINDOW_SIZE = "window.size",
CONFIGURATION_WINDOW_LOCATION = "window.location",
CONFIGURATION_WINDOW_MAXIMIZED = "window.maximized";
Expand All @@ -66,9 +66,11 @@ public static enum TranslationType { TEXT, ALTERNATIVE, TITLE, EXTERNAL, TOOLTIP
private static Locale currentLocale = Locale.getDefault();
private static ResourceBundle translationBundle;
private static SimpleClassLoader simpleClassLoader;

private static Properties configuration = new Properties();

private static boolean useAnsiSymbols = false;

public static Object copy(Object object) throws CloneNotSupportedException { //deep clone using serilization
Object copy = null;
FastByteArrayOutputStream byteOutput = new FastByteArrayOutputStream();
Expand Down Expand Up @@ -135,6 +137,13 @@ public static String getTranslation(String key, TranslationType type, Object...
else return MessageFormat.format(bundle.getString(key), variables);
}

public static boolean useAnsiSymbols(){
return useAnsiSymbols;
}
public static void setUseAnsiSymbols(boolean value){
useAnsiSymbols = value;
}

public static URL getResource(String name) {
if(!name.startsWith("/"))
name = "/lc/kra/jds/"+name;
Expand Down Expand Up @@ -420,7 +429,7 @@ private Class<?> loadClass(byte[] bytes) {
return cls;
}
}

public static class AlternateClassLoaderObjectInputStream extends ObjectInputStream {
protected final ClassLoader alternateClassLoader;
public AlternateClassLoaderObjectInputStream(InputStream in, ClassLoader alternateClassLoader) throws IOException {
Expand All @@ -445,7 +454,7 @@ public static String replaceLegacyPackage(String name) {
return name.replaceFirst("^(\\[L)?"+Pattern.quote(LEGACY_PACKAGE_PREFIX), "$1"+Matcher.quoteReplacement(PACKAGE_PREFIX));
}
}

public static URL getLocalPath() {
URL path = Utilities.class.getProtectionDomain().getCodeSource().getLocation();
try { return new URL(URLDecoder.decode(path.toString(), "UTF-8")); }
Expand Down Expand Up @@ -487,4 +496,4 @@ private static File getLastDirectory() {
setConfiguration("directory", selected.getParentFile().toString());
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/lc/kra/jds/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ public abstract class Component implements Paintable, Locatable, Moveable, Clone

private Point location;
protected PropertyChangeSupport change;
protected Boolean useAnsiSymbols;

public Component() { this.location = new Point(); }
public Component(Point location) { this.setLocation(location); }

public final void checkSymbolStandard() {
Boolean currentUseAnsiSymbols = Boolean.valueOf(useAnsiSymbols());
if (!currentUseAnsiSymbols.equals(useAnsiSymbols)) {
useAnsiSymbols = currentUseAnsiSymbols;
changeSymbolStandard();
}
}
protected void changeSymbolStandard() {}

public abstract Dimension getSize();
@Override public void moveTo(Point location) { this.setLocation(location); }
@Override public void moveRelative(Point location) {
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/lc/kra/jds/components/buildin/gate/AbstractGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
*/
package lc.kra.jds.components.buildin.gate;

import java.awt.Graphics;
import java.awt.Point;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;

import lc.kra.jds.Utilities;
import lc.kra.jds.components.Configurable;
import lc.kra.jds.components.Configurable.Option.OptionType;
Expand All @@ -32,6 +26,12 @@
import lc.kra.jds.contacts.InputContact;
import lc.kra.jds.contacts.OutputContact;

import java.awt.Graphics;
import java.awt.Point;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;

/**
* Abstract-Gate (build-in component)
* @author Kristian Kraljic ([email protected])
Expand All @@ -50,13 +50,30 @@ public AbstractGate() {
contacts = ContactUtilities.concatenateContacts(output, inputs.toArray());
}

@Override
protected void changeSymbolStandard() {
super.changeSymbolStandard();
if (inputs != null)
inputs.setContactLocations();
if (output != null)
output.setLocation(new Point(this.size.width, this.size.height / 2));
}

@Override public void paint(Graphics graphics) {
super.paint(graphics);
Class<? extends AbstractGate> cls = this.getClass();
if(cls.equals(NandGate.class)||cls.equals(NorGate.class)||cls.equals(XnorGate.class))
ContactUtilities.paintSolderingJoint(graphics, 5, 3, output);
ContactUtilities.paintSolderingJoint(graphics, 5, 3, output);
else ContactUtilities.paintSolderingJoint(graphics, 5, 10, output);
inputs.paintSolderingJoints(graphics, 5, 10);
if (useAnsiSymbols) {
if (cls.equals(OrGate.class)||cls.equals(NorGate.class)||cls.equals(XorGate.class)||cls.equals(XnorGate.class)) {
inputs.paintSolderingJoints(graphics, 9, 10);
} else {
inputs.paintSolderingJoints(graphics, 5, 10);
}
} else {
inputs.paintSolderingJoints(graphics, 5, 10);
}
}

@Override public Contact[] getContacts() { return contacts; }
Expand All @@ -75,4 +92,4 @@ public AbstractGate() {
configuration.put(getOptions()[0], inputs.getContactsCount());
return configuration;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public class AndGate extends AbstractGate {

@Override public void paint(Graphics graphics) {
super.paint(graphics);
paintLabel(graphics, "&");
if(useAnsiSymbols){
int transition = size.width - size.height / 2 - 11;
graphics.drawPolyline(new int[]{transition, 5, 5, transition}, new int[]{0, 0, size.height, size.height}, 4);
graphics.drawArc(transition - size.height / 2, 0, size.height, size.height, 90, -180);
} else {
paintLabel(graphics, "&");
}
}

@Override public void calculate() {
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/lc/kra/jds/components/buildin/gate/Gate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,47 @@
*/
package lc.kra.jds.components.buildin.gate;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import lc.kra.jds.components.Component;
import lc.kra.jds.components.Sociable;
import lc.kra.jds.contacts.Contact;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

/**
* Gate (build-in component)
* @author Kristian Kraljic ([email protected])
*/
public abstract class Gate extends Component implements Sociable {
private static final long serialVersionUID = 2l;

private static final Dimension ISO_SIZE = new Dimension(50, 48), ANSI_SIZE = new Dimension(55, 35);

protected Dimension size;

public Gate() { size = new Dimension(50, 48); }
public Gate() {
checkSymbolStandard();
}

protected void changeSymbolStandard() {
if (useAnsiSymbols) {
size = ANSI_SIZE;
} else {
size = ISO_SIZE;
}
}

@Override public void paint(Graphics graphics) {
graphics.setColor(Color.BLACK);
graphics.drawRect(5, 0, size.width-15, size.height);
if (!useAnsiSymbols) {
graphics.drawRect(5, 0, size.width-15, size.height);
}
}
protected void paintLabel(Graphics graphics, String label) { graphics.drawString(label, 5+(size.width-15)/2-graphics.getFontMetrics().stringWidth(label)/2, 15); }
protected void paintNot(Graphics graphics) { graphics.drawOval(size.width-10, size.height/2-3, 6, 6); }

@Override public abstract Contact[] getContacts();
@Override public final Dimension getSize() { return size; }
@Override public Dimension getSize() { return size; }
@Override public abstract void calculate();
}
}
32 changes: 25 additions & 7 deletions src/main/java/lc/kra/jds/components/buildin/gate/NotGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
*/
package lc.kra.jds.components.buildin.gate;

import static lc.kra.jds.Utilities.*;

import java.awt.Graphics;
import java.awt.Point;

import lc.kra.jds.Utilities.TranslationType;
import lc.kra.jds.contacts.Contact;
import lc.kra.jds.contacts.ContactUtilities;
import lc.kra.jds.contacts.InputContact;
import lc.kra.jds.contacts.OutputContact;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

import static lc.kra.jds.Utilities.getTranslation;

/**
* NOT-Gate (build-in component)
* @author Kristian Kraljic ([email protected])
Expand All @@ -38,6 +39,7 @@ public class NotGate extends Gate {
private static final String KEY;
static { KEY = "component.gate."+NotGate.class.getSimpleName().toLowerCase(); }
public static final ComponentAttributes componentAttributes = new ComponentAttributes(KEY, getTranslation(KEY), "group.gate", getTranslation(KEY, TranslationType.DESCRIPTION), "Kristian Kraljic ([email protected])", 1);
private static final Dimension ANSI_SIZE = new Dimension(45, 35);

private InputContact input;
private OutputContact output;
Expand All @@ -50,15 +52,31 @@ public NotGate() {
contacts = new Contact[]{input, output};
}

@Override
protected void changeSymbolStandard() {
super.changeSymbolStandard();
if (useAnsiSymbols)
size = ANSI_SIZE;
if (input != null)
input.setLocation(new Point(0, this.size.height / 2));
if (output != null)
output.setLocation(new Point(this.size.width, this.size.height / 2));
}

@Override public void paint(Graphics graphics) {
super.paint(graphics);
ContactUtilities.paintSolderingJoints(graphics, contacts);
paintLabel(graphics, "1");
if (useAnsiSymbols) {
graphics.drawPolyline(new int[]{5, size.width - 11, 5, 5}, new int[]{0, size.height / 2, size.height, 0}, 4);
} else {
paintLabel(graphics, "1");
}
paintNot(graphics);
}

@Override public Contact[] getContacts() { return contacts; }
@Override public Dimension getSize() { return size; }
@Override public void calculate() {
output.setCharged(!input.isCharged());
}
}
}
8 changes: 7 additions & 1 deletion src/main/java/lc/kra/jds/components/buildin/gate/OrGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public class OrGate extends AbstractGate {

@Override public void paint(Graphics graphics) {
super.paint(graphics);
paintLabel(graphics, "\u22651");
if(useAnsiSymbols){
graphics.drawArc(5 - (size.height / 3) / 2, 0, size.height / 3, size.height, 90, -180);
graphics.drawArc(5 - ((int) (size.width * 1.6)) / 2, 0, (int) (size.width * 1.6), (int) (size.height * 1.7), 90, -65);
graphics.drawArc(5 - ((int) (size.width * 1.6)) / 2, size.height - ((int) (size.height * 1.7)), (int) (size.width * 1.6), (int) (size.height * 1.7), -90, 65);
} else {
paintLabel(graphics, "\u22651");
}
}

@Override public void calculate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public class XorGate extends AbstractGate {

@Override public void paint(Graphics graphics) {
super.paint(graphics);
paintLabel(graphics, "=1");
if (useAnsiSymbols) {
graphics.drawArc(5 - (size.height / 3) / 2, 0, size.height / 3, size.height, 90, -180);
graphics.drawArc(1 - (size.height / 3) / 2, 0, size.height / 3, size.height, 90, -180);
graphics.drawArc(5 - ((int) (size.width * 1.6)) / 2, 0, (int) (size.width * 1.6), (int) (size.height * 1.7), 90, -65);
graphics.drawArc(5 - ((int) (size.width * 1.6)) / 2, size.height - ((int) (size.height * 1.7)), (int) (size.width * 1.6), (int) (size.height * 1.7), -90, 65);
} else {
paintLabel(graphics, "=1");
}
}

@Override public void calculate() {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/lc/kra/jds/contacts/ComponentContact.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.awt.Point;

import lc.kra.jds.components.Component;
import lc.kra.jds.components.Wire;
import lc.kra.jds.exceptions.LocationOutOfBoundsException;

public abstract class ComponentContact extends Contact implements Cloneable {
Expand All @@ -39,9 +40,14 @@ public ComponentContact(Component component, Point location) {
@Override public Component getComponent() { return component; }
@Override public Point getLocation() { return this.location; }
@Override public void setLocation(Point location) throws LocationOutOfBoundsException {
this.location = location;
if(location.x+location.y!=0)
validateLocation(); //store location but throw a exception
if (!location.equals(this.location)) {
this.location = location;
if(location.x+location.y!=0)
validateLocation(); //store location but throw a exception
for(Wire wire:getWires()) {
wire.revalidate();
}
}
}

@Override protected void validateLocation() throws LocationOutOfBoundsException {
Expand Down
Loading

0 comments on commit dd6e0b2

Please sign in to comment.