public
Last active

  • Download Gist
InterfaceFrame.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
package edu.umd.cs.piccolo.tutorial;
 
import java.awt.Color;
import java.awt.Graphics2D;
 
import edu.umd.cs.piccolo.*;
import edu.umd.cs.piccolo.event.*;
import edu.umd.cs.piccolo.nodes.*;
import edu.umd.cs.piccolo.util.*;
import edu.umd.cs.piccolox.*;
 
public class InterfaceFrame extends PFrame {
public void initialize() {
// Remove the Default pan event handler and add a drag event handler
// so that we can drag the nodes around individually.
getCanvas().setPanEventHandler(null);
getCanvas().addInputEventListener(new PDragEventHandler());
// Add Some Default Nodes
 
// Create a node.
PNode aNode = new PNode();
 
// A node will not be visible until its bounds and brush are set.
aNode.setBounds(0, 0, 100, 80);
aNode.setPaint(Color.RED);
 
// A node needs to be a descendent of the root to be displayed.
PLayer layer = getCanvas().getLayer();
layer.addChild(aNode);
// A node can have child nodes added to it.
PNode anotherNode = new PNode();
anotherNode.setBounds(0, 0, 100, 80);
anotherNode.setPaint(Color.YELLOW);
aNode.addChild(anotherNode);
// The base bounds of a node are easy to change. Changing the bounds
// of a node will not affect it's children.
aNode.setBounds(-10, -10, 200, 110);
// Each node has a transform that can be used to modify the position,
// scale or rotation of a node. Changing a node's transform, will
// transform all of its children as well.
aNode.translate(100, 100);
aNode.scale(1.5f);
aNode.rotate(45);
 
// Add a couple of PPath nodes and a PText node.
layer.addChild(PPath.createEllipse(0, 0, 100, 100));
layer.addChild(PPath.createRectangle(0, 100, 100, 100));
layer.addChild(new PText("Hello World"));
 
// Here we create a PImage node that displays a thumbnail image
// of the root node. Then we add the new PImage to the main layer.
PImage image = new PImage(layer.toImage(300, 300, null));
layer.addChild(image);
 
// Create a New Node using Composition
 
PNode myCompositeFace = PPath.createRectangle(0, 0, 100, 80);
// Create parts for the face.
PNode eye1 = PPath.createEllipse(0, 0, 20, 20);
eye1.setPaint(Color.YELLOW);
PNode eye2 = (PNode) eye1.clone();
PNode mouth = PPath.createRectangle(0, 0, 40, 20);
mouth.setPaint(Color.BLACK);
// Add the face parts.
myCompositeFace.addChild(eye1);
myCompositeFace.addChild(eye2);
myCompositeFace.addChild(mouth);
// Don't want anyone grabbing out our eye's.
myCompositeFace.setChildrenPickable(false);
// Position the face parts.
eye2.translate(25, 0);
mouth.translate(0, 30);
// Set the face bounds so that it neatly contains the face parts.
PBounds b = myCompositeFace.getUnionOfChildrenBounds(null);
b.inset(-5, -5);
myCompositeFace.setBounds(b);
// Opps its to small, so scale it up.
myCompositeFace.scale(1.5);
layer.addChild(myCompositeFace);
 
// Create a New Node using Inheritance.
ToggleShape ts = new ToggleShape();
ts.setPaint(Color.ORANGE);
layer.addChild(ts);
}
class ToggleShape extends PPath {
private boolean fIsPressed = false;
 
public ToggleShape() {
setPathToEllipse(0, 0, 100, 80);
addInputEventListener(new PBasicInputEventHandler() {
public void mousePressed(PInputEvent event) {
super.mousePressed(event);
fIsPressed = true;
repaint();
}
public void mouseReleased(PInputEvent event) {
super.mouseReleased(event);
fIsPressed = false;
repaint();
}
});
}
protected void paint(PPaintContext paintContext) {
if (fIsPressed) {
Graphics2D g2 = paintContext.getGraphics();
g2.setPaint(getPaint());
g2.fill(getBoundsReference());
} else {
super.paint(paintContext);
}
}
}
public static void main(String[] args) {
new InterfaceFrame();
}
}
piccolo graphics example 1.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
; This is the "Building the Interface" program for the Piccolo2D structured 2D
; graphics framework. It adds a variety of dragable Piccolo2D "nodes"(text,
; geometric shapes, and images) into the main window, as well as a derived
; "toggle shape" that changes to a rectangle when you click it.
;
; You can find the original Java program on this same webpage, or at
;
; http://www.piccolo2d.org/learn/interface.html
;
; You can compare this program to the original Java program to learn much
; about at least one valid way to write Java using Clojure syntax.
;
; create-toggle-shape implementation graciously provided by Meikel Brandmeyer;
; --visit his Clojure projects at http://kotka.de/projects/index.html
 
