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:
e la uso così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 width, int height) {
this.width = width;
this.height = height;
bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
gImg = bImg.getGraphics();
bgColor = Color.WHITE;
imgPos = new Point(0, 0);
redimType = REDIM_CENTER;
}
public void clear() {
gImg.setColor(bgColor);
gImg.clearRect(0, 0, width, height);
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(gec, 0);
try {
med.waitForAll();
} catch (Exception e) {
result = false;
}
gImg.drawImage(gec, 0, 0, this);
if(doRepaint)
this.repaint();
return result;
}
private BufferedImage redimImage(BufferedImage img, int 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(newX, newY);
}
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(newX, 0);
}
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(0, newY);
}
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)imgHeight, img.getType());
Graphics2D g = ret.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(imgWidth, imgHeight);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawImage(img, at, null);
g.dispose();
return ret;
}
@Override
public void paint(Graphics g) {
g.drawImage(bImg, imgPos.x, imgPos.y, this);
}
}
grazie in anticipoCodice PHP:JPictureBox2 pic = new JPictureBox2(300, 200);
this.getContentPane().add(pic);
pic.clear();
pic.loadImage("D:\\asso_picche.jpg");
ciao

Rispondi quotando

