Michael Heuer wrote:
>> /piccolo2d.java/trunk/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java
>> /**
>> * The cutoff at which the Swing component is rendered greek
>> */
>> - private final double renderCutoff = 0.3;
>> + private static final double GREEK_SCALE_CUT_OFF = 0.3d;
>
> Similar values in PText are called greekThreshold and
> DEFAULT_GREEK_THRESHOLD. PText also has get/setGreekThreshold.
>
>> private JComponent component = null;
>> private double minFontSize = Double.MAX_VALUE;
>> - private Stroke defaultStroke = new BasicStroke();
>> + private transient Stroke defaultStroke = new BasicStroke();
>> private Font defaultFont = new Font("Serif", Font.PLAIN, 12);
>
> Should these defaults be static and final?
>
>
> Strange that PSwing overrides protected void paint(PPaintContext) with
> public void paint(PPaintContext). I recommend refactoring to
>
> public void paint(PPaintContext) --> protected void paint(PPaintContext)
> public void paintAsGreek(Graphics2D) --> protected void
> paintGreek(PPaintContext)
> public void paint(Graphics2D) --> protected void paintComponent(PPaintContext)
>
> Subclasses might want access to the entire paint context for
> paintGreek and paintComponent, not just the graphics. I wonder how
> much work this change would be though. . .
Hmm, not as bad as I thought:
Index: src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java
===================================================================
--- src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java (revision 626)
+++ src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java (working copy)
@@ -36,7 +36,6 @@
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Insets;
-import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ComponentAdapter;
@@ -323,41 +322,40 @@
* object should set those things if they are used, but they do not need to
* be restored.
*
- * @param renderContext Contains information about current render.
+ * @param paintContext Contains information about current render.
*/
- public void paint(final PPaintContext renderContext) {
- final Graphics2D g2 = renderContext.getGraphics();
+ protected void paint(final PPaintContext paintContext) {
+ final Graphics2D graphics = paintContext.getGraphics();
if (defaultStroke == null) {
defaultStroke = new BasicStroke();
}
- g2.setStroke(defaultStroke);
+ graphics.setStroke(defaultStroke);
if (defaultFont == null) {
defaultFont = new Font("Serif", Font.PLAIN, 12);
}
- g2.setFont(defaultFont);
+ graphics.setFont(defaultFont);
if (component.getParent() == null) {
- // pSwingCanvas.getSwingWrapper().add( component );
component.revalidate();
}
if (component instanceof JLabel) {
final JLabel label = (JLabel) component;
- enforceNoEllipsis(label.getText(), label.getIcon(),
label.getIconTextGap(), g2);
+ enforceNoEllipsis(label.getText(), label.getIcon(),
label.getIconTextGap(), graphics);
}
else if (component instanceof JButton) {
final JButton button = (JButton) component;
- enforceNoEllipsis(button.getText(), button.getIcon(),
button.getIconTextGap(), g2);
+ enforceNoEllipsis(button.getText(), button.getIcon(),
button.getIconTextGap(), graphics);
}
- if (shouldRenderGreek(renderContext)) {
- paintAsGreek(g2);
+ if (shouldRenderGreek(paintContext)) {
+ paintGreek(paintContext);
}
else {
- paint(g2);
+ paintComponent(paintContext);
}
}
@@ -380,31 +378,31 @@
}
}
- protected boolean shouldRenderGreek(final PPaintContext renderContext) {
- return renderContext.getScale() < renderCutoff
- // && pSwingCanvas.getInteracting()
- || minFontSize * renderContext.getScale() < 0.5;
+ protected boolean shouldRenderGreek(final PPaintContext paintContext) {
+ return paintContext.getScale() < renderCutoff
+ || minFontSize * paintContext.getScale() < 0.5;
}
/**
* Paints the Swing component as greek.
- *
- * @param g2 The graphics used to render the filled rectangle
+ *
+ * @param paintContext paint context
*/
- public void paintAsGreek(final Graphics2D g2) {
+ protected void paintGreek(PPaintContext paintContext) {
+ final Graphics2D graphics = paintContext.getGraphics();
final Color background = component.getBackground();
final Color foreground = component.getForeground();
final Rectangle2D rect = getBounds();
if (background != null) {
- g2.setColor(background);
+ graphics.setColor(background);
}
- g2.fill(rect);
+ graphics.fill(rect);
if (foreground != null) {
- g2.setColor(foreground);
+ graphics.setColor(foreground);
}
- g2.draw(rect);
+ graphics.draw(rect);
}
/**
@@ -420,27 +418,19 @@
/**
* Renders to a buffered image, then draws that image to the
drawing surface
* associated with g2 (usually the screen).
- *
- * @param g2 graphics context for rendering the JComponent
+ *
+ * @param paintContext paint context
*/
- public void paint(final Graphics2D g2) {
+ protected void paintComponent(final PPaintContext paintContext) {
if (component.getBounds().isEmpty()) {
// The component has not been initialized yet.
return;
}
+ final Graphics2D graphics = paintContext.getGraphics();
final PSwingRepaintManager manager = (PSwingRepaintManager)
RepaintManager.currentManager(component);
manager.lockRepaint(component);
-
- final RenderingHints oldHints = g2.getRenderingHints();
-
- g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
- g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
-
- component.paint(g2);
-
- g2.setRenderingHints(oldHints);
-
+ component.paint(graphics);
manager.unlockRepaint(component);
}
Index: src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java
===================================================================
--- src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java (revision 626)
+++ src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java (working copy)
@@ -38,6 +38,8 @@
import javax.swing.JPanel;
import javax.swing.RepaintManager;
+import edu.umd.cs.piccolo.util.PPaintContext;
+
import junit.framework.TestCase;
/**
@@ -100,8 +102,8 @@
final BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics =
GraphicsEnvironment.getLocalGraphicsEnvironment().createGraphics(img);
-
- pSwing.paint(graphics);
+ final PPaintContext paintContext = new PPaintContext(graphics);;
+ pSwing.paintComponent(paintContext);
assertEquals(Color.RED.getRGB(), img.getRGB(50, 50));
}
--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---