Program do skalowania obrazu - jak skalować ramkę?

0

Witam,
tworzę program który będzie skalował obrazek. Jednak mam problem ponieważ nie wiem jak skalować ramkę.
Będę wdzięczny za pomoc.

package kadrtest2;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
 
public class Cropping extends JPanel 
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	BufferedImage image;
    Dimension size;
    Rectangle clip;
    boolean showClip;
    static File file=null;
    static Cropping test ;
    static ClipMover mover;
    static int a=1;
    static JFrame f = new JFrame();
    static BufferedImage clipped = null;

    public Cropping(BufferedImage image)
    {
        this.image = image;
        size = new Dimension(image.getWidth(), image.getHeight());
        showClip = true;
    }
 
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int x = (getWidth() - size.width)/2;
        int y = (getHeight() - size.height)/2;
        g2.drawImage(image, x, y, this);
        if(showClip)
        {
            if(clip == null)
                createClip();
            g2.setPaint(Color.blue);
            
            g2.draw(clip);
        }
    }
 
    public void setClip(int x, int y)
    {
        int x0 = (getWidth() - size.width)/2;
        int y0 = (getHeight() - size.height)/2;
        if(x < x0 || x + clip.width  > x0 + size.width ||
           y < y0 || y + clip.height > y0 + size.height)
            return;
        clip.setLocation(x, y);
        repaint();
    }
 
    public Dimension getPreferredSize()
    {
        return size;
    }
 
    private void createClip()
    {
        clip = new Rectangle(236, 295);
        clip.x = (getWidth() - clip.width)/2;
        clip.y = (getHeight() - clip.height)/2;
    }
 
    private void clipImage()
    {
        try
        {
            int w = clip.width;
            int h = clip.height;
            int x0 = (getWidth()  - size.width)/2;
            int y0 = (getHeight() - size.height)/2;
            int x = clip.x - x0;
            int y = clip.y - y0;
            clipped = image.getSubimage(x, y, w, h);
        }
        catch(RasterFormatException rfe)
        {
            System.out.println("raster format error: " + rfe.getMessage());
            return;
        }
        JLabel label = new JLabel(new ImageIcon(clipped));
        JOptionPane.showMessageDialog(this, label, "Przycięty obraz",
                                      JOptionPane.PLAIN_MESSAGE);
    }
 
    private JPanel getUIPanel()
    {
    	JFileChooser chooser = new JFileChooser();
        final JCheckBox clipBox = new JCheckBox("Pokaż ramkę", showClip);
        clipBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                showClip = clipBox.isSelected();
                repaint();
            }
        });
        JButton clip = new JButton("Przytnij");
        clip.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                clipImage();
            }
        });
        JButton wczytaj = new JButton("Wczytaj");
        wczytaj.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
            	if (e.getSource()==wczytaj){
            		chooser.showOpenDialog(null);
            		 file = chooser.getSelectedFile();
            		 try {
						test = new Cropping(ImageIO.read(file));
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
                     mover = new ClipMover(test);
                    test.addMouseListener(mover);
                    test.addMouseMotionListener(mover);
                    JFrame f2 = new JFrame();
                    Toolkit tk = Toolkit.getDefaultToolkit();  
                    int xSize = ((int) tk.getScreenSize().getWidth());  
                    int ySize = ((int) tk.getScreenSize().getHeight());  
                    
                    f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f2.getContentPane().add(new JScrollPane(test));
                    f2.getContentPane().add(test.getUIPanel(), "South");
                    f2.setSize(image.getWidth(),image.getHeight());
                    f2.setSize(xSize,ySize-30); 
                    f2.setLocation(0,0);
                    f2.setVisible(true);
                    f.setVisible(false);
            	}
            }
        });
        JButton zapisz = new JButton("Zapisz");
        zapisz.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
            	  try{ 
            		   File f = new File("skadrowany.jpg"); 
            		       
            		        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); 
            		        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
            		        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(clipped); 
            		        param.setQuality(1f, true); 
            		    
            		        encoder.setJPEGEncodeParam(param); 
            		            
            		        encoder.encode(clipped); 
            		        out.close(); 
            		        }catch(Exception ex){ 
            		           JOptionPane.showMessageDialog(null,"Problem z zapisem"+ex, "Informacja", 
            		                                    JOptionPane.INFORMATION_MESSAGE); 
            		       } 
            }
        });
        JPanel panel = new JPanel();
        panel.add(clipBox);
        panel.add(clip);
        panel.add(wczytaj);
        panel.add(zapisz);
        return panel;
    }
 
    public static void main(String[] args) throws IOException
    {
    	File file = new File("images/grafika.jpg");
         test = new Cropping(ImageIO.read(file));
         mover = new ClipMover(test);
        test.addMouseListener(mover);
        test.addMouseMotionListener(mover);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(test));
        f.getContentPane().add(test.getUIPanel(), "South");
        f.setSize(500,500);
        f.setLocation(0,0);
        f.setVisible(true);
    }
}

class ClipMover extends MouseInputAdapter
{
    Cropping cropping;
    Point offset;
    boolean dragging;
 
    public ClipMover(Cropping c)
    {
        cropping = c;
        offset = new Point();
        dragging = false;
    }
 
    public void mousePressed(MouseEvent e)
    {
        Point p = e.getPoint();
        if(cropping.clip.contains(p))
        {
            offset.x = p.x - cropping.clip.x;
            offset.y = p.y - cropping.clip.y;
            dragging = true;
        }
    }
 
    public void mouseReleased(MouseEvent e)
    {
        dragging = false;
    }
 
    public void mouseDragged(MouseEvent e)
    {
        if(dragging)
        {
            int x = e.getX() - offset.x;
            int y = e.getY() - offset.y;
            cropping.setClip(x, y);
        }
    }
}
0

To może chociaż ktoś mi podpowie jak zeskalować obraz bez skalowania ramki. Chodzi mi po prostu o to żeby program działał.

1 użytkowników online, w tym zalogowanych: 0, gości: 1