Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you crop an image?

70 views
Skip to first unread message

phillip....@gmail.com

unread,
Mar 24, 2007, 9:30:16โ€ฏPM3/24/07
to
I thought I got it and again, I failed.

[code]
public void actionPerformed(ActionEvent evt) {
if
(evt.getActionCommand().toLowerCase().trim().equals("crop")) {
cropPanel.setImage(
(BufferedImage)createImage(
new
FilteredImageSource(cropPanel.getImage().getSource(),
new
CropImageFilter((int)cropPanel.getClip().getX(),
(int)cropPanel.getClip().getY(),
(int)cropPanel.getClip().getWidth(),
(int)cropPanel.getClip().getHeight()))));
repaint();
hasCropped = true;
}
}
[/code]

This constantly throws the following exception:

[code]
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
sun.awt.image.ToolkitImage cannot be cast to
java.awt.image.BufferedImage
at
com.ppowell.tools.imagetools.ImageCropper.actionPerformed(ImageCropper.java:
180)
[/code]

I was under the impression that I'm using java.awt.Image, where did
sun.awt.image.ToolkitImage come from? I can't figure out how to cast
this correctly either, what do I do?

Thanx
Phil

hiwa

unread,
Mar 24, 2007, 10:17:08โ€ฏPM3/24/07
to
On Mar 25, 10:30 am, "phillip.s.pow...@gmail.com"

I don't fully understand your code but you should use BufferedImage
and getSubimage() method for getting a sub-image from the original
image.

phillip....@gmail.com

unread,
Mar 24, 2007, 10:24:34โ€ฏPM3/24/07
to


Sorry that also failed:

Exception in thread "AWT-EventQueue-0"
java.awt.image.RasterFormatException: (x + width) is outside of Raster
at
sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:
1230)
at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:
1156)

Using your suggestion:

public void crop() {
ImageCropper.this.cropPanel.setImage(

ImageCropper.this.cropPanel.getImage().getSubimage(
(int)ImageCropper.this.cropPanel.getClip().getX(),
(int)ImageCropper.this.cropPanel.getClip().getY(),

(int)ImageCropper.this.cropPanel.getClip().getWidth(),

(int)ImageCropper.this.cropPanel.getClip().getHeight()));
ImageCropper.this.cropPanel.setup();
ImageCropper.this.validate();
}
}

hiwa

unread,
Mar 24, 2007, 10:36:08โ€ฏPM3/24/07
to
On Mar 25, 11:24 am, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:

> Sorry that also failed:
No. You can't.
Post a small demo code that is generally compilable, runnable and
could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html
and http://www.yoda.arachsys.com/java/newsgroups.html

phillip....@gmail.com

unread,
Mar 24, 2007, 10:44:39โ€ฏPM3/24/07
to

I'm sorry I can't fathom how to make a smaller example of my problem,
I honestly don't know how to do it, and it's 15 pages long. I will
just dump the whole thing. Copy and paste as you see fit and try. I
am truly sorry.

