With scroll bars and zoom
This commit is contained in:
parent
e77f61825e
commit
c1498adc1f
1 changed files with 93 additions and 125 deletions
|
|
@ -20,163 +20,111 @@ import javax.swing.border.*;
|
|||
public class Editor
|
||||
extends JComponent
|
||||
{
|
||||
// Given from input image
|
||||
BufferedImage img;
|
||||
double rotation;
|
||||
double imgScale; // coordinates per bitmap pixel
|
||||
Rectangle imgRec;
|
||||
|
||||
// What we choose
|
||||
double rotation; // radians
|
||||
Rect roi; // Measured in coordinates
|
||||
double scale;
|
||||
Rectangle region;
|
||||
Dimension displaySize;
|
||||
AffineTransform t;
|
||||
int side;
|
||||
|
||||
// other
|
||||
// AffineTransform t;
|
||||
CropFrame mask;
|
||||
|
||||
Rectangle currentRect = null;
|
||||
|
||||
Rectangle rectToDraw = null;
|
||||
|
||||
Rectangle previousRectDrawn = new Rectangle();
|
||||
|
||||
public Editor(BufferedImage img) {
|
||||
this.img = img;
|
||||
|
||||
rotation = 0; // 135.0;
|
||||
displaySize = new Dimension(480, 300);
|
||||
int w = img.getWidth();
|
||||
int h = img.getHeight();
|
||||
imgScale = 1d / Math.sqrt(w * w + h * h);
|
||||
imgRec = new Rectangle(0, 0, w, h);
|
||||
|
||||
scale = Math.min(displaySize.width, displaySize.height)
|
||||
/ Math.sqrt(w * w + h * h);
|
||||
resize(imgScale * 680);
|
||||
/*
|
||||
Dimension size = new Dimension(680, 680);
|
||||
setPreferredSize(size);
|
||||
setMinimumSize(size);
|
||||
setMaximumSize(size);
|
||||
|
||||
t = new AffineTransform();
|
||||
t.translate(displaySize.width/2.0, displaySize.height/2.0);
|
||||
t.scale(scale, scale);
|
||||
t.translate(-w/2.0, -h/2.0);
|
||||
Point topLeft = new Point(0, 0);
|
||||
Point topLeft1 = new Point();
|
||||
Point bottomRight = new Point(w, h);
|
||||
Point bottomRight2 = new Point();
|
||||
t.transform(topLeft, topLeft1);
|
||||
t.transform(bottomRight, bottomRight2);
|
||||
Dimension d = new Dimension((int)Math.round(bottomRight2.getX() - topLeft1.getX() + 1),
|
||||
(int)Math.round(bottomRight2.getY() - topLeft1.getY() + 1));
|
||||
Rectangle imgRec = new Rectangle(topLeft1, d);
|
||||
roi = new Rect();
|
||||
roi.top = 0.5;
|
||||
roi.bottom = -0.5;
|
||||
roi.left = -0.5;
|
||||
roi.right = 0.5;
|
||||
|
||||
log("rect: " + imgRec);
|
||||
|
||||
rotation = 0.0;
|
||||
|
||||
Rectangle initialMask = new Rectangle(100, 100, 200, 200);
|
||||
mask = new CropFrame();
|
||||
add(mask);
|
||||
mask.setBounds(imgRec);
|
||||
mask.setBounds(initialMask);
|
||||
mask.setVisible(true);
|
||||
|
||||
setPreferredSize(displaySize);
|
||||
MyListener ml = new MyListener();
|
||||
addMouseListener(ml);
|
||||
addMouseMotionListener(ml);
|
||||
|
||||
|
||||
mask.addMouseListener(mask);
|
||||
mask.addMouseMotionListener(mask);
|
||||
add(mask);
|
||||
|
||||
SelectionEditor ml = new SelectionEditor();
|
||||
addMouseListener(ml);
|
||||
addMouseMotionListener(ml);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
AffineTransform render() {
|
||||
int w = img.getWidth();
|
||||
int h = img.getHeight();
|
||||
void resize(double newScale) {
|
||||
|
||||
scale = Math.min(displaySize.width, displaySize.height)
|
||||
/ Math.sqrt(w * w + h * h);
|
||||
|
||||
t = new AffineTransform();
|
||||
|
||||
t.translate(displaySize.width/2.0, displaySize.height/2.0);
|
||||
t.scale(scale, scale);
|
||||
t.rotate(rotation * Math.PI / 180);
|
||||
t.translate(-w/2.0, -h/2.0);
|
||||
|
||||
return t;
|
||||
scale = newScale;
|
||||
side = (int)Math.round(scale / imgScale);
|
||||
Dimension size = new Dimension(side, side);
|
||||
setSize(size);
|
||||
setPreferredSize(size);
|
||||
setMinimumSize(size);
|
||||
setMaximumSize(size);
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
VolatileImage dst;
|
||||
|
||||
protected void paintComponent(Graphics g1) {
|
||||
super.paintComponent(g1);
|
||||
|
||||
if (getWidth() != displaySize.width
|
||||
|| getHeight() != displaySize.height
|
||||
|| t == null || dst == null)
|
||||
{
|
||||
displaySize.width = getWidth();
|
||||
displaySize.height = getHeight();
|
||||
AffineTransform dxf = render();
|
||||
Graphics2D g = (Graphics2D)g1;
|
||||
AffineTransform orig = g.getTransform();
|
||||
|
||||
AffineTransform screenToCoord = new AffineTransform();
|
||||
screenToCoord.translate(side/2d, side/2d);
|
||||
screenToCoord.scale(side, side);
|
||||
|
||||
g.transform(screenToCoord);
|
||||
|
||||
AffineTransform imageToCoord = new AffineTransform();
|
||||
imageToCoord.rotate(rotation * Math.PI / 180d);
|
||||
imageToCoord.scale(imgScale * scale, imgScale * scale);
|
||||
imageToCoord.translate(-img.getWidth()/2d, -img.getHeight()/2d);
|
||||
|
||||
g.drawImage(img, imageToCoord, this);
|
||||
|
||||
// Crosshair
|
||||
g.setStroke(new BasicStroke(0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1f, new float[] { 0.005f, 0.005f }, 0));
|
||||
g.draw(new Line2D.Double(-0.5, 0, 0.5, 0));
|
||||
g.draw(new Line2D.Double(0, -0.5, 0, 0.5));
|
||||
|
||||
g.setTransform(orig);
|
||||
|
||||
g.setXORMode(Color.white); //Color of line varies
|
||||
g.setStroke(new BasicStroke(0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1f, new float[] { 5f, 5f }, 0));
|
||||
g.draw(new Rectangle2D.Double(100, 100, 200, 200));
|
||||
|
||||
|
||||
dst = createVolatileImage(getWidth(), getHeight());
|
||||
Graphics2D g = dst.createGraphics();
|
||||
g.drawRenderedImage(img, dxf);
|
||||
}
|
||||
g1.drawImage(dst, 0, 0, dst.getWidth(), dst.getHeight(), this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void drawWireframe(Graphics2D g, int w, int h) {
|
||||
double delta = Math.round(Math.sqrt(w * w + h * h) / 20);
|
||||
//
|
||||
// 0------------4------------1
|
||||
// | /|\ |
|
||||
// | 6 | 7 |
|
||||
// | | |
|
||||
// | 5 |
|
||||
// | 8 |
|
||||
// | | |
|
||||
// | 10-+-11 |
|
||||
// | | |
|
||||
// | 9 |
|
||||
// | |
|
||||
// | |
|
||||
// | |
|
||||
// | |
|
||||
// 3-------------------------2
|
||||
double[] wireFramePoints = new double[] {
|
||||
0, 0,
|
||||
w, 0,
|
||||
w, h,
|
||||
0, h,
|
||||
w/2, 0,
|
||||
w/2, 3 * delta,
|
||||
w/2 - delta, delta,
|
||||
w/2 + delta, delta,
|
||||
w/2, h/2 - delta,
|
||||
w/2, h/2 + delta,
|
||||
w/2 - delta, h/2,
|
||||
w/2 + delta, h/2
|
||||
};
|
||||
|
||||
float[] screen = new float[wireFramePoints.length];
|
||||
int[] x = new int[wireFramePoints.length / 2];
|
||||
int[] y = new int[wireFramePoints.length / 2];
|
||||
|
||||
t.transform(wireFramePoints, 0, screen, 0, x.length);
|
||||
|
||||
for (int i = 0; i < x.length; ++i) {
|
||||
x[i] = Math.round(screen[2*i]);
|
||||
y[i] = Math.round(screen[2*i + 1]);
|
||||
}
|
||||
g.setColor(Color.BLUE);
|
||||
|
||||
g.drawPolygon(x, y, 4);
|
||||
g.drawLine(x[4], y[4], x[5], y[5]);
|
||||
g.drawLine(x[4], y[4], x[6], y[6]);
|
||||
g.drawLine(x[4], y[4], x[7], y[7]);
|
||||
g.drawLine(x[8], y[8], x[9], y[9]);
|
||||
g.drawLine(x[10], y[10], x[11], y[11]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class MyListener extends MouseInputAdapter {
|
||||
private class SelectionEditor extends MouseInputAdapter {
|
||||
|
||||
public void mousePressed(MouseEvent e) {
|
||||
int x = e.getX();
|
||||
|
|
@ -224,8 +172,6 @@ public class Editor
|
|||
if (value != previousValue) {
|
||||
previousValue = value;
|
||||
rotation = value;
|
||||
t = null;
|
||||
// render();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
|
@ -240,7 +186,6 @@ public class Editor
|
|||
boolean value = renderQualityControl.isSelected();
|
||||
if (value != previousValue) {
|
||||
previousValue = value;
|
||||
render();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
|
@ -248,6 +193,23 @@ public class Editor
|
|||
hp.add(renderQualityControl);
|
||||
|
||||
|
||||
hp.add(new JLabel("Zoom"));
|
||||
final JSlider zoomSlider = new JSlider(1, 300, (int) Math.round(scale * 100));
|
||||
zoomSlider.setPaintTicks(true);
|
||||
zoomSlider.setMajorTickSpacing(10);
|
||||
zoomSlider.addChangeListener(new ChangeListener() {
|
||||
int previousValue = 0;
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
int value = zoomSlider.getValue();
|
||||
if (value != previousValue) {
|
||||
previousValue = value;
|
||||
resize(value * 0.01);
|
||||
}
|
||||
}
|
||||
});
|
||||
hp.add(zoomSlider);
|
||||
|
||||
|
||||
hp.add(new JPanel());
|
||||
|
||||
return hp;
|
||||
|
|
@ -276,7 +238,7 @@ public class Editor
|
|||
Container c = frame.getContentPane();
|
||||
c.setLayout(new BorderLayout());
|
||||
c.add(x.createControlPanel(), BorderLayout.EAST);
|
||||
c.add(x, BorderLayout.CENTER);
|
||||
c.add(new JScrollPane(x), BorderLayout.CENTER);
|
||||
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
|
|
@ -468,6 +430,12 @@ public class Editor
|
|||
}
|
||||
|
||||
|
||||
static class Rect {
|
||||
double top;
|
||||
double left;
|
||||
double bottom;
|
||||
double right;
|
||||
}
|
||||
}
|
||||
|
||||
// eof
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue