Avatar billede andersbl Nybegynder
14. maj 2011 - 21:55 Der er 5 kommentarer

Redigerer i et billede online og gem det (gratis J-Painter alternativ)

Hej Eksperter
Jeg har ledt efter løsninger, for brugere fra en hjemmeside, kan markerer noget på et billede og gemme(uploade) det.

Jeg har fundet J-Painter som kan alt det jeg har behov for:
http://javaboutique.internet.com/JPainter/

Men desværre koster det penge og har ingen penge at gøre godt med.

Har i nogle forslag??

TAK
Avatar billede arne_v Ekspert
14. maj 2011 - 22:05 #1
Fra lageret:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class DrawApplet extends JApplet implements ActionListener {
    private final static String UPLOAD_URL = "http://localhost/upload.asp";
    private DrawablePicture dp;
    private JButton black;
    private JButton red;
    private JButton green;
    private JButton blue;
    private JButton upload;
    public void init() {
        setLayout(new BorderLayout());
        dp = new DrawablePicture();
        getContentPane().add(dp, BorderLayout.CENTER);
        JPanel btns = new JPanel();
        btns.setLayout(new GridLayout(1,5));
        black = new JButton("Black");
        black.setForeground(Color.black);
        black.addActionListener(this);
        btns.add(black);
        red = new JButton("Red");
        red.setForeground(Color.RED);
        red.addActionListener(this);
        btns.add(red);
        green = new JButton("Green");
        green.setForeground(Color.GREEN);
        green.addActionListener(this);
        btns.add(green);
        blue = new JButton("Blue");
        blue.setForeground(Color.BLUE);
        blue.addActionListener(this);
        btns.add(blue);
        upload = new JButton("Upload");
        upload.addActionListener(this);
        btns.add(upload);
        getContentPane().add(btns, BorderLayout.SOUTH);
    }
    public void actionPerformed(ActionEvent ev) {
        if(ev.getSource() == black) {
            dp.setColor(Color.BLACK);
        } else if(ev.getSource() == red) {
            dp.setColor(Color.RED);
        } else if(ev.getSource() == green) {
            dp.setColor(Color.GREEN);
        } else if(ev.getSource() == blue) {
            dp.setColor(Color.BLUE);
        } else if(ev.getSource() == upload) {
            try {
                String filename = this.getParameter("filename");
                URL url = new URL(UPLOAD_URL + "?filename=" + filename);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("POST");
                con.setDoOutput(true);
                OutputStream os = con.getOutputStream();
                ImageIO.write(dp.getImg(), "jpeg", os);
                con.connect();
                if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    JOptionPane.showMessageDialog(this, "Picture uploaded");
                } else {
                    JOptionPane.showMessageDialog(this, "Picture not uploaded");
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Picture not uploaded");
            }
        }
    }
}