/*
* ImageCropper.java
*
* Created on March 23, 2007, 11:42 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package com.ppowell.tools.imagetools;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;


/**
* ImageCropper an image
*
* @author Phil Powell
* @version JDK 1.6.0
* <a href="http://forum.java.sun.com/thread.jspa?
threadID=627125&messageID=3587532">Reference</a>
*/
public class ImageCropper
extends JFrame
implements ActionListener {
//----------------------------- --* PROPERTIES *--
---------------------------
// <editor-fold defaultstate="collapsed" desc=" Properties ">

/**
* {@link java.lang.Boolean}
*/
private boolean hasCropped = false;
/**
* {@link javax.swing.JTextField}
*/
private JTextField urlField;
/**
* {@link javax.swing.JButton}
*/
private JButton fileChooserButton;
/**
* {@link javax.swing.JButton}
*/
private JButton cropButton;
/**
* {@link javax.swing.JButton}
*/
private JButton restoreButton;
/**
* {@link javax.swing.JButton}
*/
private JButton saveButton;
/**
* {@link javax.swing.JPanel}
*/
private JPanel topPanel;
/**
* {@link javax.swing.JPanel}
*/
private JPanel bottomPanel;

/**
* {@link com.ppowell.tools.imagetools.ImageCropper.CropPanel}
*/
private ImageCropper.CropPanel cropPanel;
/**
* {@link com.ppowell.tools.imagetools.ImageCropper.CropSelector}
*/
private ImageCropper.CropSelector selector;

/**
* {@link com.ppowell.tools.imagetools.ImageCropper.Builder}
*/
private final ImageCropper.Builder builder = new
ImageCropper.Builder();
/**
* {@link com.ppowell.tools.imagetools.ImageCropper.Handler}
*/
private final ImageCropper.Handler handler = new
ImageCropper.Handler();
/**
* {@link com.ppowell.tools.imagetools.ImageCropper.Processor}
*/
private final ImageCropper.Processor processor = new
ImageCropper.Processor();
/**
* {@link com.ppowell.tools.imagetools.ImageCropper.LayoutManager}
*/
private final ImageCropper.LayoutManager manager = new
ImageCropper.LayoutManager();

/**
* {@link java.lang.Integer} default screen width
*/
protected static final int DEFAULT_SCREEN_WIDTH =
(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
/**
* {@link java.lang.Integer} default screen height
*/
protected static final int DEFAULT_SCREEN_HEIGHT =
(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();

// </editor-fold>
//---------------------------- --* CONSTRUCTORS *--
--------------------------
// <editor-fold defaultstate="collapsed" desc=" Constructors ">

/**
* Creates a new instance of ImageCropper
*/
public ImageCropper() {
super();
setup();
}

/**
* Creates a new instance of ImageCropper
*
* @param fileName {@link java.lang.String}
*/
public ImageCropper(String fileName) {
super();
this.fileName = fileName;
setup();
}

/**
* Creates a new instance of ImageCropper
*
* @param url {@link java.net.URL}
*/
public ImageCropper(URL url) {
super();
this.url = url;
setup();
}

/**
* Creates a new instance of ImageCropper
*
* @param file {@link java.io.File}
*/
public ImageCropper(File file) {
super();
this.file = file;
setup();
}

// </editor-fold>
//------------------------ --* GETTER/SETTER METHODS *--
---------------------
// <editor-fold defaultstate="collapsed" desc=" Getter/Setter
Methods ">

/**
* Retrieve {@link java.awt.image.BufferedImage}
* @return image {@link java.awt.image.BufferedImage}
*/
private BufferedImage getImage() {
if (getFileName() == null || getFileName().equals("")) {
setFileName("http://valsignalandet.com/images/
cemetery_potluck_3/flyer.jpg");
}
String fileName = getFileName();
BufferedImage image = null;
try {
URL url = new URL(fileName);
image = ImageIO.read(url);
} catch(MalformedURLException mue) {
System.err.println("url: " + mue.getMessage());
} catch(IOException ioe) {
System.err.println("read: " + ioe.getMessage());
}
return image;
}

// </editor-fold>
//---------------------------- --* OTHER METHODS *--
-------------------------
// <editor-fold defaultstate="collapsed" desc=" Methods ">

public void actionPerformed(ActionEvent evt) {

// SELECT IMAGE FROM URL

setFileName(evt.getActionCommand());
cropPanel.setImage(getImage());
validate();

}

// PERFORM CROP
if
(evt.getActionCommand().toLowerCase().trim().equals("crop")) {
processor.crop();
repaint();
hasCropped = true;
}

if
(evt.getActionCommand().toLowerCase().trim().indexOf("save") == 0) {
// SAVE NEW IMAGE
}
}

/** Initialize all components */
public void initComponents() {
fileChooserButton = new JButton("Select via File");
handler.handleButton(fileChooserButton, 'F');
cropButton = new JButton("Crop");
handler.handleButton(cropButton);
restoreButton = new JButton("Restore");
handler.handleButton(restoreButton);
saveButton = new JButton("Save as:");
handler.handleButton(saveButton);
urlField = new JTextField();
urlField.setText("Enter your image URL here: ");
urlField.addActionListener(this);
cropPanel = new CropPanel(getImage());
topPanel = new JPanel(true);
topPanel.setBackground(Color.WHITE);
bottomPanel = new JPanel(true);
bottomPanel.setBackground(Color.WHITE);
if (getScreenWidth() == 0)
setScreenWidth(ImageCropper.DEFAULT_SCREEN_WIDTH);
if (getScreenHeight() == 0)
setScreenHeight(ImageCropper.DEFAULT_SCREEN_HEIGHT);
setSize(getScreenWidth(), getScreenHeight());
builder.addToPanel();
builder.addToFrame();
}

/** Initialize all objects */
public void initObjects() {
selector = new CropSelector(cropPanel);
builder.addListeners();
}

/** Set up initialization routine */
protected synchronized void setup() {
initComponents();
initObjects();
builder.showFrame();
}
// </editor-fold>
//----------------------------- --* MAIN METHOD *--
--------------------------
// <editor-fold defaultstate="collapsed" desc=" Main Method ">

/**
* Main method
* @param args {@link java.lang.String}
*/
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (args.length > 0) {
new ImageCropper(args[0]);
} else {
new ImageCropper();
}
}
});
}

// </editor-fold>
//------------------------- --* NESTED INNER CLASSES *--
---------------------
// <editor-fold defaultstate="collapsed" desc=" Inner Classes ">

/**
* Build {@link com.ppowell.tools.imagetools.ImageCropper}
*
* @author Phil Powell
* @version JDK 1.6.0
*/
class Builder {

//---------------------------- --* METHODS *--
----------------------------
// <editor-fold defaultstate="collapsed" desc=" Methods ">

/**
* Add to {@link javax.swing.JFrame} descendant {@link
com.ppowell.tools.imagetools.ImageCropper}
*/
public void addToFrame() {
ImageCropper.this.add(topPanel);
ImageCropper.this.add(bottomPanel);
}

/** Add to both {@link #topPanel} and {@link #bottomPanel} */
public void addToPanel() {
ImageCropper.this.manager.layoutTopPanel();
ImageCropper.this.manager.layoutCropPanel();
ImageCropper.this.manager.layoutBottomPanel();
}

/**
* Add all appropriate listeners to {@link
com.ppowell.tools.imagetools.ImageCropper.CropPanel}
*/
private void addListeners() {

ImageCropper.this.cropPanel.addMouseListener(ImageCropper.this.selector);

ImageCropper.this.cropPanel.addMouseMotionListener(ImageCropper.this.selector);
}

/**
* Display {@link javax.swing.JFrame} descendant {@link
com.ppowell.tools.imagetools.ImageCropper}
*/
public void showFrame() {

ImageCropper.this.getContentPane().setBackground(Color.WHITE);

ImageCropper.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageCropper.this.setVisible(true);
}
// </editor-fold>
}

