Skip to main content

DragDemo

3 replies [Last post]
jantaubert
Offline
Joined: 2007-12-12
Points: 0

A demo for using SGMouseListener. I started to like SceneGraph, great work so far! It would be nice, if some simpler demos would be shipped with SceneGraph.

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;

import com.sun.scenario.scenegraph.JSGPanel;
import com.sun.scenario.scenegraph.SGGroup;
import com.sun.scenario.scenegraph.SGNode;
import com.sun.scenario.scenegraph.SGShape;
import com.sun.scenario.scenegraph.SGText;
import com.sun.scenario.scenegraph.SGTransform;
import com.sun.scenario.scenegraph.event.SGMouseListener;

public class DragDemo implements SGMouseListener, MouseListener {

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

private Point point;

private SGGroup scene;

public DragDemo() {
JFrame f = new JFrame("Drag Circle Demo");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

scene = new SGGroup();

JSGPanel panel = new JSGPanel();
panel.addMouseListener(this);
panel.setBackground(Color.ORANGE);
panel.setScene(scene);

SGText textNode = new SGText();
textNode.setText("Drag circle; Middle click - delete circle; Right click - create new circle");
textNode.setLocation(new Point(2, 10));
textNode.setDrawPaint(Color.BLACK);
textNode.setFillPaint(Color.BLUE);
textNode.setFont(new Font("Sanserif", Font.BOLD, 12));
scene.add(textNode);

addCircle(50, 50);

f.add(panel);
f.pack();
f.setSize(400, 200);
f.setVisible(true);
}

private void addCircle(double x, double y) {
SGShape circ = new SGShape();
circ.setShape(new Ellipse2D.Double(x - 25, y - 25, 50, 50));
circ.setFillPaint(Color.BLUE);
circ.setAntialiasingHint(RenderingHints.VALUE_ANTIALIAS_ON);

SGTransform.Translate trans = SGTransform.createTranslation(0, 0, circ);
trans.addMouseListener(this);
scene.add(trans);
}

public void mouseClicked(MouseEvent e, SGNode node) {
if (e.getButton() == MouseEvent.BUTTON2) {
node.getParent().remove(node);
}
}

public void mousePressed(MouseEvent e, SGNode node) {
point = e.getPoint();
}

public void mouseReleased(MouseEvent e, SGNode node) {

}

public void mouseEntered(MouseEvent e, SGNode node) {

}

public void mouseExited(MouseEvent e, SGNode node) {

}

public void mouseDragged(MouseEvent e, SGNode node) {
if (node instanceof SGTransform.Translate) {
Point newpoint = e.getPoint();
SGTransform.Translate trans = (SGTransform.Translate) node;
trans.translateBy(newpoint.getX() - point.getX(), newpoint.getY()
- point.getY());
point = newpoint;
}
}

public void mouseMoved(MouseEvent e, SGNode node) {

}

public void mouseWheelMoved(MouseWheelEvent e, SGNode node) {

}

public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
addCircle(e.getX(), e.getY());
}
}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
platon007
Offline
Joined: 2008-04-10
Points: 0

And I've got another example of drag application.
Left button drag moves figure, Right button drag rotates it about figure's center.

import com.sun.scenario.scenegraph.*;
import com.sun.scenario.scenegraph.event.SGMouseAdapter;

import javax.swing.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.awt.*;
import java.awt.event.MouseEvent;

