added transparency

This commit is contained in:
bancika
2011-01-09 00:08:13 +00:00
parent 300753bb85
commit d43e098216
15 changed files with 344 additions and 56 deletions
@@ -3,6 +3,10 @@
<string>defaults</string>
<map/>
</entry>
<entry>
<string>lastPath</string>
<string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string>
</entry>
<entry>
<string>metric</string>
<boolean>true</boolean>
@@ -1,7 +1,9 @@
package org.diylc.components;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
@@ -9,7 +11,6 @@ import java.awt.Rectangle;
import java.awt.Shape;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
@@ -23,7 +24,7 @@ import org.diylc.utils.Constants;
*
* @author Branislav Stojkovic
*/
public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T> {
public abstract class AbstractLeadedDIYComponent<T> extends AbstractTransparentComponent<T> {
private static final long serialVersionUID = 1L;
@@ -35,7 +36,6 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
protected Size height;
protected Point[] points = new Point[] { new Point((int) (-Constants.GRID * 5), 0),
new Point((int) (Constants.GRID * 5), 0) };
protected String name = "New Component";
protected AbstractLeadedDIYComponent() {
super();
@@ -131,8 +131,14 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
g2d.rotate(theta, shapeRect.width / 2, shapeRect.height / 2);
// Draw body.
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1f * alpha / MAX_ALPHA));
}
g2d.setColor(getBodyColor());
g2d.fill(shape);
g2d.setComposite(oldComposite);
}
g2d.setStroke(new BasicStroke(1));
g2d.setColor(getBorderColor());
@@ -148,15 +154,6 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
(int) (shapeRect.height - textRect.getHeight()) / 2 + fontMetrics.getAscent());
}
@EditableProperty(defaultable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@EditableProperty(defaultable = true)
public Size getWidth() {
return width;
@@ -174,30 +171,4 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
public void setHeight(Size height) {
this.height = height;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractLeadedDIYComponent other = (AbstractLeadedDIYComponent) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
@@ -0,0 +1,32 @@
package org.diylc.components;
import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.EditableProperty;
public abstract class AbstractTransparentComponent<T> implements IDIYComponent<T> {
private static final long serialVersionUID = 1L;
public static byte MAX_ALPHA = 127;
protected String name = "New Component";
protected byte alpha = MAX_ALPHA;
@EditableProperty(defaultable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@EditableProperty
public Byte getAlpha() {
return alpha;
}
public void setAlpha(Byte alpha) {
this.alpha = alpha;
}
}
@@ -31,7 +31,6 @@ import org.diylc.plugins.statusbar.StatusBar;
import org.diylc.plugins.toolbox.ToolBox;
import org.diylc.presenter.Presenter;
public class MainFrame extends JFrame implements IView {
private static final long serialVersionUID = 1L;
@@ -64,7 +63,7 @@ public class MainFrame extends JFrame implements IView {
presenter.installPlugin(new ToolBox());
presenter.installPlugin(new FileManager());
presenter.installPlugin(new ClipboardManager());
// presenter.installPlugin(new OnlineManager());
// presenter.installPlugin(new OnlineManager());
presenter.installPlugin(new HelpManager());
presenter.installPlugin(new StatusBar());
@@ -97,7 +96,7 @@ public class MainFrame extends JFrame implements IView {
super.setVisible(b);
// TODO: hack to prevent painting issues in the scroll bar rulers. Find
// a better fix if possible.
Timer timer = new Timer(200, new ActionListener() {
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@@ -0,0 +1,26 @@
package org.diylc.gui.editor;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.diylc.common.PropertyWrapper;
public class ByteEditor extends JSlider {
private static final long serialVersionUID = 1L;
public ByteEditor(final PropertyWrapper property) {
super();
setMinimum(0);
setMaximum(127);
setValue((Byte) property.getValue());
addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
property.setValue(new Integer(getValue()).byteValue());
}
});
}
}
@@ -25,6 +25,11 @@ public class FieldEditorFactory {
EnumEditor editor = new EnumEditor(property);
return editor;
}
if (Byte.class.isAssignableFrom(property.getType())
|| byte.class.isAssignableFrom(property.getType())) {
ByteEditor byteEditor = new ByteEditor(property);
return byteEditor;
}
return null;
}
}
@@ -1,6 +1,13 @@
package org.diylc.plugins.toolbox;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -8,19 +15,25 @@ import java.util.Map;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ScrollPaneConstants;
import org.diylc.common.ComponentType;
import org.diylc.common.IPlugInPort;
import org.diylc.presenter.ComparatorFactory;
import org.diylc.presenter.Presenter;
/**
* Tabbed pane that shows all available components categorized into
* tabs.
* Tabbed pane that shows all available components categorized into tabs.
*
* @author Branislav Stojkovic
*/
class ComponentTabbedPane extends JTabbedPane {
public static int SCROLL_STEP = Presenter.ICON_SIZE + ComponentButton.MARGIN * 2 + 2;
private static final long serialVersionUID = 1L;
private final IPlugInPort plugInPort;
@@ -31,17 +44,68 @@ class ComponentTabbedPane extends JTabbedPane {
List<String> categories = new ArrayList<String>(componentTypes.keySet());
Collections.sort(categories);
for (String category : categories) {
addTab(category, createComponentPanel(componentTypes.get(category)));
JPanel panel = createTab((componentTypes.get(category)));
addTab(category, panel);
}
setPreferredSize(new Dimension(-1, Presenter.ICON_SIZE + ComponentButton.MARGIN * 2 + 38));
}
private Container createComponentPanel(List<ComponentType> componentTypes) {
Container componentPanel = new Container();
componentPanel.setLayout(new BoxLayout(componentPanel, BoxLayout.X_AXIS));
private JPanel createTab(List<ComponentType> componentTypes) {
JPanel panel = new JPanel(new BorderLayout());
final JScrollPane scrollPane = createComponentScrollBar(componentTypes);
panel.setOpaque(false);
JButton leftButton = new JButton("<");
leftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle rect = scrollPane.getVisibleRect();
if (rect.x > SCROLL_STEP) {
rect.translate(-SCROLL_STEP, 0);
} else {
rect.translate(-rect.x, 0);
}
}
});
leftButton.setMargin(new Insets(0, 2, 0, 2));
JButton rightButton = new JButton(">");
rightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Rectangle rect = scrollPane.getVisibleRect();
if (rect.x + rect.width < scrollPane.getViewport().getSize().width - SCROLL_STEP) {
rect.translate(SCROLL_STEP, 0);
} else {
rect.translate(scrollPane.getViewport().getSize().width - rect.x - rect.width,
0);
}
}
});
rightButton.setMargin(new Insets(0, 2, 0, 2));
panel.add(leftButton, BorderLayout.WEST);
panel.add(scrollPane);
panel.add(rightButton, BorderLayout.EAST);
return panel;
}
private JScrollPane createComponentScrollBar(List<ComponentType> componentTypes) {
JScrollPane scrollPane = new JScrollPane(createComponentPanel(componentTypes));
scrollPane.setOpaque(false);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
return scrollPane;
}
private Component createComponentPanel(List<ComponentType> componentTypes) {
Container toolbar = new Container();
Collections.sort(componentTypes, ComparatorFactory.getInstance()
.getComponentTypeComparator());
toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
for (ComponentType componentType : componentTypes) {
JButton button = new ComponentButton(plugInPort, componentType);
componentPanel.add(button);
toolbar.add(button);
}
return componentPanel;
return toolbar;
}
}
@@ -3,6 +3,7 @@ package org.diylc.presenter;
import java.util.Comparator;
import java.util.List;
import org.diylc.common.ComponentType;
import org.diylc.core.IDIYComponent;
public class ComparatorFactory {
@@ -10,6 +11,7 @@ public class ComparatorFactory {
private static ComparatorFactory instance;
private Comparator<IDIYComponent<?>> componentNameComparator;
private Comparator<ComponentType> componentTypeComparator;
public static ComparatorFactory getInstance() {
if (instance == null) {
@@ -36,6 +38,19 @@ public class ComparatorFactory {
return componentNameComparator;
}
public Comparator<ComponentType> getComponentTypeComparator() {
if (componentTypeComparator == null) {
componentTypeComparator = new Comparator<ComponentType>() {
@Override
public int compare(ComponentType o1, ComponentType o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
};
}
return componentTypeComparator;
}
public Comparator<IDIYComponent<?>> getComponentZOrderComparator(
final List<IDIYComponent<?>> components) {
Comparator<IDIYComponent<?>> componentZOrderComparator = new Comparator<IDIYComponent<?>>() {
@@ -1,17 +1,19 @@
package org.diylc.components.boards;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.utils.Constants;
public abstract class AbstractBoard implements IDIYComponent<String> {
public abstract class AbstractBoard extends AbstractTransparentComponent<String> {
private static final long serialVersionUID = 1L;
@@ -29,9 +31,15 @@ public abstract class AbstractBoard implements IDIYComponent<String> {
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
g2d.setStroke(new BasicStroke());
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha
/ MAX_ALPHA));
}
g2d.setColor(getBoardColor());
g2d.fillRect(controlPoints[0].x, controlPoints[0].y, controlPoints[1].x
- controlPoints[0].x, controlPoints[1].y - controlPoints[0].y);
g2d.setComposite(oldComposite);
}
g2d.setColor(getBorderColor());
g2d.drawRect(controlPoints[0].x, controlPoints[0].y, controlPoints[1].x
@@ -1,7 +1,9 @@
package org.diylc.components.boards;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
@@ -43,6 +45,11 @@ public class PerfBoard extends AbstractBoard {
super.draw(g2d, componentState, project);
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha
/ MAX_ALPHA));
}
Point p = new Point(controlPoints[0]);
int radius = PAD_SIZE.convertToPixels() / 2;
int holeRadius = HOLE_SIZE.convertToPixels() / 2;
@@ -61,6 +68,7 @@ public class PerfBoard extends AbstractBoard {
holeRadius * 2);
}
}
g2d.setComposite(oldComposite);
}
// System.err.println("OldDraw: " + (System.nanoTime() - t));
}
@@ -0,0 +1,140 @@
package org.diylc.components.boards;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import org.diylc.core.ComponentLayer;
import org.diylc.core.ComponentState;
import org.diylc.core.Project;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
@ComponentDescriptor(name = "Vero Board", category = "Boards", author = "Branislav Stojkovic", componentLayer = ComponentLayer.BOARD, instanceNamePrefix = "B", desciption = "Perforated FR4 board with solder pads spaced 0.1 inch apart")
public class VeroBoard extends AbstractBoard {
private static final long serialVersionUID = 1L;
public static Color BOARD_COLOR = Color.decode("#BCED91");
public static Color COPPER_COLOR = Color.decode("#B87333");
public static Color BORDER_COLOR = BOARD_COLOR.darker();
private static Size SPACING = new Size(0.1d, SizeUnit.in);
private static Size STRIP_SIZE = new Size(0.085d, SizeUnit.in);
private static Size HOLE_SIZE = new Size(0.8d, SizeUnit.mm);
// private Area copperArea;
@Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
// long t = System.nanoTime();
// Area copperArea = getCopperArea();
// System.err.println("Calc: " + (System.nanoTime() - t));
// t = System.nanoTime();
// super.draw(g2d, componentState, project);
// g2d.setColor(COPPER_COLOR);
// g2d.fill(copperArea);
// System.err.println(" Draw: " + (System.nanoTime() - t));
// long t = System.nanoTime();
super.draw(g2d, componentState, project);
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha
/ MAX_ALPHA));
}
Point p = new Point(controlPoints[0]);
int stripSize = STRIP_SIZE.convertToPixels();
int holeRadius = HOLE_SIZE.convertToPixels() / 2;
while (p.y < controlPoints[1].y - SPACING.convertToPixels()) {
p.x = controlPoints[0].x;
p.y += SPACING.convertToPixels();
g2d.setColor(COPPER_COLOR);
g2d.fillRect(p.x + SPACING.convertToPixels() / 2, p.y - stripSize / 2,
controlPoints[1].x - SPACING.convertToPixels() - p.x, stripSize);
while (p.x < controlPoints[1].x - SPACING.convertToPixels()) {
p.x += SPACING.convertToPixels();
g2d.setColor(Constants.CANVAS_COLOR);
g2d
.fillOval(p.x - holeRadius, p.y - holeRadius, holeRadius * 2,
holeRadius * 2);
}
}
g2d.setComposite(oldComposite);
}
// System.err.println("OldDraw: " + (System.nanoTime() - t));
}
// @Override
// public void setControlPoint(Point point, int index) {
// // Reset copper area if a control point is changed.
// if (!point.equals(getControlPoint(index))) {
// copperArea = null;
// }
// super.setControlPoint(point, index);
// }
//
// public Area getCopperArea() {
// if (copperArea == null) {
// copperArea = new Area();
//
// int holeRadius = HOLE_SIZE.convertToPixels() / 2;
// int radius = PAD_SIZE.convertToPixels() / 2;
// int spacing = SPACING.convertToPixels();
//
// // Create a pad.
// Area pad = new Area(new Ellipse2D.Double(-radius, -radius, radius * 2,
// radius * 2));
// pad.subtract(new Area(new Ellipse2D.Double(-holeRadius, -holeRadius,
// holeRadius * 2,
// holeRadius * 2)));
//
// // Create a row duplicating the pad.
// Area row = new Area();
// int columnCount = (controlPoints[1].x - controlPoints[0].x) / spacing -
// 1;
// int rowCount = (controlPoints[1].y - controlPoints[0].y) / spacing - 1;
//
// AffineTransform xTransform =
// AffineTransform.getTranslateInstance(spacing, 0);
// for (int i = 0; i < columnCount; i++) {
// pad.transform(xTransform);
// row.add(pad);
// }
//
// // Create the whole matrix duplicating the row.
// AffineTransform yTransform = AffineTransform.getTranslateInstance(0,
// spacing);
// for (int i = 0; i < rowCount; i++) {
// row.transform(yTransform);
// copperArea.add(row);
// }
//
// copperArea.transform(AffineTransform.getTranslateInstance(controlPoints[0].x,
// controlPoints[0].y));
// }
// return copperArea;
// }
@Override
protected Color getBoardColor() {
return BOARD_COLOR;
}
@Override
protected Color getBorderColor() {
return BORDER_COLOR;
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
}
}
@@ -1,14 +1,16 @@
package org.diylc.components.electromechanical;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.RoundRectangle2D;
import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentLayer;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
@@ -16,7 +18,7 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
@ComponentDescriptor(name = "Mini Toggle Switch", category = "Electromechanical", author = "Branislav Stojkovic", desciption = "", stretchable = false, componentLayer = ComponentLayer.COMPONENT, instanceNamePrefix = "SW")
public class MiniToggleSwitch implements IDIYComponent<ToggleSwitchType> {
public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchType> {
private static final long serialVersionUID = 1L;
@@ -163,8 +165,14 @@ public class MiniToggleSwitch implements IDIYComponent<ToggleSwitchType> {
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
if (body != null) {
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1f * alpha / MAX_ALPHA));
}
g2d.setColor(BODY_COLOR);
g2d.fill(body);
g2d.setComposite(oldComposite);
}
g2d.setStroke(new BasicStroke());
g2d.setColor(BORDER_COLOR);
@@ -1,15 +1,17 @@
package org.diylc.components.semiconductors;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import org.diylc.common.Orientation;
import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
@@ -18,7 +20,7 @@ import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
@ComponentDescriptor(name = "DIL IC", author = "Branislav Stojkovic", category = "Semiconductors", instanceNamePrefix = "IC", desciption = "test", stretchable = false)
public class DIL_IC implements IDIYComponent<String> {
public class DIL_IC extends AbstractTransparentComponent<String> {
private static final long serialVersionUID = 1L;
@@ -166,8 +168,14 @@ public class DIL_IC implements IDIYComponent<String> {
y -= height - Constants.GRID;
}
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1f * alpha / MAX_ALPHA));
}
g2d.setColor(BODY_COLOR);
g2d.fillRoundRect(x, y, width, height, EDGE_RADIUS, EDGE_RADIUS);
g2d.setComposite(oldComposite);
}
g2d.setColor(BORDER_COLOR);
g2d.setStroke(new BasicStroke());