/**
* {@link javax.swing.JPanel} to contain croppable image
* @author Phil Powell
* @version JDK 1.6.0
*/
class CropPanel extends JPanel {
//---------------------------- --* PROPERTIES *--
-----------------------
// <editor-fold defaultstate="collapsed" desc=" Properties ">

/**
* {@link java.awt.image.BufferedImage}
*/
private BufferedImage image;
/**
* {@link java.awt.Dimension}
*/
private Dimension size;
/**
* {@link java.awt.Rectangle}
*/
private Rectangle clip;

// </editor-fold>
//--------------------------- --* CONSTRUCTORS *--
----------------------
// <editor-fold defaultstate="collapsed" desc=" Constructors
">

/**
* Create a new instance of CropPanel
* @param image {@link java.awt.image.BufferedImage}
*/
public CropPanel(BufferedImage image) {
this.image = image;
this.setup();
}

// </editor-fold>
//----------------------- --* GETTER/SETTER METHODS *--
-----------------
// <editor-fold defaultstate="collapsed" desc=" Getter/Setter
Methods ">

/**
* Retrieve {@link #clip}
* @return clip {@link java.awt.Rectangle}
*/
public Rectangle getClip() {
return this.clip;
}

/**
* Retrieve {@link #image}
* @return {@link java.awt.Image}
*/
public BufferedImage getImage() {
return this.image;
}

@Override
/**
* Retrieve {@link #size}
* @return size {@link java.awt.Dimension}
*/
public Dimension getPreferredSize() {
return this.size;
}

/**
* Set {@link #clip}
* @param p1 {@link java.awt.Point}
* @param p2 {@link java.awt.Point}
*/
public void setClip(Point p1, Point p2) {
this.clip.setFrameFromDiagonal(p1, p2);
repaint();
}

/**
* Set {@link #image}
* @param image {@link java.awt.image.BufferedImage}
*/
public void setImage(BufferedImage image) {
this.image = image;
this.setup();
this.repaint();
}
// </editor-fold>
//--------------------------- --* OTHER METHODS *--
---------------------
// <editor-fold defaultstate="collapsed" desc=" Methods ">

/** Initialize all components */
public void initComponents() {
this.size = new Dimension(this.image.getWidth(),
this.image.getHeight());
this.clip = new Rectangle();
}

/** Initialize all objects */
public void initObjects() {

}

@Override
/**
* paintComponent
* @param g {@link java.awt.Graphics}
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int x = (w - this.size.width)/2;
int y = (h - this.size.height)/2;
g2.drawImage(this.image, x, y, this);
g2.setPaint(Color.red);
g2.draw(this.clip);
}

/** Set up initialization routine */
protected synchronized void setup() {
this.initObjects();
this.initComponents();
}
// </editor-fold>
}

/**
* Handling mouse actions to create "selector"
* @author Phil Powell
* @version JDK 1.6.0
*/
class CropSelector extends MouseInputAdapter {
//---------------------------- --* PROPERTIES *--
-----------------------
// <editor-fold defaultstate="collapsed" desc=" Properties ">

/** {@link java.lang.Boolean} */
private boolean hasSelected = false;
/**
* {@link com.ppowell.tools.imagetools.ImageCropper.CropPanel
*/
private CropPanel cropPanel;
/** {@link java.awt.Point} */
private Point start;

// </editor-fold>
//--------------------------- --* CONSTRUCTORS *--
----------------------
// <editor-fold defaultstate="collapsed" desc=" Constructors
">

/**
* Creates a new instance of CropSelector
*
* @param {@link
com.ppowell.tools.imagetools.ImageCropper.CropPanel}
*/
public CropSelector(CropPanel cropPanel) {
this.cropPanel = cropPanel;
}

// </editor-fold>
//------------------------------ --* METHODS *--
------------------------
// <editor-fold defaultstate="collapsed" desc=" Methods ">

@Override
/**
* Perform action upon pressing mouse
* @param evt {@link java.awt.event.MouseEvent}
*/
public void mousePressed(MouseEvent evt) {
if(evt.getClickCount() == 2) {
this.cropPanel.setClip(new Point(-1,-1), new
Point(-1,-1));
this.hasSelected = false;
}
this.start = evt.getPoint();
}

@Override
/**
* Perform action upon dragging mouse
* @param evt {@link java.awt.event.MouseEvent}
*/
public void mouseDragged(MouseEvent evt) {
this.cropPanel.setClip(this.start, evt.getPoint());
this.hasSelected = true;
}
// </editor-fold>
}

/**
* Handle components and objects
* @author Phil Powell
* @version JDK 1.6.0
*/
class Handler {

//----------------------------- --* METHODS *--
---------------------
// <editor-fold defaultstate="collapsed" desc=" Methods ">

/**
* Generic {@link javax.swing.JButton} handling
* @param button {@link javax.swing.JButton}
*/
private void genericButtonHandling(JButton button) {
button.setFocusable(false);
button.setSelected(false);
button.setFont(CropGlobals.FONT);
button.addActionListener(ImageCropper.this);
}

/**
* Handle each {@link javax.swing.JButton}
* @param button {@link javax.swing.JButton}
*/
private void handleButton(JButton button) {
this.genericButtonHandling(button);
button.setMnemonic(button.getText().charAt(0));
}

/**
* Handle each {@link javax.swing.JButton} with custom
mnemonic
* @param button {@link javax.swing.JButton}
* @param letter {@link java.lang.Character}
*/
private void handleButton(JButton button, char letter) {
this.genericButtonHandling(button);
button.setMnemonic(letter);
}
// </editor-fold>
}

/**
* Processor
* @author Phil Powell
* @version JDK 1.6.0
*/
class Processor {

public void crop() {
ImageCropper.this.cropPanel.setImage(

ImageCropper.this.cropPanel.getImage().getSubimage(
(int)ImageCropper.this.cropPanel.getClip().getX(),
(int)ImageCropper.this.cropPanel.getClip().getY(),

(int)ImageCropper.this.cropPanel.getClip().getWidth(),

(int)ImageCropper.this.cropPanel.getClip().getHeight()));
ImageCropper.this.cropPanel.setup();
ImageCropper.this.validate();
}
}

