Hi,
I currently paint shapes to a canvas from an array and want to be able to move these shapes around the screen. Unfortunately I am unsure how to set up the MouseDragged event so that it redraws only the shape I want (Currently there is no link in the paint method to the mouseDragged variables), any ideas? the code is below.
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
public class MyCanvas extends Canvas implements MouseMotionListener{
private ArrayList liList;
private ArrayList reList;
private int rectX;
private int rectY;
int re;
int li;
public MyCanvas()
{
Parser p = new Parser();
liList = p.getLineArray();
reList = p.getRectArray();
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
while(re<reList.size())
{
Shape r = (Shape)reList.get(re);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.blue);
g2.fill(r);
g2.draw(r);
re++;
}
while(li<liList.size())
{
Shape l = (Shape)liList.get(li);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.red);
g2.fill(l);
g2.draw(l);
li++;
}
}
public void mouseDragged(MouseEvent e)
{
int rx = e.getX();
int ry = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
Cheers
I currently paint shapes to a canvas from an array and want to be able to move these shapes around the screen. Unfortunately I am unsure how to set up the MouseDragged event so that it redraws only the shape I want (Currently there is no link in the paint method to the mouseDragged variables), any ideas? the code is below.
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
public class MyCanvas extends Canvas implements MouseMotionListener{
private ArrayList liList;
private ArrayList reList;
private int rectX;
private int rectY;
int re;
int li;
public MyCanvas()
{
Parser p = new Parser();
liList = p.getLineArray();
reList = p.getRectArray();
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
while(re<reList.size())
{
Shape r = (Shape)reList.get(re);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.blue);
g2.fill(r);
g2.draw(r);
re++;
}
while(li<liList.size())
{
Shape l = (Shape)liList.get(li);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.red);
g2.fill(l);
g2.draw(l);
li++;
}
}
public void mouseDragged(MouseEvent e)
{
int rx = e.getX();
int ry = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
Cheers