Click here to Skip to main content
15,899,754 members
Home / Discussions / Java
   

Java

 
AnswerRe: java Pin
Paul Conrad8-Jun-12 8:15
professionalPaul Conrad8-Jun-12 8:15 
Questionproblem with jarsigner Pin
ahmadiss6-Jun-12 23:12
ahmadiss6-Jun-12 23:12 
AnswerRe: problem with jarsigner Pin
Richard MacCutchan6-Jun-12 23:24
mveRichard MacCutchan6-Jun-12 23:24 
GeneralRe: problem with jarsigner Pin
ahmadiss6-Jun-12 23:52
ahmadiss6-Jun-12 23:52 
GeneralMessage Removed Pin
6-Jun-12 23:56
ahmadiss6-Jun-12 23:56 
GeneralRe: problem with jarsigner Pin
Richard MacCutchan7-Jun-12 4:57
mveRichard MacCutchan7-Jun-12 4:57 
GeneralRe: problem with jarsigner Pin
ahmadiss7-Jun-12 7:20
ahmadiss7-Jun-12 7:20 
Questionwhy i cant save the image? Pin
aelynne2-Jun-12 17:42
aelynne2-Jun-12 17:42 
this is my code. what wrong with my code? i want to save the image on the canvas as a png picture.

Java
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.colorchooser.*;
import javax.swing.event.*;
import java.awt.geom.Line2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.imageio.ImageIO;
 
 
public class PaintIt extends JFrame implements ActionListener{
 
 
     
public BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
//JPanel canvas = new JPanel();
     
    JPanel buttonPanel = new JPanel();
    Point lastPos = null;
    Point startPos = null;
    Point finishPos = null;
    Graphics g;
    JButton save = new JButton("Save");
    JButton cancel = new JButton("Cancel");
    JButton clear = new JButton("Clear");
    JButton proceed = new JButton("Proceed");
    JPanel canvas = new JPanel();
    int changer = 1;
    String path="";
     
    public PaintIt () {
 
         
        setLocation(100,100);
        setSize(600,500);
        setTitle("ENCODE SECTION");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvas.setBackground(Color.WHITE);
         
        clear.addActionListener(this);
        clear.setActionCommand("clear");
         
        save.addActionListener(this);
        save.setActionCommand("Save");
        
               
        cancel.addActionListener(this);
        cancel.setActionCommand("Cancel");
        
        proceed.addActionListener(this);
        proceed.setActionCommand("Proceed");
         
        //add buttons here
        
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(save);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPanel.add(clear);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPanel.add(cancel);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPanel.add(proceed);
        
        //set the look
        getContentPane().add(canvas, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        setVisible(true);
         
        g = canvas.getGraphics();
        g.setColor(Color.BLACK);
 
        canvas.addMouseMotionListener(new MouseMotionListener () {                      
            public void mouseDragged (MouseEvent m) {
                Point p = m.getPoint() ;
                if (changer==1){
                g.drawLine(lastPos.x, lastPos.y, p.x, p.y) ;
                }           
                lastPos = p ;
 
            }   
            public void mouseMoved (MouseEvent m) {}
        });
                         
        canvas.addMouseListener(new MouseListener () {
            public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
            public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
            public void mouseReleased(MouseEvent e) {
            lastPos = null; 
            finishPos = e.getPoint(); 
            startPos = null;}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
            });
     
         
    }
     
     
     
    public void actionPerformed(ActionEvent e) {
        if("clear".equals(e.getActionCommand())) {
            repaint();
        }
        if("Save".equals(e.getActionCommand())) {
            new captureCanvasImage();
        }
        if("Cancel".equals(e.getActionCommand())) {
            dispose();
        }
        if("Proceed".equals(e.getActionCommand())) {
            new encode1Pane();
        }
         
             
    }
     
    class captureCanvasImage {
        public void capture(){
        Graphics g = image.createGraphics();
        canvas.paint(g);
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        g.dispose();
        try
        {
            ImageIO.write(image, "jpg", new File("panel.jpg"));
        }
        catch(IOException ioe)
        {
            System.out.println("Panel write help: " + ioe.getMessage());
        }
    }}
   

    /*
    @Override
    public void invalidate() {     
    super.invalidate(); 
     
    this.paint(this.getGraphics()); 
    }
*/
     
    public static void main (String [] args) {
        PaintIt p = new PaintIt();
        p.setVisible(true);
    }
}

AnswerRe: why i cant save the image? Pin
Paul Conrad2-Jun-12 17:59
professionalPaul Conrad2-Jun-12 17:59 
GeneralRe: why i cant save the image? Pin
aelynne2-Jun-12 18:02
aelynne2-Jun-12 18:02 
GeneralRe: why i cant save the image? Pin
Paul Conrad2-Jun-12 18:08
professionalPaul Conrad2-Jun-12 18:08 
GeneralRe: why i cant save the image? Pin
aelynne2-Jun-12 18:11
aelynne2-Jun-12 18:11 
GeneralRe: why i cant save the image? Pin
Paul Conrad2-Jun-12 18:15
professionalPaul Conrad2-Jun-12 18:15 
GeneralRe: why i cant save the image? Pin
aelynne2-Jun-12 18:17
aelynne2-Jun-12 18:17 
SuggestionRe: why i cant save the image? Pin
Paul Conrad3-Jun-12 17:31
professionalPaul Conrad3-Jun-12 17:31 
GeneralRe: why i cant save the image? Pin
aelynne3-Jun-12 17:35
aelynne3-Jun-12 17:35 
GeneralRe: why i cant save the image? Pin
Paul Conrad3-Jun-12 17:42
professionalPaul Conrad3-Jun-12 17:42 
GeneralRe: why i cant save the image? Pin
aelynne3-Jun-12 17:44
aelynne3-Jun-12 17:44 
GeneralRe: why i cant save the image? Pin
Paul Conrad3-Jun-12 19:19
professionalPaul Conrad3-Jun-12 19:19 
GeneralRe: why i cant save the image? Pin
aelynne3-Jun-12 19:31
aelynne3-Jun-12 19:31 
GeneralRe: why i cant save the image? [modified] Pin
Paul Conrad3-Jun-12 19:35
professionalPaul Conrad3-Jun-12 19:35 
GeneralRe: why i cant save the image? [modified] Pin
aelynne3-Jun-12 19:43
aelynne3-Jun-12 19:43 
GeneralRe: why i cant save the image? [modified] Pin
Paul Conrad3-Jun-12 19:47
professionalPaul Conrad3-Jun-12 19:47 
GeneralRe: why i cant save the image? [modified] Pin
aelynne3-Jun-12 20:13
aelynne3-Jun-12 20:13 
GeneralRe: why i cant save the image? [modified] Pin
Richard MacCutchan3-Jun-12 22:18
mveRichard MacCutchan3-Jun-12 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.