class DrawablePicture extends JPanel implements MouseMotionListener {
    private final static String STARTPIC_URL = "http://localhost/bg.jpg";
    private final static int W = 750;
    private final static int H = 250;
    private BufferedImage img;
    private Color drawcol;
    private int lastx;
    private int lasty;
    public DrawablePicture() {
        try {
            img = ImageIO.read(new URL(STARTPIC_URL));
            setPreferredSize(new Dimension(W, H));
            this.addMouseMotionListener(this);
            drawcol = Color.BLACK;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }
    public BufferedImage getImg() {
        return img;
    }
    public void mouseDragged(MouseEvent ev) {
        Graphics g = img.getGraphics();
        g.setColor(drawcol);
        g.drawLine(lastx, lasty, ev.getX(), ev.getY());
        repaint();
        lastx = ev.getX();
        lasty = ev.getY();
    }
    public void mouseMoved(MouseEvent ev) {
        lastx = ev.getX();
        lasty = ev.getY();
    }
    public void setColor(Color drawcol) {
        this.drawcol = drawcol;
    }
}
Avatar billede arne_v Ekspert
14. maj 2011 - 22:06 #2
Den loader et fast billede, brugeren kan tegne med 4 farver paa det og saa sendes det til et upload script som raw POST.
Avatar billede andersbl Nybegynder
15. maj 2011 - 15:28 #3
Hej Arne
Super godt eksempel.
Indeholder rigtig mange ting jeg skal bruge. Jeg har brugt din kode og ændret den lidt så den passer mere til mig behov. Jeg kan ikke helt regne ud om hvordan man gør:
* Når man trækker musen og laver en firkant skal den heletiden vises og ikke kun når musen slippes
* Mulighed for at "undo" og "redo"

Koden:
[code]
package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoableEditSupport;


class DrawablePicture extends JPanel implements MouseMotionListener, MouseListener {
    private final static String STARTPIC_URL = "http://static.bold.dk/img/v2/common/template/logo.gif";
    private final static int W = 750;
    private final static int H = 250;
    private static BufferedImage img;
    private Color drawcol;
    int x1, x2, y1, y2;
    int x, y, w, h;
    boolean isNewRect = true;
    public DrawablePicture() {
        try {
            img = ImageIO.read(new URL(STARTPIC_URL));
            setPreferredSize(new Dimension(W, H));
            this.addMouseMotionListener(this);
            this.addMouseListener(this);
            drawcol = Color.BLACK;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    UndoableEditSupport undoableEditSupport = new UndoableEditSupport(this);
    public void addUndoableEditListener(
              UndoableEditListener undoableEditListener) {
            undoableEditSupport.addUndoableEditListener(undoableEditListener);
          }
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }
    public BufferedImage getImg() {
        return img;
    }
    public static BufferedImage getImgZoom() {
        img = DrawApplet.zoomIn(img, 2);
        return img;
    }
    public static BufferedImage getImgZoomOut() {
        img = DrawApplet.zoomIn(img, -2);
        return img;
  }   
    public void setColor(Color drawcol) {
        this.drawcol = drawcol;
    }
    public void mousePressed( final MouseEvent event ) {
        this.x1 = event.getX();
        this.y1 = event.getY();

        this.isNewRect = true;
    }
 
// handle event when mouse released after dragging
    public void mouseReleased( final MouseEvent event ) {
        this.x2 = event.getX();
        this.y2 = event.getY();
       
        Graphics g = img.getGraphics();
       
        int width = this.x1 - this.x2;
        int height = this.y1 - this.y2;

        this.w = Math.abs( width );
        this.h = Math.abs( height );
        this.x = width < 0 ? this.x1
                : this.x2;
        this.y = height < 0 ? this.y1
                : this.y2;

        if ( !this.isNewRect ) {
                g.drawRect(this.x, this.y, this.w, this.h);
                g.setColor(Color.GREEN);
                g.fillRect(this.x, this.y, this.w, this.h);
        }
        repaint();
    }

    // MouseMotionListener event handlers // handle event when user drags mouse with button pressed
    public void mouseDragged( final MouseEvent event ) {
        this.x2 = event.getX();
        this.y2 = event.getY();
       
        this.isNewRect = false;

        repaint();
    }

    public void mouseClicked(MouseEvent e) {       
    }
    public void mouseEntered( final MouseEvent event ) {
    }
    public void mouseExited( final MouseEvent event ) {;
    }
    public void mouseMoved(MouseEvent arg0) {
    }

}
[/code]
Avatar billede arne_v Ekspert
19. maj 2011 - 03:19 #4
>Når man trækker musen og laver en firkant skal den heletiden vises og ikke kun når musen slippes

Er det ikke et spoergsmaal om at goere noget i mouseMoved?


>Mulighed for at "undo" og "redo"

En maade at lave det paa et er Command pattern.
Avatar billede arne_v Ekspert
25. juni 2011 - 04:15 #5
all set?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester