package ui;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.SwingConstants;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.MediaTracker;
import java.awt.image.WritableRaster;
public class JImage extends JComponent implements SwingConstants {
private BufferedImage img;
private int mode;
private int halign;
private int valign;
private Dimension preferredSize;
public static final int SCALE_NONE = 0;
public static final int SCALE_HORIZONTAL = 1;
public static final int SCALE_VERTICAL = 2;
public static final int SCALE_BOTH = 3;
public static final int SCALE_PROPORTIONAL = 4;
public JImage()
{
this.img = null;
mode = SCALE_NONE;
this.halign = CENTER;
this.valign = CENTER;
this.preferredSize = null;
}
public JImage(Image img)
{
this();
this.setImage(img);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Insets i = this.getInsets();
int cWidth = this.getWidth() - (i.left + i.right);
int cHeight = this.getHeight() - (i.top + i.bottom);
g.setColor(this.getBackground());
g.fillRect(i.left, i.top, cWidth, cHeight);
// Do nothing if the image is null
if(img == null)
return;
int width = 0;
int height = 0;
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
if(imgWidth > 0 && imgHeight > 0)
{
switch(this.mode)
{
case SCALE_NONE:
width = imgWidth;
height = imgHeight;
break;
case SCALE_HORIZONTAL:
width = cWidth;
height = imgHeight;
break;
case SCALE_VERTICAL:
width = imgWidth;
height = cHeight;
break;
case SCALE_BOTH:
width = cWidth;
height = cHeight;
break;
case SCALE_PROPORTIONAL:
float ratio = (float)imgWidth / imgHeight;
float cRatio = (float)cWidth / cHeight;
if(ratio > cRatio)
{
width = cWidth;
height = (int)(cWidth / ratio);
}
else if(ratio < cRatio)
{
width = (int)(cHeight * ratio);
height = cHeight;
}
else
{
width = cWidth;
height = cHeight;
}
break;
default:
// Unknown mode; do nothing
}
boolean l2r = this.getComponentOrientation().isHorizontal();
int x = 0;
int y = 0;
// Do alignment if necessary
switch(this.halign)
{
case LEFT:
x = 0;
break;
case CENTER:
x = (cWidth - width) / 2;
break;
case RIGHT:
x = cWidth - width;
break;
case LEADING:
if(!l2r)
x = cWidth - width;
break;
case TRAILING:
if(l2r)
x = cWidth - width;
break;
default:
}
switch(this.valign)
{
case TOP:
y = 0;
break;
case CENTER:
y = (cHeight - height) / 2;
break;
case BOTTOM:
y = cHeight - height;
break;
default:
}
Image img = this.img;
if(width != imgWidth || height != imgHeight)
{
img = this.img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
return;
}
tracker.removeImage(img);
}
// Draw actual image
g.drawImage(img, x + i.left, y + i.top, null);
}
}
public void setScalingMode(int mode)
{
if(mode != SCALE_NONE &&
mode != SCALE_HORIZONTAL &&
mode != SCALE_VERTICAL &&
mode != SCALE_BOTH &&
mode != SCALE_PROPORTIONAL)
throw new IllegalArgumentException("mode");
if(this.mode != mode)
{
this.mode = mode;
if(this.isDisplayable())
this.repaint();
}
}
public int getScalingMode()
{
return this.mode;
}
public void setImage(Image img)
{
boolean repaint = this.isDisplayable();
if(img == null)
{
if(this.img == null)
repaint = false;
else
this.img.flush();
this.img = null;
}
else
{
if(img.equals(this.img))
repaint = false;
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
return;
}
tracker.removeImage(img);
int width = img.getWidth(null);
int height = img.getHeight(null);
if(img instanceof BufferedImage)
{
BufferedImage source = (BufferedImage)img;
WritableRaster raster = source.copyData(null);
this.img = new BufferedImage(
source.getColorModel(),
raster,
source.isAlphaPremultiplied(),
null);
}
else
{
this.img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
Graphics bg = this.img.getGraphics();
bg.drawImage(img, 0, 0, null);
bg.dispose();
}
if(width > 0 && height > 0)
{
Insets i = this.getInsets();
super.setPreferredSize(
new Dimension(width + i.left + i.right,
height + i.top + i.bottom));
}
}
if(repaint)
this.repaint();
}
public void setBorder(Border border)
{
super.setBorder(border);
if(this.img != null)
{
int width = img.getWidth(null);
int height = img.getHeight(null);
if(width > 0 && height > 0)
{
Insets i = this.getInsets();
super.setPreferredSize(new Dimension(width + i.left + i.right, height + i.top + i.bottom));
}
}
}
public void setHorizontalAlignment(int alignment)
{
this.halign = alignment;
if(this.img != null && this.isDisplayable())
this.repaint();
}
public int getHorizontalAlignment()
{
return this.halign;
}
public void setVerticalAlignment(int alignment)
{
this.valign = alignment;
if(this.img != null && this.isDisplayable())
this.repaint();
}
public int getVerticalAlignment()
{
return this.valign;
}
public void setPreferredSize(Dimension preferredSize)
{
// Insets i = this.getInsets();
this.preferredSize = preferredSize;
// this.preferredSize.width -= i.left + i.right;
// this.preferredSize.height -= i.top + i.bottom;
}
public Dimension getPreferredSize()
{
if(this.preferredSize == null)
return super.getPreferredSize();
// Insets i = this.getInsets();
//
// Dimension prefSize = new Dimension();
// prefSize.width = this.preferredSize.width + i.left + i.right;
// prefSize.height = this.preferredSize.height + i.top + i.bottom;
//
// return prefSize;
return this.preferredSize;
}
}

댓글을 달아 주세요
내가 누군지 맞춰보라능 우어어어어어어
힌트는 이미 주어졌다!!