ciao,
sto sviluppando una picturebox, simile a quella di visual basic per intenderci...
sto cercando di modificare quella fatta da Mehmet Hakan Satman (qui il link)
ma ho qualche problema:
  • Come ridimensiono l'immagine?
  • Come posiziono l'immagine?
  • Quando viene chiamato il paint (chiama lo stesso metodo anche il repaint?)


questo è il mio codice:
Codice PHP:
/**
 * JPictureBox
 * 
 * @author Ilo
 * @version 1.0
 * @info Inspired on Java PictureBox Control 1.0 by Mehmet Hakan Satman
 */

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.net.URL;

import com.msmfc.exceptions.MSMFCImageException;

public class 
JPictureBox2 extends Canvas {

    private static final 
long serialVersionUID 1L;
    
    
// constants
    
public final int REDIM_CENTER 1;
    public final 
int REDIM_FILL 2;
    public final 
int REDIM_FIX 3;
    public final 
int REDIM_EXTEND 4;
    
    private 
BufferedImage bImg;
    private 
Graphics gImg;
    private 
int width;
    private 
int height;
    private 
boolean doRepaint true;
    private 
Color bgColor;
    private 
Point imgPos;
    private 
int redimType;
    
    public 
JPictureBox2(int widthint height) {
        
this.width width;
        
this.height height;
        
        
bImg = new BufferedImage(widthheightBufferedImage.TYPE_INT_RGB);
        
gImg bImg.getGraphics();
        
        
bgColor Color.WHITE;
        
imgPos = new Point(00);
        
redimType REDIM_CENTER;
    }
    
    public 
void clear() {
        
gImg.setColor(bgColor);
        
gImg.clearRect(00widthheight);
        if(
doRepaint)
            
this.repaint();
    }
    
    public 
boolean loadImage(String filename) {
        
boolean result true;
        
URL url null;
        
        try {
            
url = new URL("file:" filename);
        } catch (
Exception e) {
            
result false;
        }
        
        
MediaTracker med = new MediaTracker(this);
        
Image gec Toolkit.getDefaultToolkit().getImage(url);
        
med.addImage(gec0);
        
        try {
            
med.waitForAll();
        } catch (
Exception e) {
            
result false;
        }
        
gImg.drawImage(gec00this);
        
        if(
doRepaint)
            
this.repaint();
        
        return 
result;
    }
    
    private 
BufferedImage redimImage(BufferedImage imgint redimType) {
        
double imgWidth img.getWidth();
        
double imgHeight img.getHeight();
        
        switch(
redimType) {
            case 
REDIM_CENTER:
                if((
img.getHeight() < this.getHeight()) && (img.getWidth() < this.getWidth())) {
                    
// center the image
                    
int newX = (this.getWidth() - img.getWidth()) / 2;
                    
int newY = (this.getHeight() - img.getHeight()) / 2;
                    
                    
redimPos = new Point(newXnewY);
                }
                else {
                    
// redim with FIX
                    // H : W = nH : nW
                    
if(img.getHeight() >= img.getWidth()) {
                        
// W = (H * nW) / nH
                        
imgHeight this.getHeight();
                        
imgWidth = (imgHeight this.getWidth()) / this.getHeight();
                    }
                    else {
                        
// H = (W * nH) / nW
                        
imgWidth this.getWidth();
                        
imgHeight = (imgWidth this.getHeight()) / this.getWidth();
                    }
                }
                
                break;
            case 
REDIM_FILL:
                
// H : W = nH : nW
                
if(img.getHeight() < img.getWidth()) {
                    
// W = (H * nW) / nH
                    
imgHeight this.getHeight();
                    
imgWidth = (imgHeight this.getWidth()) / this.getHeight();
                    
                    
int newX = (-1) * ((img.getWidth() - this.getWidth()) /2);
                    
                    
redimPos = new Point(newX0);
                }
                else {
                    
// H = (W * nH) / nW
                    
imgWidth this.getWidth();
                    
imgHeight = (imgWidth this.getHeight()) / this.getWidth();

                    
int newY = (-1) * ((img.getHeight() - this.getHeight()) /2);
                    
                    
redimPos = new Point(0newY);
                }
                
                break;
            case 
REDIM_FIX:
                
// H : W = nH : nW
                
if(img.getHeight() >= img.getWidth()) {
                    
// W = (H * nW) / nH
                    
imgHeight this.getHeight();
                    
imgWidth = (imgHeight this.getWidth()) / this.getHeight();
                }
                else {
                    
// H = (W * nH) / nW
                    
imgWidth this.getWidth();
                    
imgHeight = (imgWidth this.getHeight()) / this.getWidth();
                }
                
                break;
            case 
REDIM_EXTEND:
                
imgHeight this.getHeight();
                
imgWidth this.getWidth();
                
                break;
        }    
        
        
BufferedImage ret = new BufferedImage((int)imgWidth, (int)imgHeightimg.getType());

        
Graphics2D g ret.createGraphics();
        
AffineTransform at AffineTransform.getScaleInstance(imgWidthimgHeight);
        
g.setRenderingHint(RenderingHints.KEY_INTERPOLATIONRenderingHints.VALUE_INTERPOLATION_BICUBIC);
        
g.drawImage(imgatnull);
        
g.dispose();
        
        return 
ret;
    }

    @
Override
    
public void paint(Graphics g) {
        
g.drawImage(bImgimgPos.ximgPos.ythis);
    }


e la uso così

Codice PHP:
JPictureBox2 pic = new JPictureBox2(300200);
this.getContentPane().add(pic);

pic.clear();
pic.loadImage("D:\\asso_picche.jpg"); 
grazie in anticipo
ciao