/**
* Layout manager
* @author Phil Powell
* @version JDK 1.6.0
*/
class LayoutManager {
//------------------------------ --* METHODS *--
------------------------
// <editor-fold defaultstate="collapsed" desc=" Methods ">
@Override
/** Layout {@link #bottomPanel} */
public void layoutBottomPanel() {
GroupLayout bottomPanelLayout = new
GroupLayout(ImageCropper.this.bottomPanel);

ImageCropper.this.bottomPanel.setLayout(bottomPanelLayout);
bottomPanelLayout.setHorizontalGroup(

bottomPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(bottomPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(ImageCropper.this.cropPanel,
GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
bottomPanelLayout.setVerticalGroup(

bottomPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(bottomPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(ImageCropper.this.cropPanel,
GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

GroupLayout layout = new
GroupLayout(ImageCropper.this.getContentPane());
ImageCropper.this.getContentPane().setLayout(layout);
layout.setHorizontalGroup(

layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(ImageCropper.this.bottomPanel,
GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ImageCropper.this.topPanel,
GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(

layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(ImageCropper.this.topPanel,
GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ImageCropper.this.bottomPanel,
GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

ImageCropper.this.getContentPane().add(new
JScrollPane(ImageCropper.this.cropPanel));
}

/** Layout {@link #cropPanel} */
public void layoutCropPanel() {
GroupLayout cropPanelLayout = new
GroupLayout(ImageCropper.this.cropPanel);
ImageCropper.this.cropPanel.setLayout(cropPanelLayout);
cropPanelLayout.setHorizontalGroup(

cropPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 390, Short.MAX_VALUE)
);
cropPanelLayout.setVerticalGroup(

cropPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 204, Short.MAX_VALUE)
);
}

@Override
/** Layout {@link #topPanel} */
public void layoutTopPanel() {
JButton whichButton; // TO DETERMINE WHICH JButton YOU
WILL PLACE BASED ON CROPPING ACTION
if (ImageCropper.this.hasCropped) {
whichButton = ImageCropper.this.restoreButton;
} else {
whichButton = ImageCropper.this.cropButton;
}
GroupLayout topPanelLayout = new
GroupLayout(ImageCropper.this.topPanel);
ImageCropper.this.topPanel.setLayout(topPanelLayout);
topPanelLayout.setHorizontalGroup(

topPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(topPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(ImageCropper.this.urlField,
GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ImageCropper.this.fileChooserButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED,
34, Short.MAX_VALUE)
.addComponent(whichButton)
.addGap(5, 5, 5)
.addComponent(ImageCropper.this.saveButton)
.addContainerGap())
);
topPanelLayout.setVerticalGroup(

topPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(topPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(topPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ImageCropper.this.fileChooserButton)
.addComponent(whichButton)
.addComponent(ImageCropper.this.saveButton)
.addComponent(ImageCropper.this.urlField,
GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE))
.addContainerGap(23, Short.MAX_VALUE))
);
}
// </editor-fold>
}

// </editor-fold>
}

Andrew Thompson

unread,
Mar 24, 2007, 11:29:18โ€ฏPM3/24/07
to
On Mar 25, 12:44 pm, "phillip.s.pow...@gmail.com"

<phillip.s.pow...@gmail.com> wrote:
> On Mar 24, 10:36 pm, "hiwa" <cardinal_r...@yahoo.co.jp> wrote:
>
> > On Mar 25, 11:24 am, "phillip.s.pow...@gmail.com"
>
> > <phillip.s.pow...@gmail.com> wrote:
> > > Sorry that also failed:
>
> > No. You can't.
> > Post a small demo code that is generally compilable, runnable and
> > could reproduce your problem. See:http://homepage1.nifty.com/algafield/sscce.html
> > andhttp://www.yoda.arachsys.com/java/newsgroups.html
>
> I'm sorry I can't fathom how to make a smaller example of my problem,

Are you sure programming is the thing for you?

> I honestly don't know how to do it, and it's 15 pages long.

Removing the JavaDoc comments would already spare
a few pages. Removing all declarations of 'private'
would make it shorter. More specific to the code..

topPanel.setBackground(Color.WHITE);

Does this problem disappear wehen that line is
commented? If not, REMOVE IT! (Apply the same
process to *every* other line in the code.)

Further, you seem to be ignoring parts of the
SSCCE docuemtn which are relatively straightforward.
One thing it states is to ensure the code you post
does not line wrap, yet this mess you posted, had
many wrapped lines. It would require considerable
fixing before it would compile. (If you do not
believe me, copy/paste it and try compiling it
yourself.)

> ..I will just dump the whole thing. Copy and paste as you see fit and try. I
> am truly sorry.

..yes.

> read more ยป...

No thanks. Try posting an SSCCE.

Andrew T.

hiwa

unread,
Mar 25, 2007, 2:03:16โ€ฏAM3/25/07
to
Your current code is a terrible dirty lump of spaghetti.
And, those too many unnecessary classes!!!
Scrap and redesign it. You could much simplify them.
Anyway, here's a barely compilable and runnable code.
Don't use non-standard classes when you post a code on a
public forum, please.
-----------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;

public class ImageCropper extends JFrame implements ActionListener {


private boolean hasCropped = false;

private JTextField urlField;
private JButton fileChooserButton, cropButton, restoreButton,
saveButton;
private JPanel topPanel, bottomPanel;
private CropPanel cropPanel;
private CropSelector selector;
private final Builder builder = new Builder();
private final Handler handler = new Handler();
private final Processor processor = new Processor();


static final int DEFAULT_SCREEN_WIDTH
= (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();

static final int DEFAULT_SCREEN_HEIGHT
= (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();

String fileName;
URL url;
File file;
BufferedImage initialImage, mainImage;

public ImageCropper() {
super();
setup();
}

public ImageCropper(String fileName) {
super();
this.fileName = fileName;
setup();
}

public ImageCropper(URL url) {


super();
this.url = url;
setup();
}

public ImageCropper(File file) {


super();
this.file = file;
setup();
}

String getFileName(){
return fileName;
}

void setFileName(String s){
fileName = s;
}

int getScreenWidth(){
return 0;
}

int getScreenHeight(){
return 0;
}

void setScreenWidth(int w){
}

void setScreenHeight(int h){
}

public BufferedImage getImage(){
return mainImage;
}

private BufferedImage prepareImage() {


if (getFileName() == null || getFileName().equals("")) {
setFileName
("http://valsignalandet.com/images/cemetery_potluck_3/
flyer.jpg");
}
String fileName = getFileName();
BufferedImage image = null;
try {
URL url = new URL(fileName);
image = ImageIO.read(url);
}
catch(MalformedURLException mue) {
System.err.println("url: " + mue.getMessage());
}
catch(IOException ioe) {
System.err.println("read: " + ioe.getMessage());
}
return image;
}

public void actionPerformed(ActionEvent evt) {
setFileName(evt.getActionCommand());


if (evt.getActionCommand().toLowerCase().trim().equals("crop")) {
processor.crop();
repaint();
hasCropped = true;
}

else if


(evt.getActionCommand().toLowerCase().trim().indexOf("save") ==
0) {
}
}

public void initComponents() {


fileChooserButton = new JButton("Select via File");
handler.handleButton(fileChooserButton, 'F');
cropButton = new JButton("Crop");
handler.handleButton(cropButton);
restoreButton = new JButton("Restore");
handler.handleButton(restoreButton);
saveButton = new JButton("Save as:");
handler.handleButton(saveButton);

urlField = new JTextField();
urlField.setText("Enter your image URL here: ");
urlField.addActionListener(this);

cropPanel = new CropPanel(mainImage);

topPanel = new JPanel(true);
topPanel.setBackground(Color.WHITE);

topPanel.setLayout(new FlowLayout());
topPanel.add(fileChooserButton);
topPanel.add(cropButton);
topPanel.add(restoreButton);
topPanel.add(saveButton);

bottomPanel = new JPanel(true);
bottomPanel.setBackground(Color.WHITE);

bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(urlField);

if (getScreenWidth() == 0){
setScreenWidth(ImageCropper.DEFAULT_SCREEN_WIDTH);
}
if (getScreenHeight() == 0){
setScreenHeight(ImageCropper.DEFAULT_SCREEN_HEIGHT);
}

setSize(mainImage.getWidth(), mainImage.getHeight());
builder.addToPanel();
builder.addToFrame();
}

public void initObjects() {
selector = new CropSelector(cropPanel);
builder.addListeners();
}

synchronized void setup() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
initialImage = mainImage = prepareImage();
initComponents();
initObjects();
builder.showFrame();
}

public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (args.length > 0) {
new ImageCropper(args[0]);
} else {
new ImageCropper();
}
}
});
}

class Builder {
public void addToFrame() {
ImageCropper.this.add(topPanel, BorderLayout.NORTH);
ImageCropper.this.add(cropPanel, BorderLayout.CENTER);
ImageCropper.this.add(bottomPanel, BorderLayout.SOUTH);
}

public void addToPanel() {
}

private void addListeners() {

ImageCropper.this.cropPanel.addMouseListener(ImageCropper.this.selector);
ImageCropper.this.cropPanel.addMouseMotionListener
(ImageCropper.this.selector);
}

public void showFrame() {
ImageCropper.this.getContentPane().setBackground(Color.WHITE);

ImageCropper.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageCropper.this.setVisible(true);
}
}

class CropPanel extends JPanel {
BufferedImage image;
Dimension size;
Rectangle clip;

public CropPanel(BufferedImage img) {
clip = new Rectangle();
image = img;
setup();
}

public Rectangle getClip() {
return clip;
}

public BufferedImage getImage() {
return image;
}

public Dimension getPreferredSize() {
return size;
}

public void setClip(Point p1, Point p2) {

clip.setFrameFromDiagonal(p1, p2);
repaint();
}

public void setImage(BufferedImage img) {
image = img;
setup();
repaint();
}

public void initComponents() {
size = new Dimension(image.getWidth(), image.getHeight());
clip = new Rectangle();
}

public void initObjects() {
}

public void paintComponent(Graphics g) {


super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int x = (w - this.size.width) / 2;
int y = (h - this.size.height) / 2;
g2.drawImage(this.image, x, y, this);

g2.setPaint(Color.red);
g2.draw(this.clip);
}

synchronized void setup() {
this.initObjects();
this.initComponents();
}
}

class CropSelector extends MouseInputAdapter {
private CropPanel cropPanel;
private Point start;
boolean hasSelected;

public CropSelector(CropPanel cropPanel) {
this.cropPanel = cropPanel;
}

public void mousePressed(MouseEvent evt) {
if(evt.getClickCount() == 2) {
cropPanel.setClip(new Point(-1, -1), new Point(-1, -1));
hasSelected = false;
}
start = evt.getPoint();
}

public void mouseDragged(MouseEvent evt) {
cropPanel.setClip(start, evt.getPoint());
hasSelected = true;
}
}

class Handler {


private void genericButtonHandling(JButton button) {
button.setFocusable(false);
button.setSelected(false);

button.addActionListener(ImageCropper.this);
}

private void handleButton(JButton button) {
this.genericButtonHandling(button);
button.setMnemonic(button.getText().charAt(0));
}

private void handleButton(JButton button, char letter) {
this.genericButtonHandling(button);
button.setMnemonic(letter);
}
}

class Processor {
public void crop() {
if (hasCropped){
return;
}

int x = (int)cropPanel.clip.getX();
int y = (int)cropPanel.clip.getY();
int w = (int)cropPanel.clip.getWidth();
int h = (int)cropPanel.clip.getHeight();

if (x <= 0 || y <= 0 || w <= 0 || h <= 0){
return;
}

BufferedImage bi = mainImage.getSubimage(x, y, w, h);
mainImage = bi;
cropPanel.setImage(mainImage);
cropPanel.setup();
}
}
}
---------------------------------------------------------------

phillip....@gmail.com

unread,
Mar 25, 2007, 3:07:29โ€ฏPM3/25/07
to

I have a better idea

Andrew, Lew, Roedy: If asked how YOU would crop an image, how would
YOU do it, what concepts would you use to ensure an image can be
consistently cropped?

Instead of asking stupid questions and posting stupid code and cannot
translate stupid code, I am going to ask a more fundamental question
that I hope makes more sense.

Joshua Cranmer

unread,
Mar 25, 2007, 4:58:32โ€ฏPM3/25/07
to
phillip....@gmail.com wrote:
> Andrew, Lew, Roedy: If asked how YOU would crop an image, how would
> YOU do it, what concepts would you use to ensure an image can be
> consistently cropped?
>
> Instead of asking stupid questions and posting stupid code and cannot
> translate stupid code, I am going to ask a more fundamental question
> that I hope makes more sense.
>

Please no, no ad hominem attacks. The point is that you were not
providing sufficient background with which to explain the problem, and
then you supplied too much.

From the first line of code, it would appear that your problem is that
you tried to cast a generic Image to a BufferedImage.

createImage() does not return BufferedImage's. Sorry.

Knute Johnson

unread,
Mar 25, 2007, 6:33:48โ€ฏPM3/25/07
to

The first thing that folks are going to ask you is what kind of image?
It makes a difference as you have found out.

> Instead of asking stupid questions and posting stupid code and cannot
> translate stupid code, I am going to ask a more fundamental question
> that I hope makes more sense.

And that is the problem. You think the questions we ask are stupid and
not related to your problem and so you ignore them. To give you a good
answer requires that we have some clue what you are up to. You never
supply any background, just this or that doesn't work. You absolutely
refuse to write any compilable test code to demonstrate your problem.
This alone infuriates some of us and yet we continue to try to be helpful.

hiwa gave you a good description of where this problem lies and you
don't even acknowledge him or his solution to your problem. You just
tell him that it doesn't work and you can't possibly produce an SSCCE.

There are a lot of very knowledgeable and helpful people on this list
that would be glad to help you out if you would just spend a little bit
more of your time and expect a little less of ours.

There are several simple ways to produce an image that is a portion of
another image. In fact while I was watching TV last night I wrote a
component that lets you select a sub image with the mouse. It works
great and has nowhere near as much code as that thing you posted.

So if you are still interested in code to make a sub image, answer the
questions below, otherwise YOYOMOFO.

1) What is your program supposed to do?

2) What type of image is the source (Image or BufferedImage)?

3) If an Image, did you create it or load it?

4) What are you going to do with the sub image (write it to disk,
draw it on a component, modify it in some way, etc.)?

or you can post an SSCCE and I would be glad to help you debug it.

--

Knute Johnson
email s/nospam/knute/

hiwa

unread,
Mar 25, 2007, 8:28:41โ€ฏPM3/25/07
to
On Mar 26, 4:07 am, "phillip.s.pow...@gmail.com"

You make an arbitrary decsion that the problem is cropping the image.
But your are wrong, completely wrong. You would understand that from
comparing your original code and my posted code. Don't decide it
yourself because you don't know the whole picture of the issue. Only
thing you have to do is posting a good SSCCE.

phillip....@gmail.com

unread,
Mar 26, 2007, 12:34:52โ€ฏAM3/26/07
to
On Mar 25, 4:58 pm, Joshua Cranmer <Pidgeo...@epenguin.zzn.com> wrote:

> phillip.s.pow...@gmail.com wrote:
> > Andrew, Lew, Roedy: If asked how YOU would crop an image, how would
> > YOU do it, what concepts would you use to ensure an image can be
> > consistently cropped?
>
> > Instead of asking stupid questions and posting stupid code and cannot
> > translate stupid code, I am going to ask a more fundamental question
> > that I hope makes more sense.
>
> Please no, no ad hominem attacks.

...Dude, I AM THE ONE WITH THE STUPID QUESTIONS!!!!!!!!!! I am ad-
hominem attacking myself!

The point is that you were not
> providing sufficient background with which to explain the problem, and
> then you supplied too much.
>
> From the first line of code, it would appear that your problem is that
> you tried to cast a generic Image to a BufferedImage.
>
> createImage() does not return BufferedImage's. Sorry.

Which is why I switched coding, because I learned that createImage()
is supposed to return java.awt.Image, but instead it returns
sun.awt.image.ToolkitImage, which cannot be cast to BufferedImage. So
you're unfortunately telling me what I already know.


phillip....@gmail.com

unread,
Mar 26, 2007, 12:48:59โ€ฏAM3/26/07
to
On Mar 25, 6:33 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:

Knute you're off. I never said YOUR questions are stupid. I said
that MY QUESTIONS are stupid!!

> not related to your problem and so you ignore them. To give you a good
> answer requires that we have some clue what you are up to. You never
> supply any background, just this or that doesn't work. You absolutely
> refuse to write any compilable test code to demonstrate your problem.
> This alone infuriates some of us and yet we continue to try to be helpful.

I don't understand this. I wrote compilable test code to demonstrate
my problem; I pasted it. It was apparently still too long. I don't
know HOW to write it to meet your standards and no online link is
going to apparently help as you've given it to me a dozen times and
I'm still seemingly "not getting it" if I can't seem to paste
compilable, short code that you understand.

>
> hiwa gave you a good description of where this problem lies and you
> don't even acknowledge him or his solution to your problem. You just
> tell him that it doesn't work and you can't possibly produce an SSCCE.


Um, because I don't know how to do that?


>
> There are a lot of very knowledgeable and helpful people on this list
> that would be glad to help you out if you would just spend a little bit
> more of your time and expect a little less of ours.

I doubt this will help, but I have ADD. It is a learning disability
that causes me to have to learn things radically differently from you
guys and perhaps that's why it seems I am resisting your help, perhaps
it's because I physically and psychologically CANNOT understand your
help.

>
> There are several simple ways to produce an image that is a portion of
> another image. In fact while I was watching TV last night I wrote a
> component that lets you select a sub image with the mouse. It works
> great and has nowhere near as much code as that thing you posted.
>
> So if you are still interested in code to make a sub image, answer the
> questions below, otherwise YOYOMOFO.
>
> 1) What is your program supposed to do?

You select an image from URL or select from file. You drag your mouse
over the image and a red rectangle will appear. That red rectangle is
going to be your cropped image. You click "Crop" and zap it crops.
And then you can save the new image as a new file. That's it.

>
> 2) What type of image is the source (Image or BufferedImage)?

The source image can be jpeg, gif, png, whatever. It will become a
BufferedImage object when loaded.

>
> 3) If an Image, did you create it or load it?

Not an Image, and don't understand the question. I assume the right
answer will be that I got it from URL or from file.


>
> 4) What are you going to do with the sub image (write it to disk,
> draw it on a component, modify it in some way, etc.)?
>

write it to disk.

> or you can post an SSCCE and I would be glad to help you debug it.

I can't write SSCCE's. I tried but my SSCCE turns into "spaghetti
code" that nobody understands. I have studied over and over and all I
know how to write is "spaghetti code" as it seems, even when I "dumb
it down" to the smallest possible subsegment of what I need to do; I
have no idea whatever how to make my application small enough to
qualify by you guys as SSCCE, so I decided to paste the whole thing
(minus my special classes I wrote) and allow you to paste it, compile
it and run it for yourselves. I thought that would be what you
request as far as an SSCCE is concerned because it is compilable and
short.

