2D-Grafik Rechtecke auf Bilder legen und auslesen

3p1kur

Mitglied
Hallo

Ich habe eine Grafik im PNG Format, auf diese möchte ich Rechtecke beliebiger Höhe und Breite legen.
Aus dem Ganzen möchte ich dann die XY Koordinate aller Rechtecke auf der Grafik auslesen sowie deren Höhe und Breite. Die rechtecke sollen per drag and drop verschiebbar sein.
Gibt es eventuell schon Software, die die oben genannte Funktionalität implementiert und aus der ich leicht die Informationen auslesen kann?

Gruß
 
Die Klasse [c]com.sun.magic.DoWhatIWant[/c] gibt es leider noch nicht. Soll das ganze auf sowas rauslaufen wie zum Beispiel(!) eine XML-Datei
Code:
<stuff>
  <image>image.png</image>
  <rectangle>10 20 30 40</rectangle>
  <rectangle>50 60 70 80</rectangle>
   ....
</stuff>
und eine Art "Editor" dafür?
 
Hallo Marco

Die Rechtecke werden aus einer XML Datei ausgelesen, sollen aber über den Grafikeditor verschiebbar/vergrößerbar sein
und dann wieder in der XML gespeichert werden.
Ich wollte nur wissen ob es da schon was gibt ansonsten muss ich es halt selbst implementieren.

Gruß
 
Ich habe eine Grafik im PNG Format, auf diese möchte ich Rechtecke beliebiger Höhe und Breite legen ... Die rechtecke sollen per drag and drop verschiebbar sein.

Hallo 3p1kur,

hier ist ein kleines Beispiel:

Java:
/*
 * ImageMarker.java
 *
 * Benutzt die Klassen JComponentBounds und ComponentsContainer:
 * http://wiki.byte-welt.net/wiki/JComponentBounds
 * http://wiki.byte-welt.net/wiki/ComponentsContainer
 *
 * Hier wird ein "mainpanel" mit OverlayLayout benutzt.
 * Darin sind ein "imagePanel" und ein überlagertes "markerPanel".
 * Die Methode "addMarker(markerPanel)" fügt einen neuen Marker hinzu.
 */

import container.JComponentBounds;
import container.ComponentsContainer;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.beans.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class ImageMarker extends JFrame {

    private final static String FILENAME = "Picture1.jpg";
    private JPanel mainpanel;
    private Rectangle marker;
    private JButton btAddRect, btAddOval;
    private final ComponentsContainer markerPanel;
    private String filenameXml = "ImageMarker.xml";
    private final JPanel controls;

    public ImageMarker() {
        super("ImageMarker");
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        controls = new JPanel();
        mainpanel = new JPanel() {

            @Override
            public boolean isOptimizedDrawingEnabled() {
                return false;
            }
        };
        mainpanel.setLayout(new OverlayLayout(mainpanel));
        markerPanel = new ComponentsContainer();
        load(null);
        markerPanel.setOpaque(false);

        Picture imagePanel = new Picture(FILENAME);

        mainpanel.add(markerPanel);//zuerst den Marker Panel (vorne)
        mainpanel.add(imagePanel);//dann die Bildkomponente (hinten)

        add(new JScrollPane(mainpanel));
        btAddRect = new JButton("Add Rectangle");
        btAddRect.setPreferredSize(new Dimension(130, 25));
        btAddRect.addActionListener(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                addMarker(markerPanel, MarkerComponent.RECT);
            }
        });
        controls.add(btAddRect);
        btAddOval = new JButton("Add Oval");
        btAddOval.setPreferredSize(new Dimension(130, 25));
        btAddOval.addActionListener(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                addMarker(markerPanel, MarkerComponent.OVAL);
            }
        });
        controls.add(btAddOval);
        add(controls, BorderLayout.SOUTH);
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(final WindowEvent e) {
                save(null);
                System.exit(0);
            }
        });
    }

    private void addMarker(final JPanel markerPanel, final int type) {
        marker = new Rectangle(0, 0, 100, 100);
        int m = JComponentBounds.MARGIN;
        final MarkerComponent markerComponent = new MarkerComponent(
                type, Color.RED, 1);
        markerComponent.setSize(marker.width + m * 2, marker.height
                + m * 2);
        markerComponent.setLocation(-m, -m);
        markerPanel.add(markerComponent);
        markerPanel.revalidate();
        displayMarkers();
    }

    private void displayMarkers() {
        System.out.println("Current Markers:");
        int componentCount = markerPanel.getComponentCount();
        for (int i = 0; i < componentCount; i++) {
            MarkerComponent component = (MarkerComponent) markerPanel.getComponent(i);
            int shape = component.getShape();
            String shapeS = null;
            if (shape == MarkerComponent.RECT) {
                shapeS = "RECT";
            } else if (shape == MarkerComponent.OVAL) {
                shapeS = "OVAL";
            }
            Rectangle componentRect = component.getComponentRect();
            System.out.println(shapeS + ": " + componentRect);
        }
    }

    private String save(final String filenameXml) {
        String returnValue = filenameXml;
        try {
            FileOutputStream fos = new FileOutputStream(
                    filenameXml == null ? this.filenameXml
                    : filenameXml);
            XMLEncoder o = new XMLEncoder(new BufferedOutputStream(fos));
            Component[] comps = markerPanel.getComponents();
            for (int i = 0; i < comps.length; i++) {
                o.writeObject(comps[i]);
            }
            o.close();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
            returnValue = null;
        }
        return returnValue;
    }

    private String load(final String filenameXml) {
        String returnValue = filenameXml;
        XMLDecoder d = null;
        Object component = null;
        try {
            returnValue = filenameXml == null ? this.filenameXml
                    : filenameXml;
            FileInputStream fis = new FileInputStream(returnValue);
            d = new XMLDecoder(new BufferedInputStream(fis));
            returnValue = new File(returnValue).getAbsolutePath();
            try {
                component = d.readObject();
                markerPanel.removeAll();
                while (component != null) {
                    markerPanel.add((Component) component);
                    component = d.readObject();
                }
            } catch (ArrayIndexOutOfBoundsException e) {
            }
            d.close();
        } catch (FileNotFoundException ex) {
        }
        return returnValue;
    }

    public static void main(final String[] args) {
        Runnable gui = new Runnable() {

            public void run() {
                new ImageMarker().setVisible(true);
            }
        };
        //GUI must start on EventDispatchThread:
        SwingUtilities.invokeLater(gui);
    }
}

