public class JPEGEditor extends JFrame
{
public final static Dimension VERTICAL_RIGID_SIZE
= new Dimension(1,3);
public final static Dimension HORIZONTAL_RIGID_SIZE
= new Dimension(3,1);
public void setBufferedImage(BufferedImage bi) {
if (bi == null)
return;
m_bi = bi;
Dimension d = new Dimension(m_bi.getWidth(this),
m_bi.getHeight(this));
setPreferredSize(d);
revalidate();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
g.setColor(getBackground());
g.fillRect(0, 0, d.width, d.height);
if (m_bi != null)
g.drawImage(m_bi, 0, 0, this);
}
public BufferedImage getBufferedImage() {
return m_bi;
}
public int print(Graphics pg, PageFormat pageFormat,
int pageIndex) throws PrinterException {
if (pageIndex >= m_maxNumPage || m_bi == null)
return NO_SUCH_PAGE;
pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();
int w = m_bi.getWidth(this);
int h = m_bi.getHeight(this);
if (w == 0 || h == 0)
return NO_SUCH_PAGE;
int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);
int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);
m_maxNumPage = nCol*nRow;
int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int x = iCol*wPage;
int y = iRow*hPage;
int wImage = Math.min(wPage, w-x);
int hImage = Math.min(hPage, h-y);
class PreviewContainer extends JPanel
{
protected int H_GAP = 16;
protected int V_GAP = 10;
public Dimension getPreferredSize() {
int n = getComponentCount();
if (n == 0)
return new Dimension(H_GAP, V_GAP);
Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;
Dimension dp = getParent().getSize();
int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
int nRow = n/nCol;
if (nRow*nCol < n)
nRow++;
int ww = nCol*(w+H_GAP) + H_GAP;
int hh = nRow*(h+V_GAP) + V_GAP;
Insets ins = getInsets();
return new Dimension(ww+ins.left+ins.right,
hh+ins.top+ins.bottom);
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public void doLayout() {
Insets ins = getInsets();
int x = ins.left + H_GAP;
int y = ins.top + V_GAP;
int n = getComponentCount();
if (n == 0)
return;
Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;
Dimension dp = getParent().getSize();
int nCol = Math.max((dp.width-H_GAP)/(w+H_GAP), 1);
int nRow = n/nCol;
if (nRow*nCol < n)
nRow++;
int index = 0;
for (int k = 0; k<nRow; k++) {
for (int m = 0; m<nCol; m++) {
if (index >= n)
return;
comp = getComponent(index++);
comp.setBounds(x, y, w, h);
x += w+H_GAP;
}
y += h+V_GAP;
x = ins.left + H_GAP;
}
}
}
class PagePreview extends JPanel
{
protected int m_w;
protected int m_h;
protected Image m_source;
protected Image m_img;