AffineTransform & Pfeile drehen

Status
Nicht offen für weitere Antworten.

Zentauro

Neues Mitglied
Hi!

ich hab da mal ne frage...

schreibe ein programm, mit dem man über netzwerk so eine art whiteboard anwendung hat. (ein server, mehrere clients)

alle formen (kreis, rechteck,....) gehen schon, bis auf den pfeil.

hab mir nun gedacht ich errechne mir die positionen des spitzes, aber irgendwie klappt das nicht...

Code:
public class Pfeil extends Element
{
  public Pfeil(int startx, int starty, int endx, int endy)
  {
    this.startx = startx;
    this.starty = starty;
    this.endx = endx;
    this.endy = endy;
    int compx;
    int compy;


  

    if (startx > endx)
    {
      compx = endx;
    }
    else
    {
      compx = startx;
    }
    if (starty > endy)
    {
      compy = endy;
    }
    else
    {
      compy = starty;
    }

    this.setBounds(compx, compy, Math.abs(endx - startx),
                   Math.abs(endy - starty));

  }

  public void paint(Graphics g)
 {
       super.paint(g);
       g.setColor(color);

       g.drawLine(startx - this.getLocation().x, starty - this.getLocation().y,
               endx - this.getLocation().x, endy - this.getLocation().y);

    double xp1 =  5*Math.cos((Math.atan(((starty - this.getLocation().y)-(endy - this.getLocation().y))/((startx - this.getLocation().x)-(endx - this.getLocation().x)))-45)*180/Math.PI);
    double yp1 =  5*Math.sin(Math.atan((((starty - this.getLocation().y)-(endy - this.getLocation().y))/((startx - this.getLocation().x)-(endx - this.getLocation().x)))-45)*180/Math.PI);
    double xp2 =  5*Math.cos(Math.atan(((starty - this.getLocation().y)-(endy - this.getLocation().y))/((startx - this.getLocation().x)-(endx - this.getLocation().x)))-45);
    double yp2 =  5*Math.sin(Math.atan(((starty - this.getLocation().y)-(endy - this.getLocation().y))/((startx - this.getLocation().x)-(endx - this.getLocation().x)))-45);

    g.drawLine((int)xp1, (int) yp1,
               endx - this.getLocation().x, endy - this.getLocation().y);

    g.drawLine((int)xp2,(int) yp2,
               endx - this.getLocation().x, endy - this.getLocation().y);
  }


}

die frage die sich nun stellt ist folgende:

wie wende ich nun die AffineTransformation Klasse auf einen Standardpfeil an oder was soll ich nun tun??

danke, mfg
 

Karl

Aktives Mitglied
Hallo,

Also als erstes solltest Du den Pfeil als Shape (Polygon) anlegen.
Dazu musst Du nur die Punkte so hinzufügen, wie man das auch auf Papier machen
würde, wenn man die Linien des Pfeils als Pfad immer von einem Punkt zum nächsten zieht.

Code:
        Polygon poly = new Polygon();
        Point p = ... //Punkt im geschlossenen Pfad
        poly.addPoint(p.x, p.y);

Ein Shape lässt sich jetzt bequem zeichnen (gelbe Füllung, schwarzer Rand):

Code:
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D)g;
                if (s != null) {
                    g2d.setColor(Color.YELLOW);
                    g2d.fill(s);
                    g2d.setColor(Color.BLACK);
                    g2d.draw(s);
                }

Nun kann man ein Shape mit AffineTransform auch in der Größe ändern oder auch drehen:

Beispiel: Drehung um den Mittelpunkt des umgebenden Rechtecks:
Code:
    Rectangle2D r2d = s.getBounds2D();
    double d = Math.toRadians(36); //Gradzahl, Zehnmal drehen = einmal komplett gedreht
    AffineTransform aff = AffineTransform.getRotateInstance(d, r2d.getCenterX(), r2d.getCenterY());
    s = aff.createTransformedShape(s);


Du könntest einfach die Pfeilspitze nehmen als Rotationspunkt nehmen.

Gruß,
Karl
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen


Oben