4 Replies Latest reply: Jul 24, 2011 12:34 AM by EJP RSS

    AffineTransform not running

    877520
      This is the program.but while I compile it then shows no error.....but while I run the program then it shows these error:

      Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
           at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
           at java.awt.image.BufferedImage.<init>(Unknown Source)
           at ShowLabel.createImages(AffineTransformExample.java:78)
           at ShowLabel.<init>(AffineTransformExample.java:73)
           at AffineTransformExample.<init>(AffineTransformExample.java:16)
           at AffineTransformExample.main(AffineTransformExample.java:39)
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.image.*;
      import java.awt.geom.AffineTransform;
      
      public class AffineTransformExample extends JFrame {
       ShowLabel label;
        JComboBox shearX, shearY;
        
        String[] shear = { "0.00", "0.25", "0.50", "0.75", "1.00" };
      
        public AffineTransformExample() {
          super("Affine Transform Example");
          Container container = getContentPane();
          label = new ShowLabel();
          container.add(label);
      
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(2, 4, 5, 5));
          
          shearX = new JComboBox(shear);
          shearX.setSelectedItem("0.00");
          panel.add(new JLabel("Shear X:"));
          panel.add(shearX);
          shearX.addActionListener(new ComboBoxListener());
         
          shearY = new JComboBox(shear);
          shearY.setSelectedItem("0.00");
           panel.add(new JLabel("Shear Y:"));
          panel.add(shearY);
          shearY.addActionListener(new ComboBoxListener());
          container.add(BorderLayout.NORTH, panel);
      
          setSize(350,300);
          setVisible(true);
        }
          public static void main(String arg[]) {
          new AffineTransformExample();
        }
          class ComboBoxListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
            JComboBox box = (JComboBox) e.getSource();
              if (box == shearX) {
              label.shearx = Double.parseDouble((String) box.getSelectedItem());
              label.value(true);
              label.filter();
              label.repaint();
            } else if (box == shearY) {
              label.sheary = Double.parseDouble((String) box.getSelectedItem());
              label.value(true);
              label.filter();
              label.repaint();
            }
          }
        }
      }
      class ShowLabel extends JLabel {
        Image image;
        BufferedImage bufferedImage1, bufferedImage2;
        BufferedImage bufferedImage;
        Graphics2D g2d;
        AffineTransform affineTransform;
        double shearx = 1.0, sheary = 1.0;
      
        ShowLabel() {
          image = Toolkit.getDefaultToolkit().getImage("image4.jpg");
          MediaTracker mediaTracker = new MediaTracker(this);
          mediaTracker.addImage(image, 1);
          try {
            mediaTracker.waitForAll();
          } catch (Exception e) {}
          createImages();
          affineTransform = new AffineTransform();
        }
          public void createImages() {
          bufferedImage1 = new BufferedImage(image.getWidth(this), image
              .getHeight(this), BufferedImage.TYPE_INT_RGB);
          g2d = bufferedImage1.createGraphics();
          g2d.drawImage(image, 0, 0, this);
          bufferedImage = bufferedImage1;
          bufferedImage2 = new BufferedImage(image.getWidth(this), image
              .getHeight(this), BufferedImage.TYPE_INT_RGB);
        }
          public void value(boolean shear) {
          if (shear) {
            affineTransform.setToShear(shearx, sheary);
            shear = true;
          }
        }
          public void filter() {
          AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, null);
          Graphics2D G2D = bufferedImage2.createGraphics();
          G2D.clearRect(0, 0, bufferedImage2.getWidth(this), bufferedImage2.getHeight(this));
          affineTransformOp.filter(bufferedImage1, bufferedImage2);
          bufferedImage = bufferedImage2;
        }
          public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2D = (Graphics2D) g;
          g2D.drawImage(bufferedImage, 0, 0, this);
        }
      }
      Please correct the code and explain in detail.It will help me a lot

      Edited by: 874517 on Jul 22, 2011 11:13 PM
        • 1. Re: AffineTransform not running
          EJP
          Width (-1) and height (-1) cannot be <= 0
          What part of that don't you understand?
          • 2. Re: AffineTransform not running
            877520
            My problem is the program is not running...It shows these error:

            Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
            at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
            at java.awt.image.BufferedImage.<init>(Unknown Source)
            at ShowLabel.createImages(AffineTransformExample.java:78)
            at ShowLabel.<init>(AffineTransformExample.java:73)
            at AffineTransformExample.<init>(AffineTransformExample.java:16)
            at AffineTransformExample.main(AffineTransformExample.java:39)
            • 3. Re: AffineTransform not running
              DrClap
              Yes. Let me explain something about error messages. They are intended to tell you something about the problem. That means that you should read what they say.

              In this case the message says you cannot use -1 for the width and height of something. And it tells you exactly where the error occurs:
              at java.awt.image.BufferedImage.<init>(Unknown Source)
              at ShowLabel.createImages(AffineTransformExample.java:78)
              Therefore at line 78 of your code, in the createImages method, you are creating a new BufferedImage object, and you did it wrong. The error message explains what you did wrong. So change your program to not do that. It really isn't rocket science to read error messages.
              • 4. Re: AffineTransform not running
                EJP
                Bhutu wrote:
                My problem is the program is not running...It shows these error:
                You already posted that. If you didn't understand my reply, say so. Don't just repeat yourself.