public class Google extends JSGPanel {

public Google() {
SGGroup group = new SGGroup();
SGShape shape = new SGShape();
shape.setShape(new RoundRectangle2D.Double(-100, -75 , 200, 150, 50, 50));
shape.setAntialiasingHint(RenderingHints.VALUE_ANTIALIAS_ON);
group.add(shape);
initPlaces(group, shape);
final SGTransform.Rotate r = SGTransform.createRotation(0, group);
final SGTransform.Translate t = SGTransform.createTranslation(100, 75, r);
shape.addMouseListener(new SGMouseAdapter() {
private MouseStrategy strategy;
public void mousePressed(MouseEvent e, SGNode node) {
switch(e.getButton()) {
case MouseEvent.BUTTON1 : strategy = new TranslateStrategy(t); break;
case MouseEvent.BUTTON3 : strategy = new RotageStrategy(t, r);
}
strategy.mousePressed(e);
}

public void mouseDragged(MouseEvent e, SGNode node) {
strategy.mouseDragged(e);
}
});
setScene(t);
}

private void initPlaces(final SGGroup group, final SGShape sh) {
Rectangle2D r = sh.getBounds();
for (int i = 0; i < 10; i++)
for (int j = 0; j < 5; j++) {
SGShape shape = new SGShape();
shape.setShape(new Rectangle2D.Double(0, 0, 10, 10));
shape.setFillPaint(Color.RED);
shape.setAntialiasingHint(RenderingHints.VALUE_ANTIALIAS_ON);
final SGTransform.Translate t = SGTransform.createTranslation(r.getX() + 10 + i*19, r.getY() + 10 + j*20, shape);
shape.addMouseListener(new SGMouseAdapter() {

private Point startPoint;

public void mouseDragged(MouseEvent e, SGNode node) {
t.translateBy(e.getX() - startPoint.x, e.getY() - startPoint.y);
startPoint = e.getPoint();
}

public void mousePressed(MouseEvent e, SGNode node) {
startPoint = e.getPoint();
}
});
shape.setMouseBlocker(true);
group.add(t);
}
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new Google());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}

interface MouseStrategy {
public void mouseDragged(MouseEvent e);
public void mousePressed(MouseEvent e);
}

class TranslateStrategy implements MouseStrategy {

private SGTransform.Translate r;
private Point2D startPoint;

public TranslateStrategy(SGTransform.Translate r) {
this.r = r;
}

public void mouseDragged(MouseEvent e) {
r.translateBy(e.getX() - startPoint.getX(), e.getY() - startPoint.getY());
startPoint = e.getPoint();
}

public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}
}

class RotageStrategy implements MouseStrategy {

private SGTransform.Rotate r;
private Point p;
private SGNode n;

public RotageStrategy(SGNode n, SGTransform.Rotate r) {
this.n = n;
this.r = r;
}

public void mouseDragged(MouseEvent e) {
Rectangle2D rect = n.getBounds();
double angle = Math.atan2(e.getY() - rect.getCenterY(), e.getX() - rect.getCenterX());
r.setRotation(angle);
}

public void mousePressed(MouseEvent e) {

}
}

But there's some problem. Nested red elements are dragged in their own coordinates. But I want to drag them as usual. How to?

Message was edited by: platon007

andy_till
Offline
Joined: 2008-01-19
Points: 0

This was helpful in working out what I needed in the mouseDragged method. However I found that this code didn't give me pixel perfect dragging in my project, the following code did work for me.

[i]
@Override
public void mouseDragged(MouseEvent e, SGNode node)
{
if(node instanceof SGTransform.Translate)
{
SGTransform.Translate dragger = (SGTransform.Translate)node;
Point newpoint = e.getLocationOnScreen();
double x = newpoint.getX() - point.getX();
double y = newpoint.getY() - point.getY();
dragger.translateBy(x, y);

point = newpoint;
}
}
[/i]

brianh
Offline
Joined: 2007-12-15
Points: 0

Thanks for the code.

One thing. If you add the following code

circ.setDrawPaint( Color.red );
circ.setDrawStroke(new BasicStroke(2f));

to the addCircle() method, there is a repaint problem when you drag the circle upwards and/or to the left. It only occurs with even values. When I turn on the debugBounds = true in the JSGPanel class, the bounds that is drawn does not encompass the Stroke (again for values of 2n). I don't have a lot of experience with the java2d (or scene graphs for that matter) so I'm not sure if it's a bound accumulation problem etc...

Update:

Again, I'm probably clueless here, but I notice that the paintComponent(Graphics g) in the JSGPanel class returns that same sized dirtyRegion, from the g.getClipBounds() for both stroke values of 1f and 2f or any (2n & 2n - 1). Seems odd to me on the face of it.

Message was edited by: brianh