"Easy" question concerning 2D graphics

G

gajo

Hello!
I'm an absolute newbie when it comes to 2D graphics in Java. I have
downloaded an e-book about the subject, but I thought I could get a head
start if I post my question here. I did use graphics in Delphi, but it seems
to me that Java implements these things in an entirely different way.

So here's my problem: Let's say I have a simple form, and I draw a circle on
it. Nothing special, an empty circle, allthough I could perhaps fill it with
yellow :) Now, what I want to do is that when I click on the circle's
interior with the mouse, I can drag it around in the form. How can I do
this?
Do I need to make a new component that will have certain attributes, and in
the constructor I set the radius, then I set the mouse event to drag the
component and finally make it visible - OR there's already something like
this made in Java?

Thanks, Gajo
 
R

Ryan Stewart

gajo said:
Hello!
I'm an absolute newbie when it comes to 2D graphics in Java. I have
downloaded an e-book about the subject, but I thought I could get a head
start if I post my question here. I did use graphics in Delphi, but it seems
to me that Java implements these things in an entirely different way.

So here's my problem: Let's say I have a simple form, and I draw a circle on
it. Nothing special, an empty circle, allthough I could perhaps fill it with
yellow :) Now, what I want to do is that when I click on the circle's
interior with the mouse, I can drag it around in the form. How can I do
this?
Do I need to make a new component that will have certain attributes, and in
the constructor I set the radius, then I set the mouse event to drag the
component and finally make it visible - OR there's already something like
this made in Java?

Thanks, Gajo
This is probably as simple as you could get:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;

public class DragMe extends JFrame {

private Circle circle = new Circle(200, 200, 25);
private boolean dragging = false;

public DragMe() {
super("Draggy Thing");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 500, 500);
this.setVisible(true);
circle.setInsets(this.getInsets());
Container cp = this.getContentPane();
cp.addMouseListener(new EventManager(this));
cp.addMouseMotionListener(new EventManager(this));
}

public void startDrag(Point p) {
if (circle.contains(p)) {
dragging = true;
}
}

public void setCursor(Point p) {
if (dragging) {
circle.setPosition(p);
repaint();
}
}

public void stopDrag() {
dragging = false;
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.YELLOW);
circle.render(g);
}

public static void main(String[] args) {
DragMe d = new DragMe();
}

class Circle {

private int x;
private int y;
private int radius;
private int xOffset;
private int yOffset;

public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
}

public void setInsets(Insets insets) {
this.xOffset = insets.left;
this.yOffset = insets.top;
}

public void setPosition(Point p) {
this.x = p.x;
this.y = p.y;
}

public void render(Graphics g) {
int diameter = radius << 1;
g.setColor(Color.YELLOW);
g.fillOval(x - radius + xOffset, y - radius + yOffset, diameter,
diameter);
}

public boolean contains(Point p) {
int xDiff = p.x - x;
int yDiff = p.y - y;
long distanceSquared = xDiff * xDiff + yDiff * yDiff;
if (distanceSquared <= radius * radius) {
return true;
}
return false;
}
}

class EventManager extends MouseInputAdapter {

private DragMe eventConsumer;

public EventManager(DragMe dc) {
this.eventConsumer = dc;
}

public void mousePressed(MouseEvent me) {
eventConsumer.startDrag(me.getPoint());
}

public void mouseReleased(MouseEvent me) {
eventConsumer.stopDrag();
}

public void mouseDragged(MouseEvent me) {
eventConsumer.setCursor(me.getPoint());
}
}
}
 
L

Larry Coon

Ryan said:
This is probably as simple as you could get:

(snipped)

Well, I think this is a little simpler (or at least about
half the size, if you want to define "simple" that way),
albeit kludgy:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class DragCircle extends JFrame {
private static final int RADIUS = 50;

public DragCircle() {
super("Drag the circle around");

getContentPane().add(new DragPanel(), BorderLayout.CENTER);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}

private class DragPanel extends JPanel
implements MouseMotionListener {
private Ellipse2D.Double ellipse;
private boolean dragAllowed = false;

public DragPanel() {
ellipse = new Ellipse2D.Double(200, 200, RADIUS, RADIUS);

addMouseMotionListener(this);

addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
dragAllowed = false;
}

public void mouseReleased(MouseEvent e) {
dragAllowed = false;
}

public void mousePressed(MouseEvent e) {
dragAllowed = ellipse.contains(e.getX(), e.getY());
}
});
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

((Graphics2D) g).draw(ellipse);
}

public void mouseDragged(MouseEvent e) {
if (dragAllowed) {
ellipse.setFrame(e.getX() - (RADIUS / 2),
e.getY() - (RADIUS / 2), RADIUS, RADIUS);

repaint();
}
}

public void mouseMoved(MouseEvent e) { }
}

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


Larry Coon
University of California
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top