class Picture extends JComponent {

    private BufferedImage image;
    private String filename;

    public Picture(String filename) {
        this.filename = filename;
    }

    @Override
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        if (image == null) {
            try {
                image =
                        ImageIO.read(getClass().getResource(filename));
                setPreferredSize(new Dimension(
                        image.getWidth(), image.getHeight()));
                revalidate();
            } catch (Exception ex) {
                image = new BufferedImage(
                        800, 600, BufferedImage.TYPE_INT_ARGB);
                Graphics graphics = image.getGraphics();
                graphics.setColor(Color.red);
                graphics.drawString("Bild nicht gefunden: "
                        + filename, 300, 500);
            }
        }
        Rectangle r = g.getClipBounds();
        g.drawImage(image, r.x, r.y, r.width + r.x, r.height + r.y,
                r.x, r.y, r.width + r.x, r.height + r.y, null);
    }

    public void setFilename(final String filename) {
        this.filename = filename;
        image = null;
        repaint();
    }
}

Java:
import container.JComponentBounds;
import java.awt.*;
import java.beans.*;
import java.util.logging.*;
import javax.swing.*;

/*
 * MarkerComponent.java
 */
public class MarkerComponent extends JComponentBounds {

    //PROPERTIES:
    private int shape;
    private Color color;
    private int strokeWidth;
    private BasicStroke stroke;
    //
    public static final int RECT = 0;
    public static final int OVAL = 1;
    private Color focus = new Color(0.1f, 0.1f, 0.1f, 0.4f);

    public MarkerComponent() {
        this(0, Color.GREEN, 5);
        transientProperties(new String[]{"focused",
                    "desktopIcon"});//desktopIcon is inherited
    }

    public MarkerComponent(final int shapeP, final Color colorP,
            final int strokeWidthP) {
        shape = shapeP;
        color = colorP;
        strokeWidth = strokeWidthP;
        stroke = new BasicStroke(strokeWidthP);
        setBounds(50, 50, 100, 100);
        setComponent(new ShapeLabel());
    }

    @Override
    public void setZorderAllowed(final boolean b) {
//        ((ComponentsContainer) getParent()).setZorderAllowed(b);
    }

    private void transientProperties(final String[] properties) {
        try {
            BeanInfo info =
                    Introspector.getBeanInfo(MarkerComponent.class);
            PropertyDescriptor[] propertyDescriptors =
                    info.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; ++i) {
                PropertyDescriptor pd = propertyDescriptors[i];
                for (int j = 0; j < properties.length; j++) {
                    if (pd.getName().equals(properties[j])) {
                        pd.setValue("transient", Boolean.TRUE);
                    }
                }
            }
        } catch (IntrospectionException ex) {
            Logger.getLogger(MarkerComponent.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    public int getShape() {
        return shape;
    }

    public Color getColor() {
        return color;
    }

    public BasicStroke getStroke() {
        return stroke;
    }

    public void setStroke(final BasicStroke stroke) {
        this.stroke = stroke;
    }

    public void setShape(final int shape) {
        this.shape = shape;
    }

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

    public int getStrokeWidth() {
        return strokeWidth;
    }

    public void setStrokeWidth(final int strokeWidth) {
        this.strokeWidth = strokeWidth;
        stroke = new BasicStroke(strokeWidth);
    }

    class ShapeLabel extends JLabel {

        public ShapeLabel() {
            super();
        }

        @Override
        protected void paintComponent(final Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(color);
            g2.setStroke(stroke);
            int w = (int) stroke.getLineWidth();
            int width = getParent().getWidth();
            int height = getParent().getHeight();
            Rectangle location = getComponentRect();
            switch (shape) {
                case RECT:
                    g2.drawRect(w / 2 + 1, w / 2 + 1, width - w
                            - 1, height - w - 1);
                    break;
                case OVAL:
                    g2.drawOval(w / 2 + 1, w / 2 + 1, width - w
                            - 1, height - w - 1);
                    break;
            }
            if (isFocused()) {
                g2.setColor(focus);
                g2.fillRect(0, 0, width, height);
                g2.setColor(Color.GRAY);
                g2.drawString(location.x + ", " + location.y + ", "
                        + (width) + "x" + (height), 5, 15);
            }
        }
    }
}

Gruß,
André
 

Neue Themen


Zurück
Oben