Image in ein Polygon zeichnen

farcry-gamer

Grünschnabel
Hallo erstamal
ich bin neu hier und hab eine Frage.
Ich habe einen Quader gemacht der aus drei Polygonen besteht.
der sieht dann aus wie wenn er 3D wäre
Hier is die Klasse
Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package engine.shapes;

import engine.utils.Vector3f;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Vector;

/**
 *
 * @author user
 */
public class Quader extends Shape{

    //Gibt an ob sich der Quader dreht
    private boolean rotating;
    //Würfelfarbe
    private Color color;
    //Vektorangaben
    private int x=1, y=2, z=3;
    //Array indem die Eckpunkte gespeichert sind
    private double p[][] = new double[9][4];
    //Rotationswinkel
    private double angle_x = 0.00;
    private double angle_y = 0.00;
    private double angle_z = 0.00;
    //Weltkoordinaten
    private int w = 500;
    //Array der Perspektiven informationen enthält
    private double[] c = new double[9];
    //Polygon Array der den Quader beinhaltet
    private Vector<Polygon> würfel;
    //Quaderlrotation
    private double rotationX;
    private double rotationY;
    private double rotationZ;
    //Abmessungen des Qaders
    private int länge;
    private int breite;
    private int höhe;
    
    public Quader(Vector3f pos, Vector3f extentsions){
        int a = extentsions.x;
        int b =  extentsions.y;
        int h = extentsions.z;
        länge = a;
        höhe = h;
        breite = b;
        init(pos,länge-(länge*pos.z/100),breite-(breite*pos.z/100),höhe-(höhe*pos.z/100));
    }
    
    public void paint(){
        
        würfel.clear();
        // In die Perspektive berechen Perspektive: *1+z/1000
        for (int i=1;i<9;i++) {
            c[i] = 1+p[i][z]/1000;
        }
        if(rotationY>0||rotationX>0||rotationZ>0)rotateLocal();     

        //[ p1 ]   [ q1 ]   [ p2*q3 - p3*q2 ]
        //[ p2 ] x [ q2 ] = [ p3*q1 - p1*q3 ]
        //[ p3 ]   [ q3 ]   [ p1*q2 - p2*q1 ]

                if((p[1][x]*c[1]-p[2][x]*c[2])*(p[3][y]*c[3]-p[2][y]*c[2])
                -(p[1][y]*c[1]-p[2][y]*c[2])*(p[3][x]*c[3]-p[2][x]*c[2]) > 0) {
                        // |2->1 x 2->3| > 0   
                        int xCoords1234[] = {(int)(p[1][x]*c[1])+X,(int)(p[2][x]*c[2])+X,
                        (int)(p[3][x]*c[3])+X,(int)(p[4][x]*c[4])+X};
                        int yCoords1234[] = {(int)(p[1][y]*c[1])+Y,(int)(p[2][y]*c[2])+Y,
                        (int)(p[3][y]*c[3])+Y,(int)(p[4][y]*c[4])+Y};
                         
                        würfel.add(new Polygon(xCoords1234, yCoords1234,4));                    
                }
                else if((p[7][x]*c[7]-p[6][x]*c[6])*(p[5][y]*c[5]-p[6][y]*c[6])
                -(p[7][y]*c[7]-p[6][y]*c[6])*(p[5][x]*c[5]-p[6][x]*c[6]) > 0) {
                         // |6->7 x 6->5| > 0
                        int xCoords5678[] = {(int)(p[5][x]*c[5])+X,(int)(p[6][x]*c[6])+X,
                        (int)(p[7][x]*c[7])+X,(int)(p[8][x]*c[8])+X};
                        int yCoords5678[] = {(int)(p[5][y]*c[5])+Y,(int)(p[6][y]*c[6])+Y,
                        (int)(p[7][y]*c[7])+Y,(int)(p[8][y]*c[8])+Y};
                         
                        würfel.add(new Polygon(xCoords5678, yCoords5678, 4));
            }

                if((p[6][x]*c[6]-p[2][x]*c[2])*(p[1][y]*c[1]-p[2][y]*c[2])
                -(p[6][y]*c[6]-p[2][y]*c[2])*(p[1][x]*c[1]-p[2][x]*c[2]) > 0) {
                        // |2->6 x 2->1| > 0
                        int xCoords1265[] = {(int)(p[1][x]*c[1])+X,(int)(p[2][x]*c[2])+X,
                        (int)(p[6][x]*c[6])+X,(int)(p[5][x]*c[5])+X};
                        int yCoords1265[] = {(int)(p[1][y]*c[1])+Y,(int)(p[2][y]*c[2])+Y,
                        (int)(p[6][y]*c[6])+Y,(int)(p[5][y]*c[5])+Y};
                         
                        würfel.add(new Polygon(xCoords1265, yCoords1265, 4));
               }
                else if((p[4][x]*c[4]-p[3][x]*c[3])*(p[7][y]*c[7]-p[3][y]*c[3])
                -(p[4][y]*c[4]-p[3][y]*c[3])*(p[7][x]*c[7]-p[3][x]*c[3]) > 0) {
                        // |3->4 x 3->7| > 0
                        int xCoords4378[] = {(int)(p[4][x]*c[4])+X,(int)(p[3][x]*c[3])+X,
                        (int)(p[7][x]*c[7])+X,(int)(p[8][x]*c[8])+X};
                        int yCoords4378[] = {(int)(p[4][y]*c[4])+Y,(int)(p[3][y]*c[3])+Y,
                        (int)(p[7][y]*c[7])+Y,(int)(p[8][y]*c[8])+Y};
                         
                        würfel.add(new Polygon(xCoords4378, yCoords4378, 4));
        }
                if((p[3][x]*c[3]-p[2][x]*c[2])*(p[6][y]*c[6]-p[2][y]*c[2])-(p[3][y]*c[3]
                -p[2][y]*c[2])*(p[6][x]*c[6]-p[2][x]*c[2]) > 0) {
                        // |2->3 x 2->6| > 0
                        int xCoords2376[] = {(int)(p[2][x]*c[2])+X,(int)(p[3][x]*c[3])+X,
                        (int)(p[7][x]*c[7])+X,(int)(p[6][x]*c[6])+X};
                        int yCoords2376[] = {(int)(p[2][y]*c[2])+Y,(int)(p[3][y]*c[3])+Y,
                        (int)(p[7][y]*c[7])+Y,(int)(p[6][y]*c[6])+Y};
                         
                        würfel.add(new Polygon(xCoords2376, yCoords2376, 4));
    }
        
                else if((p[5][x]*c[5]-p[1][x]*c[1])*(p[4][y]*c[4]-p[1][y]*c[1])
                -(p[5][y]*c[5]-p[1][y]*c[1])*(p[4][x]*c[4]-p[1][x]*c[1]) > 0) {
                        // |1->5 x 1->4| > 0
                        int xCoords1485[] = {(int)(p[1][x]*c[1])+X,(int)(p[4][x]*c[4])+X,
                        (int)(p[8][x]*c[8])+X,(int)(p[5][x]*c[5])+X};
                        int yCoords1485[] = {(int)(p[1][y]*c[1])+Y,(int)(p[4][y]*c[4])+Y,
                        (int)(p[8][y]*c[8])+Y,(int)(p[5][y]*c[5])+Y};
                         
                        würfel.add(new Polygon(xCoords1485, yCoords1485, 4));
    }
        rotate();
    }
    public void rotate(){
        double px,py,pz;
            for (int i=1;i<9;i++) {
            px = p[i][x];
            py = p[i][y];
            pz = p[i][z];

            // Rotation um x-Achse
            p[i][y] = py*Math.cos(angle_x)-pz*Math.sin(angle_x);
            p[i][z] = py*Math.sin(angle_x)+pz*Math.cos(angle_x);

            py = p[i][y];
            pz = p[i][z];

            // Rotation um y-Achse
            p[i][x] = px*Math.cos(angle_y)+pz*Math.sin(angle_y);
            p[i][z] =-px*Math.sin(angle_y)+pz*Math.cos(angle_y);

            px = p[i][x];

            // Rotation um z-Achse
            p[i][x] = px*Math.cos(angle_z)-py*Math.sin(angle_z);
            p[i][y] = py*Math.cos(angle_z)+px*Math.sin(angle_z);
        }
  }

    public Vector<Polygon> getWürfel() {
        return würfel;
    }

    public void setXRotationAngle(double angle_x) {
        this.angle_x = angle_x;
    }

    public void setYRotationAngle(double angle_y) {
        this.angle_y = angle_y;
    }

    public void setZRotationAngle(double angle_z) {
        this.angle_z = angle_z;
    }

    private void init(Vector3f pos, int a, int b, int h) {
        p[1][x] = -a; p[1][y] = -b; p[1][z] = -h;
        p[2][x] = +a; p[2][y] = -b; p[2][z] = -h;
        p[3][x] = +a; p[3][y] = -b; p[3][z] = +h;
        p[4][x] = -a; p[4][y] = -b; p[4][z] = +h;
        p[5][x] = -a; p[5][y] = +b; p[5][z] = -h;
        p[6][x] = +a; p[6][y] = +b; p[6][z] = -h;
        p[7][x] = +a; p[7][y] = +b; p[7][z] = +h;
        p[8][x] = -a; p[8][y] = +b; p[8][z] = +h;
        //Koordinaten spiegeln
         for (int i=1;i<9;i++) {
            p[i][y] = -p[i][y];
        }
        würfel = new Vector<Polygon>();
        rotationX=0;
        rotationY=0;
        rotationZ=0;
        color = Color.BLACK;
        w=0;
        X=pos.x;
        Y=pos.y;
        Z=pos.z;
        rotating = false;
        paint();
    }

