moving a triangle

how would i move a triangle using amouselistner ?
[56 byte] By [sebperrya] at [2007-11-26 18:35:53]
# 1
http://forum.java.sun.com/thread.jspa?threadID=5137248&messageID=9501341#9501341
zadoka at 2007-7-9 6:09:57 > top of Java-index,Security,Cryptography...
# 2

// <applet code="TriangleMotion" width="400" height="400"></applet>

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.Color;

import java.awt.*;

import javax.swing.JApplet;

import javax.swing.JPanel;

public class TriangleMotion extends JApplet implements MouseListener,

MouseMotionListener

{

private static final long serialVersionUID = 1;

Triangle2 itemToDraw ;

DrawingPanel testpad ;

Point offset = new Point();

boolean drag = false;

public void init()

{

JPanel pane = new JPanel() ;

pane.setLayout(new BorderLayout()) ;

testpad = new DrawingPanel() ;

itemToDraw = new Triangle2() ;

pane.add(testpad,BorderLayout.CENTER) ;

testpad.addMouseListener(this) ;

testpad.addMouseMotionListener(this) ;

this.setContentPane(pane) ;

}

//@SuppressWarnings("serial")

public class DrawingPanel extends JPanel

{

protected void paintComponent(Graphics g)

{

super.paintComponent(g) ;

g.setColor( Color.green );

itemToDraw.draw(g) ;

}

}

public void mousePressed(MouseEvent arg0) {

Point p = arg0.getPoint();

if(itemToDraw.contains(p)) {

Point cp = itemToDraw.getCenter();

offset.x = p.x - cp.x;

offset.y = p.y - cp.y;

drag = true;

}

}

public void mouseReleased(MouseEvent arg0) {

drag = false;

}

public void mouseDragged(MouseEvent e) {

if(drag) {

int x = e.getX() - offset.x;

int y = e.getY() - offset.y;

itemToDraw.setFromCenter(x, y);

repaint();

}

}

public void mouseMoved(MouseEvent e) { }

public void mouseClicked(MouseEvent arg0) { }

public void mouseEntered(MouseEvent arg0) { }

public void mouseExited(MouseEvent arg0) { }

}

class Triangle2

{

Polygon poly;

Triangle2()

{

poly = new Polygon();

poly.addPoint(200, 200);

poly.addPoint(230, 250);

poly.addPoint(170, 250);

}

public void setFromCenter(int cx, int cy)

{

Point cp = getCenter();

int x = cx - cp.x;

int y = cy - cp.y;

poly.translate(x, y);

}

public void draw(Graphics g)

{

g.fillPolygon(poly);

}

public boolean contains(Point p)

{

return poly.contains(p);

}

public Point getCenter()

{

Rectangle r = poly.getBounds();

return new Point((int)r.getCenterX(), (int)r.getCenterY());

}

}

crwooda at 2007-7-9 6:09:57 > top of Java-index,Security,Cryptography...
# 3
cheers nice one
sebperrya at 2007-7-9 6:09:57 > top of Java-index,Security,Cryptography...
# 4
how would u repaint the triangle to where u clicked the mouse on the screen?
sebperrya at 2007-7-9 6:09:57 > top of Java-index,Security,Cryptography...
# 5

Changes to TriangleMotion:

//testpad.addMouseMotionListener(this) ;

...

public void mousePressed(MouseEvent arg0) {

itemToDraw.setFromCenter(arg0.getX(), arg0.getY());

repaint();

}

crwooda at 2007-7-9 6:09:57 > top of Java-index,Security,Cryptography...