Knute Johnson

unread,
Mar 26, 2007, 1:15:21โ€ฏAM3/26/07
to

Toolkit.createImage() and Component.createImage() both return a
java.awt.Image. An Image cannot be cast to a BufferedImage because
BufferedImage extends Image. You could cast a BufferedImage to an Image
if you wanted to.

SSCCE

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class test8 {
public static void main(String[] args) throws Exception {
BufferedImage bi =
new BufferedImage(10,10,BufferedImage.TYPE_INT_BGR);
if (bi instanceof Image)
System.out.println("A BufferedImage is an Image");
Image i = (Image)bi;
System.out.println("A BufferedImage can be cast to an Image");

Toolkit t = Toolkit.getDefaultToolkit();
i = t.createImage("saturn.jpg");
if (i instanceof Image)
System.out.println(
"Toolkit.createImage() returns an Image");
if (!(i instanceof java.awt.image.BufferedImage))
System.out.println("An Image is not a BufferedImage");
bi = (BufferedImage)i;
}
}

It is however fairly easy to create a BufferedImage from an Image but it
is crude.

Knute Johnson

unread,
Mar 26, 2007, 2:28:43โ€ฏAM3/26/07
to
OK. Let's start from scratch then. You want to get a BufferedImage
from a file or the net and create a sub image that from the original.

To get a BufferedImage you have to use the imageio package to load and
save. You need to look at the ImageIO.read() and ImageIO.write()
methods. A BufferedImage is very easy to get a sub image from, you use
the BufferedImage.getSubImage() method. The only thing to watch out for
here is that the sub image uses the same pixel data as the original.

Please look at my CropComponent class below to see a simple example of
what you want to do. The sub image is saved to the file, crop.jpg, as
soon as you release the mouse button.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