  private void rotateLocal()
  {
   double px,py,pz;
            for (int i=1;i<9;i++) {
            px = p[i][x];
            py = p[i][y];
            pz = p[i][z];

            // Rotation um x-Achse
            p[i][y] = py*Math.cos(rotationX)-pz*Math.sin(rotationX);
            p[i][z] = py*Math.sin(rotationX)+pz*Math.cos(rotationX);

            py = p[i][y];
            pz = p[i][z];

            // Rotation um y-Achse
            p[i][x] = px*Math.cos(rotationY)+pz*Math.sin(rotationY);
            p[i][z] =-px*Math.sin(rotationY)+pz*Math.cos(rotationY);

            px = p[i][x];

            // Rotation um z-Achse
            p[i][x] = px*Math.cos(rotationZ)-py*Math.sin(rotationZ);
            p[i][y] = py*Math.cos(rotationZ)+px*Math.sin(rotationZ);
        }
   rotationX=0;
   rotationY=0;
   rotationZ=0;     
  }

  public void setRotationX(double rotationX)
  {
    this.rotationX = rotationX;
  }

  public void setRotationY(double rotationY)
  {
    this.rotationY = rotationY;
  }

  public void setRotationZ(double rotationZ)
  {
    this.rotationZ = rotationZ;
  }

  public void setColor(Color color)
  {
    this.color = color;
  }

  public Color getColor()
  {
    return color;
  }

    public boolean isRotating() {
        return rotating;
    }

    public void rot() {
        rotating=true;
    }
    public void setTranslation(Vector3f pos){
       X = pos.x;
       Y = pos.y;
       Z = pos.z;
       init(pos,länge-(länge*pos.z/100),breite-(breite*pos.z/100),höhe-(höhe*pos.z/100));
    }
    public Vector3f getTranslation(){
        return new Vector3f(X, Y, Z);
    }

    public int getLänge() {
        return länge;
    }

    public int getBreite() {
        return breite;
    }

    public int getHöhe() {
        return höhe;
    }
}

wie man heir sieht besteht er aus drei Polygonen
ich möchte jezt ein eines dieser Polygone ein Bild reinzeichnen?
Damit es dann aussieht wie eine Textur?
Geht das überhaupt?

Vielen Dank im Voraus
 
Ja danke mal!

Aber das eigentliche Problem ist nicht das ich das bild ins Polygon zeichnen
sondern das Bild selbst zu skalieren praktisch das das bild selbst ein polygon.
Also im prinziep das bild zu transformieren
 
Also mit Bild in Polygon einzeichnen habe ich das noch nicht ausprobiert,
aber eigentlich ist es nur Verhältnis REchnung. Manche sagen dazu auch Proportionalitätsrechnung oder Dreisatz.

Ich habe mal sowas ähnliches für Bildlichen im Tool-Tip-text benötigt.
hier mal so ein Ausschnitt.

Java:
public String getToolTipText(MouseEvent e)
    {                        
        Point2D p = new Point2D((int)((double)e.getX() / m_skalaFaktor),
	                            (int)((double)e.getY() / m_skalaFaktor));

        int x = getPolygonIdx(p);
        String tip = null;
        if (x>=0)
        {
        	Country c =(Country) m_sektors.get(x);        	
        	tip = "<html><body><pre>Land     :"+c.getName()+"\n"+
        	      "Kontinent:"+c.getContinent()+"\n";
        	if (c.getOwner()!=null)
        	{
        		ImageIcon imgIcon = c.getOwner().getImageIcon();
        		double scaleFactor = 100/(double)Math.min(imgIcon.getIconWidth(),imgIcon.getIconHeight());
        		
        		int w = new Double((double)imgIcon.getIconWidth()  * scaleFactor).intValue();
        		int h = new Double((double)imgIcon.getIconHeight() * scaleFactor).intValue();
        		
        		tip += "Besitzer :"+c.getOwner().getName()+"</pre>"+
        		       "<img src=\"file:" + imgIcon.getDescription()+ 
        		       "\" width="+w+" height="+h+" alt=\"Bild1\">";
        		
        	}
        	else
        	{
        		tip += "unbesetzt</pre>";
        	}
        	tip += "</body></html>";
        }

        return tip;
    }

vielleicht hilt es Dir weiter

Takidoso
 
Zurück