(ns piccolotest
(:gen-class)
(:import
(java.awt Color Graphics2D)
(edu.umd.cs.piccolo.nodes PHtmlView PImage PPath PText)
(edu.umd.cs.piccolo PCamera PCanvas PInputManager PLayer PNode
POffscreenCanvas PRoot)
(edu.umd.cs.piccolo.event PBasicInputEventHandler PDragEventHandler
PDragSequenceEventHandler PInputEvent PInputEventFilter PPanEventHandler
PZoomEventHandler)
(edu.umd.cs.piccolo.util PAffineTransform PBounds PDebug PDimension
PObjectOutputStream PPaintContext PPickPath PStack PUtil)
(edu.umd.cs.piccolox PApplet PFrame)))
 
(defn create-toggle-shape
"Creates an ellipse that changes shape when it is clicked."
[]
(let [fIsPressed? (atom false)
shape (proxy [PPath] []
(paint
[#^PPaintContext paintContext]
(if @fIsPressed?
(doto (.getGraphics paintContext)
(.setPaint (.getPaint this))
(.fill (.getBoundsReference this)))
(proxy-super paint paintContext))))]
 
(doto shape
(.setPathToEllipse 0 0 100 80)
(.addInputEventListener
(proxy [PBasicInputEventHandler] []
(mousePressed
[event]
(proxy-super mousePressed event)
(reset! fIsPressed? true)
(.repaint shape))
(mouseReleased
[event]
(proxy-super mouseReleased event)
(reset! fIsPressed? false)
(.repaint shape)))))))
 
 
(defn create-interface-frame
"Creates the main InterfaceFrame used by the program."
[]
(proxy [PFrame] []
 
(initialize []
(let [aNode (PNode.)
anotherNode (PNode.)
layer (.. this getCanvas getLayer)
image (PImage. (.toImage layer 300 300 nil))]
 
(.. this getCanvas (setPanEventHandler nil))
(.. this getCanvas (addInputEventListener (PDragEventHandler.)))
 
(.setBounds aNode 0 0 100 80)
(.setPaint aNode (Color/RED))
(.addChild layer aNode)
 
(.setBounds anotherNode 0 0 100 80)
(.setPaint anotherNode (Color/YELLOW))
(.addChild aNode anotherNode)
 
(.setBounds aNode -10 -10 200 110)
 
(.translate aNode 100 100)
(.scale aNode 1.5)
(.rotate aNode 45)
 
(.addChild layer (PPath/createEllipse 0 0 100 100))
(.addChild layer (PPath/createRectangle 0 100 100 100))
(.addChild layer (PText. "Hello World"))
 
(let [image (PImage. (.toImage layer 300 300 nil))]
(.addChild layer image))
 
(let [myCompositeFace (PPath/createRectangle 0 0 100 80)
eye1 (PPath/createEllipse 0 0 20 20)
eye2 (.clone eye1)
mouth (PPath/createRectangle 0 0 40 20)
]
(.setPaint eye1 (Color/YELLOW))
(.setPaint eye2 (Color/YELLOW)) ; "let" forces deviation from original
(.setPaint mouth (Color/BLACK))
(.addChild myCompositeFace eye1)
(.addChild myCompositeFace eye2)
(.addChild myCompositeFace mouth)
 
(.setChildrenPickable myCompositeFace false)
 
(.translate eye2 25 0)
(.translate mouth 0 30)
 
(let [b (.getUnionOfChildrenBounds myCompositeFace nil)]
(.inset b -5 -5)
(.setBounds myCompositeFace b)
 
(.scale myCompositeFace 1.5)
 
(.addChild layer myCompositeFace)))
 
(let [ts (create-toggle-shape)]
(.setPaint ts (Color/ORANGE))
(.addChild layer ts))))))
 
 
(defn -main []
(let [main-frame (create-interface-frame)]
(.setVisible main-frame true)))

package edu.umd.cs.piccolo.tutorial;

import java.awt.Color;
import java.awt.Graphics2D;

import edu.umd.cs.piccolo.;
import edu.umd.cs.piccolo.event.
;
import edu.umd.cs.piccolo.nodes.;
import edu.umd.cs.piccolo.util.
;
import edu.umd.cs.piccolox.*;

public class InterfaceFrame extends PFrame {

public void initialize() {
    // Remove the Default pan event handler and add a drag event handler
    // so that we can drag the nodes around individually.
    getCanvas().setPanEventHandler(null);
    getCanvas().addInputEventListener(new PDragEventHandler());

    // Add Some Default Nodes

    // Create a node.
    PNode aNode = new PNode();

    // A node will not be visible until its bounds and brush are set.
    aNode.setBounds(0, 0, 100, 80);
    aNode.setPaint(Color.RED);

    // A node needs to be a descendent of the root to be displayed.
    PLayer layer = getCanvas().getLayer();
    layer.addChild(aNode);

    // A node can have child nodes added to it.
    PNode anotherNode = new PNode();
    anotherNode.setBounds(0, 0, 100, 80);
    anotherNode.setPaint(Color.YELLOW);
    aNode.addChild(anotherNode);

    // The base bounds of a node are easy to change.  Changing the bounds
    // of a node will not affect it's children.
    aNode.setBounds(-10, -10, 200, 110);

    // Each node has a transform that can be used to modify the position,
    // scale or rotation of a node.  Changing a node's transform, will
    // transform all of its children as well.
    aNode.translate(100, 100);
    aNode.scale(1.5f);
    aNode.rotate(45);

    // Add a couple of PPath nodes and a PText node.
    layer.addChild(PPath.createEllipse(0, 0, 100, 100));
    layer.addChild(PPath.createRectangle(0, 100, 100, 100));
    layer.addChild(new PText("Hello World"));

    // Here we create a PImage node that displays a thumbnail image
    // of the root node. Then we add the new PImage to the main layer.
    PImage image = new PImage(layer.toImage(300, 300, null));
    layer.addChild(image);

    // Create a New Node using Composition

    PNode myCompositeFace = PPath.createRectangle(0, 0, 100, 80);

    // Create parts for the face.
    PNode eye1 = PPath.createEllipse(0, 0, 20, 20);
    eye1.setPaint(Color.YELLOW);
    PNode eye2 = (PNode) eye1.clone();
    PNode mouth = PPath.createRectangle(0, 0, 40, 20);
    mouth.setPaint(Color.BLACK);

    // Add the face parts.
    myCompositeFace.addChild(eye1);
    myCompositeFace.addChild(eye2);
    myCompositeFace.addChild(mouth);

    // Don't want anyone grabbing out our eye's.
    myCompositeFace.setChildrenPickable(false);

    // Position the face parts.
    eye2.translate(25, 0);
    mouth.translate(0, 30);

    // Set the face bounds so that it neatly contains the face parts.
    PBounds b = myCompositeFace.getUnionOfChildrenBounds(null);
    b.inset(-5, -5);
    myCompositeFace.setBounds(b);

    // Opps its to small, so scale it up.
    myCompositeFace.scale(1.5);

    layer.addChild(myCompositeFace);

    // Create a New Node using Inheritance.
    ToggleShape ts = new ToggleShape();
    ts.setPaint(Color.ORANGE);
    layer.addChild(ts);
}

class ToggleShape extends PPath {

    private boolean fIsPressed = false;

    public ToggleShape() {
        setPathToEllipse(0, 0, 100, 80);

        addInputEventListener(new PBasicInputEventHandler() {
            public void mousePressed(PInputEvent event) {
                super.mousePressed(event);
                fIsPressed = true;
                repaint();
            }
            public void mouseReleased(PInputEvent event) {
                super.mouseReleased(event);
                fIsPressed = false;
                repaint();
            }
        });
    }

    protected void paint(PPaintContext paintContext) {
        if (fIsPressed) {
            Graphics2D g2 = paintContext.getGraphics();
            g2.setPaint(getPaint());
            g2.fill(getBoundsReference());
        } else {
            super.paint(paintContext);
        }
    }
}

public static void main(String[] args) {
    new InterfaceFrame();
}

}

Ignore previous note; it contains nothing new, and I can't figure out how to delete it. Thanks. --gw

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.