public class CropComponent extends JPanel {
BufferedImage bi,subImage;
boolean mouseDown;
int startX,startY;
int endX,endY;
int x,y,w,h;

public CropComponent(BufferedImage bi) {
this.bi = bi;
setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
mouseDown = true;
startX = me.getX();
startY = me.getY();
}
public void mouseReleased(MouseEvent me) {
mouseDown = false;
subImage =
CropComponent.this.bi.getSubimage(x,y,w+1,h+1);
Runnable r = new Runnable() {
public void run() {
try {
ImageIO.write(
subImage,"JPEG",new File("crop.jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
new Thread(r).start();
x = y = w = h = 0;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
endX = Math.max(0,me.getX());
endX =
Math.min(endX,CropComponent.this.bi.getWidth()-1);
endY = Math.max(0,me.getY());
endY =
Math.min(endY,CropComponent.this.bi.getHeight()-1);
if (startX < endX && startY < endY) {
x = startX;
y = startY;
w = endX-startX;
h = endY-startY;
} else if (startX > endX && startY > endY) {
x = endX;
y = endY;
w = startX-endX;
h = startY-endY;
} else if (startX < endX && startY > endY) {
x = startX;
y = endY;
w = endX-startX;
h = startY-endY;
} else if (startX > endX && startY < endY) {
x = endX;
y = startY;
w = startX-endX;
h = endY-startY;
}
repaint();
}
});
}

public void paintComponent(Graphics g2D) {
Graphics2D g = (Graphics2D)g2D;
g.drawImage(bi,0,0,null);
if (mouseDown) {
g.setStroke(new BasicStroke(1.0f,BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND,1.0f,new float[] {2.0f,3.0f},1.0f));
g.setColor(Color.WHITE);
g.drawRect(x,y,w,h);
}
}

public static void main(final String[] args) {

Runnable r = new Runnable() {
public void run() {
try {
BufferedImage bi = ImageIO.read(new File(args[0]));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CropComponent cc = new CropComponent(bi);
f.add(cc,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
EventQueue.invokeLater(r);

phillip....@gmail.com

unread,
Mar 26, 2007, 10:18:28โ€ฏAM3/26/07
to
On Mar 26, 2:28 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:

> OK. Let's start from scratch then. You want to get a BufferedImage
> from a file or the net and create a sub image that from the original.

Got it to work! Turns out that my research wasn't complete and I
overlooked the solution someone else on the Sun Java forums provided
that incorporates perfectly (as it is supposed to) into my code:

/**
* Perform crop


* <a href="http://forum.java.sun.com/thread.jspa?
threadID=627125&messageID=3587532">Reference</a>
*/

public void crop() {
Rectangle r = ImageCropper.this.cropPanel.getClip();
if (!this.isClipInsideImage(r)) /* AVOID
RasterFormatException */ {
return;
}
// offset clip from view to raster model
int x = (ImageCropper.this.cropPanel.getWidth() -

ImageCropper.this.cropPanel.getImage().getWidth()) / 2;
int y = (ImageCropper.this.cropPanel.getHeight() -

ImageCropper.this.cropPanel.getImage().getHeight()) / 2;
BufferedImage clippedImage =

ImageCropper.this.cropPanel.getImage().getSubimage(
(int)r.getX() - x,
(int)r.getY() - y,
(int)r.getWidth(),
(int)r.getHeight());
ImageCropper.this.cropPanel.setImage(clippedImage);
ImageCropper.this.cropPanel.setup();
ImageCropper.this.validate();
}

/**
* Determine if the {@link java.awt.image.BufferedImage} has a
legitimate {@link java.awt.Rectangle} clip
* To avoid {@link java.awt.image.RasterFormatException}
* @param r {@link java.awt.Rectangle}
* @return {@link java.lang.Boolean}


* <a href="http://forum.java.sun.com/thread.jspa?
threadID=627125&messageID=3587532">Reference</a>
*/

private boolean isClipInsideImage(Rectangle r) {
int w = ImageCropper.this.cropPanel.getWidth();
int h = ImageCropper.this.cropPanel.getHeight();
int imageWidth =
ImageCropper.this.cropPanel.getImage().getWidth();
int imageHeight =
ImageCropper.this.cropPanel.getImage().getHeight();
int x = (w - imageWidth) / 2;
int y = (h - imageHeight) / 2;
if ((int)r.getX() >= x &&
(int)r.getX() + (int)r.getWidth() <= x +
imageWidth &&
(int)r.getY() >= y &&
(int)r.getY() + (int)r.getHeight() <= y +
imageHeight) {
return true;
}
return false;


}

phillip....@gmail.com

unread,
Mar 26, 2007, 10:31:02โ€ฏAM3/26/07
to
On Mar 26, 10:18 am, "phillip.s.pow...@gmail.com"

<phillip.s.pow...@gmail.com> wrote:
> On Mar 26, 2:28 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
>
> > OK. Let's start from scratch then. You want to get a BufferedImage
> > from a file or the net and create a sub image that from the original.
>
> Got it to work! Turns out that my research wasn't complete and I
> overlooked the solution someone else on the Sun Java forums provided
> that incorporates perfectly (as it is supposed to) into my code:
>

[snip]

Even better version I think:

/**
* Perform crop
* <a href="http://forum.java.sun.com/thread.jspa?
threadID=627125&messageID=3587532">Reference</a>
*/
public void crop() {
Rectangle r = ImageCropper.this.cropPanel.getClip();

// OFFSET CLIP FROM VIEW TO RASTER MODEL
try {


int x = (ImageCropper.this.cropPanel.getWidth() -

ImageCropper.this.cropPanel.getImage().getWidth()) / 2;
int y = (ImageCropper.this.cropPanel.getHeight() -

ImageCropper.this.cropPanel.getImage().getHeight()) / 2;
BufferedImage clippedImage =

ImageCropper.this.cropPanel.getImage().getSubimage(
(int)r.getX() - x,
(int)r.getY() - y,
(int)r.getWidth(),
(int)r.getHeight());
ImageCropper.this.cropPanel.setImage(clippedImage);
ImageCropper.this.cropPanel.setup();
ImageCropper.this.validate();

} catch (RasterFormatException e) /* CLIP IS OUT OF BOUNDS
*/ {
JOptionPane.showMessageDialog(
ImageCropper.this, "Your selection is out of bounds of
the image",
"Invalid Selection",
JOptionPane.ERROR_MESSAGE);
ImageCropper.this.cropPanel.resetClip();
}
}

Wojtek

unread,
Mar 26, 2007, 10:39:14โ€ฏAM3/26/07
to
phillip....@gmail.com wrote :

> I thought I got it and again, I failed.

Try a toolkit from http://www.nsftools.com/tips/JavaTips.htm#jpgimage
by Julian Robichaux.

It is quite useful for cropping and rotating JPG images.

--
Wojtek :-)


Oliver Wong

unread,
Mar 26, 2007, 1:28:36โ€ฏPM3/26/07
to
<phillip....@gmail.com> wrote in message
news:1174884539.2...@p77g2000hsh.googlegroups.com...

>
> I don't understand this. I wrote compilable test code to demonstrate
> my problem; I pasted it. It was apparently still too long. I don't
> know HOW to write it to meet your standards and no online link is
> going to apparently help as you've given it to me a dozen times and
> I'm still seemingly "not getting it" if I can't seem to paste
> compilable, short code that you understand.

The problem is that what you posted was simply too long for most
people to deal with. I think one could write a program that loads, crops,
and displays an image in 20-30 lines of text, so your SSCCE shouldn't be
much longer than, say, 60 lines. When you posted 15 pages of text, I guess
some people automatically assumed you weren't trying very hard.

From a quick glance at your code, it looks like you're using some sort
of IDE which wants to restrict you to only editing some code, and
otherwise marks other parts of the code as "don't touch" zones, where it
automatically generates the code for that section. Using such an IDE in
general is fine, but obviously such an IDE is not well suited for writing
SSCCEs, since you can't control all the extra code it's generating.

Try using a different IDE for the purposes of writing SSCCEs. If you
don't know how to write GUI code (e.g. creating JLabels and such) without
using your specialized IDE, then maybe you should learn how. There are
tutorials on Sun's website.

- Oliver


hiwa

unread,
Mar 26, 2007, 6:35:51โ€ฏPM3/26/07
to
On Mar 26, 11:39 pm, Wojtek <nowh...@a.com> wrote:
> phillip.s.pow...@gmail.com wrote :

>
> > I thought I got it and again, I failed.
>
> Try a toolkit fromhttp://www.nsftools.com/tips/JavaTips.htm#jpgimage

> by Julian Robichaux.
>
> It is quite useful for cropping and rotating JPG images.
>
> --
> Wojtek :-)

Cropping per se is NOT his problem.
It is a bug borne from hideous spaghetti code.
See his original code.

phillip....@gmail.com

unread,
Apr 5, 2007, 5:55:34โ€ฏPM4/5/07
to
Yours is as long as mine so how is yours acceptable as SSCCE?

Lew

unread,
Apr 5, 2007, 6:34:37โ€ฏPM4/5/07
to
phillip....@gmail.com wrote in response to "hiwa":

> Yours is as long as mine so how is yours acceptable as SSCCE?
>

How did they differ? Was the other one (I can't see yours any more - the
thread is too old) more correct? More compilable? Did it, as claimed, use
standard classes instead of non-standard ones?

I don't think length is what's important here. What hiwa was apparently trying
to do was give you an SSCCE of something different from what you wrote - not
trying to give you lessons on how to write an SSCCE.

Why don't you try responding to hiwa's actual point?

--
Lew

Andrew Thompson

unread,
Apr 5, 2007, 7:52:41โ€ฏPM4/5/07
to
phillip....@gmail.com wrote:
>Yours is as long as mine so how is yours acceptable as SSCCE?

Even assuming your code example was S*, there is also
the SCCE. Have you ever actually *read* the document
on the SSCCE? Again, the URL is..
<http://www.physci.org/codes/sscce.html>

* 'Short' is simply one part of an SSCCE, if code
lacks the other aspects, it is not an SSCCE.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-setup/200704/1

0 new messages