PDA

Visualizza la versione completa : [java] traslare il quadrato sul canvas (era: un aiutino..)


mcgrafic
13-12-2004, 22:20
Sto preparando un'esame vorrei poter traslare il quadrato sul canvas ma non ne vengo fuori :dh˛: :dh˛: :dh˛:
Vi posto il codice...


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

public class DisegniQuadrati extends Frame {
public int x,y,lastX,lastY;
public Label l1x, l2y;
public Quadrato q1=new Quadrato((new Point2D.Double(50,50)), 25);
public QuadratoDeformabile q2=new QuadratoDeformabile((new Point2D.Double(100,150)), 35);;
CanvasUtente tela = new CanvasUtente();
public DisegniQuadrati() {
super ("Quadrati Deformabili");
setBounds (50, 50, 700, 450);
setLayout(new BorderLayout());
add(tela, "Center");
Panel controlli=new Panel(new FlowLayout());
controlli.setBackground(Color.lightGray);
Label l1=new Label("Cordinate X:");
controlli.add(l1);
Label l1x=new Label("("+x+")"+lastX);
controlli.add(l1x);
TextField txt1= new TextField("",5);
controlli.add(txt1);
Label l2=new Label(" Y:");
controlli.add(l2);
TextField txt2= new TextField("",5);
controlli.add(txt2);
Label l2y=new Label("("+y+")"+lastY);
controlli.add(l2y);
add(controlli, "South");
setVisible (true);

WindowAdapter adpWinAdapter =(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
} );
addWindowListener(adpWinAdapter);
}
class CanvasUtente extends Canvas{
public CanvasUtente(){

super();
AdpMsClick AdpMsClick = new AdpMsClick();
addMouseListener (AdpMsClick);
AdpMsDrag AdpMsDrag =new AdpMsDrag();
addMouseMotionListener (AdpMsDrag);

}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D)g;
q1.draw(g2);
q2.draw(g2);
}
public void update (Graphics g) {
Graphics2D g2=(Graphics2D)g;
paint (g2);
}
class AdpMsDrag extends MouseMotionAdapter {
public void mouseDragged (MouseEvent e) {
x = e.getX();
y = e.getY();
System.out.println("("+x+")"+lastX+" ("+y+")"+lastY+"Vert: "+q1.getVertSupSx());
l1x.setText("("+x+")"+lastX);
q1.trasla(x,y);
repaint();

}
}
class AdpMsClick extends MouseAdapter{
public void mousePressed (MouseEvent e) {
lastX = e.getX();
lastY = e.getY();
//p = new Point2D.Double(lastX, lastY);
}
}
}
public static void main (String [] args){
DisegniQuadrati DisegniQuadrati = new DisegniQuadrati();
}
}

class Quadrato {
protected Point2D vertSupSx;
protected Point2D vertInfDx;
protected int lato;
public Quadrato (Point2D vertSupSx, int lato) {
this.lato = lato;
this.vertSupSx = vertSupSx;
vertInfDx = new Point2D.Double (vertSupSx.getX() + lato, vertSupSx.getY() + lato);
}
public void draw (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
double latoOriz = vertInfDx.getX() - vertSupSx.getX();
double latoVert = vertInfDx.getY() - vertSupSx.getY();
g2.fill (new Rectangle2D.Double (vertSupSx.getX(), vertSupSx.getY(), latoOriz, latoVert));
}
public void trasla (int dx, int dy) {
vertSupSx = new Point2D.Double (vertSupSx.getX() + dx, vertSupSx.getY() + dy);
vertInfDx = new Point2D.Double (vertInfDx.getX() + dx, vertInfDx.getY() + dy);
}
public boolean contains (Point2D p) {
double latoOriz = vertInfDx.getX() - vertSupSx.getX();
double latoVert = vertInfDx.getY() - vertSupSx.getY();
Rectangle2D r = new Rectangle2D.Double (vertSupSx.getX(), vertSupSx.getY(),
latoOriz, latoVert);
return r.contains (p);
}
public String getVertSupSx () {
return "X:"+vertSupSx.getX()+" Y: "+vertSupSx.getY();
}
}
class QuadratoDeformabile extends Quadrato {
public QuadratoDeformabile (Point2D vertSupSx, int lato) {
super (vertSupSx, lato);
}
public void draw (Graphics g) { // Ereditato e sovrascritto
Graphics2D g2 = (Graphics2D) g;
g2.setPaint (Color.blue);
super.draw (g);
g2.setPaint (Color.red);
g2.fill (new Rectangle2D.Double (vertInfDx.getX() - 10, vertInfDx.getY() - 10, 10, 10));
}
public void setDeformazione (int dx, int dy) { // Aggiunto
double latoOriz = vertInfDx.getX() - vertSupSx.getX() + dx;
double latoVert = vertInfDx.getY() - vertSupSx.getY() + dy;
if (latoOriz < lato)
latoOriz = lato;
if (latoVert < lato)
latoVert = lato;
vertInfDx = new Point2D.Double (vertSupSx.getX() + latoOriz, vertSupSx.getY() + latoVert);
}
public boolean containsDeformazione (Point2D p) { // Aggiunto
Rectangle2D r = new Rectangle2D.Double (vertInfDx.getX() - 10, vertInfDx.getY() - 10, 10, 10);
return r.contains (p);
}
}



Un'altra cosa vorrei aggioranre le etichette...
Mi da un errore di thread...
Grazie in anticipo...

MMarzia
14-12-2004, 09:25
occhio ai titoli..

Loading