Hey Leute,
ich möchte gern in ein Panel mehrere Polygone einzeichnen die in einem XML file stehen. Wenn man dann auf diese Polygone klickt soll das Hintergrundbild geändert werden und neue Polygone aus einem anderen XML File eingezeichnet werden und so weiter. Ohne Dynamisches Laden bekomm ichs hin mit einem Polygon aber mit mehreren hab ich gerade n Brett vorm Kopf.
Hier mal der Java Quellcode
und hier das XML File
Für Hilfe wäre ich echt dankbar. Komm nicht weiter.
Danke schon mal
ich möchte gern in ein Panel mehrere Polygone einzeichnen die in einem XML file stehen. Wenn man dann auf diese Polygone klickt soll das Hintergrundbild geändert werden und neue Polygone aus einem anderen XML File eingezeichnet werden und so weiter. Ohne Dynamisches Laden bekomm ichs hin mit einem Polygon aber mit mehreren hab ich gerade n Brett vorm Kopf.
Hier mal der Java Quellcode
Code:
package main;
import java.awt.Color;
import main.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
/**
*
* @author M4ST3R
*/
public class panel_menue extends javax.swing.JPanel {
private String str_bild_url;
private Image img_bild;
private Toolkit tk_kit;
private XML_verarbeiten ref_xml = new XML_verarbeiten();
private String str_xml_file_pfad;
private Dimension dim_maxgroesse;
private Dimension dim_preferedgroesse;
private String str_haupt_bild_xml_pfad;
private boolean bool_polygon_eingezeichnet;
/** Nur zu Testzwecken später löschen*/
private Polygon poly_testpolygon = new Polygon();
/** Creates new form panel_menue */
public panel_menue(String xml_file_pfad) {
/** Variablen initialisieren */
str_xml_file_pfad = xml_file_pfad;
str_haupt_bild_xml_pfad = xml_file_pfad;
tk_kit = Toolkit.getDefaultToolkit();
poly_testpolygon.addPoint(270, 21);
poly_testpolygon.addPoint(281, 31);
poly_testpolygon.addPoint(285, 45);
poly_testpolygon.addPoint(286, 49);
poly_testpolygon.addPoint(292, 49);
poly_testpolygon.addPoint(294, 59);
poly_testpolygon.addPoint(291, 62);
poly_testpolygon.addPoint(284, 78);
poly_testpolygon.addPoint(275, 84);
poly_testpolygon.addPoint(262, 86);
poly_testpolygon.addPoint(249, 82);
poly_testpolygon.addPoint(238, 74);
poly_testpolygon.addPoint(239, 70);
poly_testpolygon.addPoint(226, 58);
poly_testpolygon.addPoint(230, 51);
poly_testpolygon.addPoint(237, 47);
poly_testpolygon.addPoint(235, 35);
poly_testpolygon.addPoint(238, 29);
poly_testpolygon.addPoint(254, 21);
initComponents();
bild_erzeugen();
mouselistener_einfuegen();
}
/**
* @return Erzeugt ein bild aus dem Pfad von str_bild_url
*/
private void bild_erzeugen() {
setStr_bild_url(str_xml_file_pfad);
if (str_bild_url != null && !str_bild_url.equals("")) {
File file = new File(str_bild_url);
/* Prüfen ob die Datei vorhanden ist */
if (file.exists()) {
setImg_bild(tk_kit.createImage(str_bild_url));
System.out.println("Bild erfolgreich von Pfad: " + str_bild_url + " geladen.");
} else {
/* Wenn die Datei nicht vorhanden ist FEHLER !! */
setImg_bild(null);
System.out.println("Es ist kein Bild an dem Pfad: " + str_bild_url + " vorhanden vorhanden!");
}
}
}
/** Schreibt das Bild in das Panel */
@Override
public void paintComponent(Graphics g) throws NullPointerException {
super.paintComponent(g);
/** Ließt die Panelgröße aus und setzt das Bild auf die Größe*/
Dimension s = getSize();
System.out.println("Panel wird neu gezeichnet ");
g.drawImage(getImg_bild(), 0, 0, s.width, s.height, this);
}
/** Fügt die Mouselistener ein und ließt die Bewegung der Maus aus*/
private void mouselistener_einfuegen() {
addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseMoved(MouseEvent e) {
// System.out.println("Punkte Mouse: x:" + e.getX() + " y:" + e.getY());
/** Ließt aus ob der Mauszeiger sich über einem Polygon befindet*/
if (!bool_polygon_eingezeichnet && poly_testpolygon.contains(e.getX(), e.getY())) {
bool_polygon_eingezeichnet = true;
polygone_einzeichnen(getGraphics());
} else if (bool_polygon_eingezeichnet && !poly_testpolygon.contains(e.getX(), e.getY())) {
bool_polygon_eingezeichnet = false;
update(getGraphics());
}
// Mausevent wieder freigeben
e.consume();
}
});
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Angeklickt wurde beim Punkt: x:" + e.getX() + " y:" + e.getY());
if (poly_testpolygon.contains(e.getX(), e.getY())) {
System.out.println("############ wooohooooo es wurde ins polygon geklickt ##########");
//////////////
setStr_xml_file_pfad("befundungs_file/befundung_ueberarbeitet2.xml");
/////////////
bild_erzeugen();
update(getGraphics());
}
// Mausevent wieder freigeben
e.consume();
}
public void mousePressed(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
// throw new UnsupportedOperationException("Not supported yet.");
}
});
}
public void polygone_einzeichnen(Graphics g) {
g.setColor(new Color(.9f, 0f, 0f, 0.4f));
g.fillPolygon(poly_testpolygon);
}
///////////////////////////////////////////////////////////////
/**
*
* Setter Methoden
*
*/
public void setImg_bild(Image img_bild_uebergabe) {
this.img_bild = img_bild_uebergabe;
}
public void setStr_bild_url(String str_xml_filepath) {
try {
str_bild_url = ref_xml.getBild_url(str_xml_filepath);
System.out.println("Der Bildpfad wurde auf: "+str_bild_url+" gesetzt");
} catch (Exception ex) {
System.out.println("Es ist ein Fehler in der Klasse Eigenbau_Befundung in der Methode setStr_Bild_url aufgetreten: ");
ex.printStackTrace();
}
}
public void setStr_xml_file_pfad(String str_xml_file_pfad) {
this.str_xml_file_pfad = str_xml_file_pfad;
}
public void setPoly_testpolygon(Polygon poly_testpolygon) {
this.poly_testpolygon = poly_testpolygon;
}
/**
*
* Getter Methoden
*
*/
public Image getImg_bild() {
return img_bild;
}
public String getStr_bild_url() {
return str_bild_url;
}
///////////////////////////////////////////////////////////////
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 65, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 65, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
und hier das XML File
HTML:
<Overlay>
<Name>Bild</Name>
<Image>c:/bild.jpg</Image>
<Polygon number="1">
<Name>Bild 2</Name>
<TargetImage>c:/bild2.xml</TargetImage>
<Shape>
<Point> <X>12</X> <Y>12</Y> </Point>
<Point> <X>300</X> <Y>12</Y> </Point>
<Point> <X>300</X> <Y>300</Y> </Point>
<Point> <X>12</X> <Y>300</Y> </Point>
</Shape>
</Polygon>
<Polygon number="2">
<Name>Bild3</Name>
<TargetImage>c:/bild3.xml</TargetImage>
<Shape>
<Point> <X>12</X> <Y>12</Y> </Point>
<Point> <X>300</X> <Y>12</Y> </Point>
<Point> <X>300</X> <Y>300</Y> </Point>
<Point> <X>12</X> <Y>300</Y> </Point>
</Shape>
</Polygon>
... etc
</Overlay>
Für Hilfe wäre ich echt dankbar. Komm nicht weiter.
Danke schon mal