Removed use of java.awt.Color from RocketComponent, OpenRocketLoader. The class net.sf.openrocket.util.Color is used instead in these components, and the conversion to/from java.awt.Color is done in the gui code. This is necessary to port RocketComponent to Android platform.

This commit is contained in:
Kevin Ruland 2011-12-17 04:45:16 +00:00
parent 649ec84cf6
commit 832e610675
10 changed files with 56 additions and 45 deletions

View File

@ -1,6 +1,5 @@
package net.sf.openrocket.file.openrocket;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
@ -73,6 +72,7 @@ import net.sf.openrocket.simulation.SimulationOptions;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.BugException;
import net.sf.openrocket.util.Color;
import net.sf.openrocket.util.Coordinate;
import net.sf.openrocket.util.GeodeticComputationStrategy;
import net.sf.openrocket.util.LineStyle;

View File

@ -1,6 +1,5 @@
package net.sf.openrocket.file.openrocket.savers;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -15,6 +14,7 @@ import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.util.BugException;
import net.sf.openrocket.util.Color;
import net.sf.openrocket.util.LineStyle;

View File

@ -35,6 +35,7 @@ import net.sf.openrocket.gui.components.ColorIcon;
import net.sf.openrocket.gui.components.StyledLabel;
import net.sf.openrocket.gui.components.StyledLabel.Style;
import net.sf.openrocket.gui.components.UnitSelector;
import net.sf.openrocket.gui.util.ColorConversion;
import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.l10n.Translator;
@ -377,13 +378,15 @@ public class RocketComponentConfig extends JPanel {
colorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color c = component.getColor();
net.sf.openrocket.util.Color c = component.getColor();
if (c == null) {
c = ((SwingPreferences) Application.getPreferences()).getDefaultColor(component.getClass());
c = Application.getPreferences().getDefaultColor(component.getClass());
}
//// Choose color
c = JColorChooser.showDialog(tabbedPane, trans.get("RocketCompCfg.lbl.Choosecolor"), c);
Color awtColor = ColorConversion.toAwtColor(c);
awtColor = JColorChooser.showDialog(tabbedPane, trans.get("RocketCompCfg.lbl.Choosecolor"), awtColor);
c = ColorConversion.fromAwtColor(awtColor);
if (c != null) {
component.setColor(c);
}
@ -439,11 +442,11 @@ public class RocketComponentConfig extends JPanel {
private Color getColor() {
Color c = component.getColor();
net.sf.openrocket.util.Color c = component.getColor();
if (c == null) {
c = ((SwingPreferences) Application.getPreferences()).getDefaultColor(component.getClass());
c = Application.getPreferences().getDefaultColor(component.getClass());
}
return c;
return ColorConversion.toAwtColor(c);
}

View File

@ -21,6 +21,7 @@ import java.util.LinkedHashSet;
import net.sf.openrocket.gui.figureelements.FigureElement;
import net.sf.openrocket.gui.main.ExceptionHandler;
import net.sf.openrocket.gui.util.ColorConversion;
import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.motor.Motor;
import net.sf.openrocket.rocketcomponent.Configuration;
@ -310,11 +311,11 @@ public class RocketFigure extends AbstractScaleFigure {
}
// Set component color and line style
Color color = c.getColor();
net.sf.openrocket.util.Color color = c.getColor();
if (color == null) {
color = ((SwingPreferences) Application.getPreferences()).getDefaultColor(c.getClass());
color = Application.getPreferences().getDefaultColor(c.getClass());
}
g2.setColor(color);
g2.setColor(ColorConversion.toAwtColor(color));
LineStyle style = c.getLineStyle();
if (style == null)

View File

@ -89,17 +89,6 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences {
}
private static final HashMap<Class<?>, String> DEFAULT_COLORS =
new HashMap<Class<?>, String>();
static {
DEFAULT_COLORS.put(BodyComponent.class, "0,0,240");
DEFAULT_COLORS.put(FinSet.class, "0,0,200");
DEFAULT_COLORS.put(LaunchLug.class, "0,0,180");
DEFAULT_COLORS.put(InternalComponent.class, "170,0,100");
DEFAULT_COLORS.put(MassObject.class, "0,0,0");
DEFAULT_COLORS.put(RecoveryDevice.class, "255,0,0");
}
//////////////////////
@ -315,25 +304,6 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences {
return new Color(0, 0, 0, 100);
}
public Color getDefaultColor(Class<? extends RocketComponent> c) {
String color = get("componentColors", c, DEFAULT_COLORS);
if (color == null)
return Color.BLACK;
net.sf.openrocket.util.Color clr = parseColor(color);
if (clr != null) {
return ColorConversion.toAwtColor(clr);
} else {
return Color.BLACK;
}
}
public final void setDefaultColor(Class<? extends RocketComponent> c, Color color) {
if (color == null)
return;
putString("componentColors", c.getSimpleName(), stringifyColor(ColorConversion.fromAwtColor(color)));
}
public static int getMaxThreadCount() {

View File

@ -1,6 +1,5 @@
package net.sf.openrocket.rocketcomponent;
import java.awt.Color;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
@ -16,6 +15,7 @@ import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.BugException;
import net.sf.openrocket.util.ChangeSource;
import net.sf.openrocket.util.Color;
import net.sf.openrocket.util.Coordinate;
import net.sf.openrocket.util.Invalidator;
import net.sf.openrocket.util.LineStyle;

View File

@ -6,7 +6,12 @@ import java.util.Map;
import net.sf.openrocket.database.Databases;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.material.Material;
import net.sf.openrocket.rocketcomponent.BodyComponent;
import net.sf.openrocket.rocketcomponent.FinSet;
import net.sf.openrocket.rocketcomponent.InternalComponent;
import net.sf.openrocket.rocketcomponent.LaunchLug;
import net.sf.openrocket.rocketcomponent.MassObject;
import net.sf.openrocket.rocketcomponent.RecoveryDevice;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.util.BugException;
import net.sf.openrocket.util.BuildProperties;
@ -168,6 +173,26 @@ public abstract class Preferences {
}
}
public Color getDefaultColor(Class<? extends RocketComponent> c) {
String color = get("componentColors", c, DEFAULT_COLORS);
if (color == null)
return Color.BLACK;
Color clr = parseColor(color);
if (clr != null) {
return clr;
} else {
return Color.BLACK;
}
}
public final void setDefaultColor(Class<? extends RocketComponent> c, Color color) {
if (color == null)
return;
putString("componentColors", c.getSimpleName(), stringifyColor(color));
}
/**
* Retrieve a Line style for the given component.
* @param c
@ -361,6 +386,17 @@ public abstract class Preferences {
Databases.findMaterial(Material.Type.BULK, trans.get("Databases.materials.Cardboard"), 680, false);
}
private static final HashMap<Class<?>, String> DEFAULT_COLORS =
new HashMap<Class<?>, String>();
static {
DEFAULT_COLORS.put(BodyComponent.class, "0,0,240");
DEFAULT_COLORS.put(FinSet.class, "0,0,200");
DEFAULT_COLORS.put(LaunchLug.class, "0,0,180");
DEFAULT_COLORS.put(InternalComponent.class, "170,0,100");
DEFAULT_COLORS.put(MassObject.class, "0,0,0");
DEFAULT_COLORS.put(RecoveryDevice.class, "255,0,0");
}
}

View File

@ -1,6 +1,5 @@
package net.sf.openrocket.util;
import java.awt.Color;
import java.util.Random;
import net.sf.openrocket.material.Material;

View File

@ -5,6 +5,7 @@ import static org.junit.Assert.*;
import java.awt.Color;
import java.util.Iterator;
import net.sf.openrocket.gui.util.ColorConversion;
import net.sf.openrocket.util.Coordinate;
import org.junit.Test;
@ -37,7 +38,7 @@ public class ComponentCompareTest {
ComponentCompare.assertDeepSimilarity(r1, r2, false);
r1.setColor(Color.YELLOW);
r1.setColor(ColorConversion.fromAwtColor(Color.YELLOW));
try {
ComponentCompare.assertEquality(r1, r2);
fail();

View File

@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue;
import java.awt.Color;
import net.sf.openrocket.gui.util.ColorConversion;
import net.sf.openrocket.material.Material;
import net.sf.openrocket.material.Material.Type;
import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
@ -35,7 +36,7 @@ public class FinSetTest extends BaseTestCase {
fin.setBaseRotation(1.1);
fin.setCantAngle(0.001);
fin.setCGOverridden(true);
fin.setColor(Color.YELLOW);
fin.setColor(ColorConversion.fromAwtColor(Color.YELLOW));
fin.setComment("cmt");
fin.setCrossSection(CrossSection.ROUNDED);
fin.setFinCount(5);