Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Deleting an image

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deleting an image

    I'm working with blueJ atm, and have a question about removal of an image that i create.
    I'm doing some edditing of pictures, and need a new image to complete my goal, but it's not working out the way I planned...

    I create the new image, and grants it the same size as my original picture:

    blank = new Image(img.getWidth(),img.getHeight(),"blank");

    I then use this image as i please, and then i haven't got a use for it any more. The issue being, when I call my method, this picture pops up along with the changes to my original image.
    I'm looking for a way to delete the new image (blank) that i created, so it wont ever appear when the method is called. How do I do this?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Deleting an image

    The key is in how and where you display the image, and the code used to do this as this will determine how to make it so that it is no longer displayed.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deleting an image

    Quote Originally Posted by curmudgeon View Post
    The key is in how and where you display the image, and the code used to do this as this will determine how to make it so that it is no longer displayed.
    This is the code for an image

    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.awt.image.WritableRaster;
    import java.io.File;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
     
    import java.util.List;
    import java.util.ArrayList;
     
     
    public class Image
    {
        private Pixel[][] pixels;
        private int width, height;
        private java.awt.Image img;
     
        /**
         * Creates a new <code>Image</code> object, based on
         * an existing file.
         * The title of the image is taken from the filename.
         * 
         * @param filename The file to be loaded. Can be a JPG, PNG, BMP or GIF
         */
        public Image(String filename)
        {
            try{
                BufferedImage input = ImageIO.read(new File(filename));
                width = input.getWidth();
                height = input.getHeight();
                pixels = new Pixel[width][height];
                WritableRaster raster = (WritableRaster) input.getData();
                int[] rgb = new int[3];
                for(int i = 0; i < width; i++)
                    for(int j = 0; j < height; j++)
                    {
                        rgb = raster.getPixel(i, j, rgb);
                        int value = (rgb[0] + rgb[1] + rgb[2]) / 3;
                        pixels[i][j] = new Pixel(value);
                }
            } catch(Exception e)
            {
                width = 100;
                height = 100;
                pixels = new Pixel[width][height];
                for(int i = 0; i < width; i++)
                    for(int j = 0; j < height; j++)
                        pixels[i][j] = new Pixel(0);
            }
     
            vindue = new JFrame(filename);
            tavle = new Tavle();
     
            vindue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            vindue.setContentPane(tavle);
            vindue.pack();
            vindue.setVisible(true);
        }
     
        /**
         * Constructs a new empty image.
         * Constructs all of the pixels in the image as well.
         * 
         * @param width The width of the image
         * @param height The height of the image
         * @param title The title of the image
         */
        public Image(int width, int height, String title)
        {
            this.width = width;
            this.height = height;
            pixels = new Pixel[width][height];
            for(int i = 0; i < width; i++)
                for(int j = 0; j < height; j++)
                    pixels[i][j] = new Pixel(0);
     
            vindue = new JFrame(title);
            tavle = new Tavle();
     
            vindue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            vindue.setContentPane(tavle);
            vindue.pack();
            vindue.setVisible(true);
     
        }
     
        /**
         * Returns the width of the image
         * 
         * @return The width of the image
         */
        public int getWidth()
        {
            return width;
        }
     
        /**
         * Returns the height of the image
         * 
         * @return The height of the image
         */
        public int getHeight()
        {
            return height;
        }
     
        /**
         * Informs the image that one or more of its pixels
         * has changed value, and that it is time to repaint
         * the canvas.
         */
        public void pixelsUpdated()
        {
            tavle.rebuildPicture();
            tavle.repaint();
        }
     
        /**
         * Returns the pixel in position <i>(i, j)</i>.
         * If the position <i>(i, j)</i> is outside 
         * the image, an exception is thrown.
         * 
         * @param i The horizontal index
         * @param j The vertical index
         */
        public Pixel getPixel(int i, int j)
        {
            return pixels[i][j];
        }
     
        /**
         * Returns a list of all the pixels in the image
         * 
         * @return A list of all the pixels in the image.
         */
        public List<Pixel> getPixels()
        {
            List<Pixel> all = new ArrayList<Pixel>();
            for(int i = 0; i < width; i++)
                for(int j = 0; j < height; j++)
                    all.add(pixels[i][j]);
            return all;
        }
     
        private void addPixelToList(int i, int j, List<Pixel> list)
        {
            if(i >= 0 && j >= 0 && i < width && j < height)
                list.add(pixels[i][j]);
        }
     
        /**
         * Returns the up to nine neighbours in the neighbourhod
         * of the pixel in position <i>(i, j)</i>.
         * 
         * @param i The horizontal index
         * @param j The vertical index
         */
        public List<Pixel> getNeighbours(int i, int j)
        {
            List<Pixel> result = new ArrayList<Pixel>();
     
            for(int x = i - 1; x <= i + 1; x ++)
                for(int y  = j - 1; y <= j + 1; y++)
                    addPixelToList(x, y, result);
     
            return result;
        }
     
        public int average(int i, int j){
     
            Pixel x = getPixel(i, j);
     
            int total=x.getValue();
            int number=0;
            int value=0;
     
            for(Pixel p : getNeighbours(i,j)){
     
                value = p.getValue();
                number++;
                total = total + value;
     
            }
     
            return total/number;
     
        }
     
        private class Tavle extends JPanel
        {
            private Tavle()
            {
                setPreferredSize(new Dimension(width, height));
            }
     
            private void rebuildPicture() {
                int pix[] = new int[width*height];
                int tmp = 0;
                for(int j = 0; j < height; j++) {
                    for(int i = 0; i < width; i++) {
                        int val = pixels[i][j].getValue();
                        // alpha, red, green, blue
                        pix[tmp++] = (255 << 24) | (val << 16) | (val << 8) | val;     
                    }
                }
                img = createImage(new MemoryImageSource(width, height, pix, 0, width));
            }
     
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);         
                if (img == null) rebuildPicture();
                g.drawImage(img, 0, 0, this);
            }
        }
     
        private JFrame vindue;
        private Tavle tavle;
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Deleting an image

    The code you posted doesn't compile without errors. Missing class definition. Also there is no main() method for testing.
    Can you make a small program that compiles, executes and shows the problem?

    BTW Image is the name of a class in Java SE. Using the same name makes for confusion.
    Last edited by Norm; September 22nd, 2012 at 02:53 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deleting an image

    Quote Originally Posted by Norm View Post
    The code you posted doesn't compile without errors. Missing class definition. Also there is no main() method for testing.
    Can you make a small program that compiles, executes and shows the problem?

    BTW Image is the name of a class in Java SE. Using the same name makes for confusion.
    I'm new to this, and didn't write the base of the code myself, but i assure you that it compiles just fine. This is BlueJ, and I'm not exactly sure what differens it makes, but it runs just fine.
    I don't know how to make such a program, but im having no problem that prevents the program from executing. I just need a way to delete the Image i make.
    As it is now, a new Image will pop up every time i run my methods, because i need an additional image to save data in. I want the method that creates it, to also delete it when it is done with it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Deleting an image

    Sorry, I can not help you if I can't execute the code.
    You need to post a complete program that compiles and executes if you want anyone to help you.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. finding and deleting from an array?
    By 93tomh in forum Java Theory & Questions
    Replies: 19
    Last Post: August 13th, 2012, 09:39 AM
  2. deleting with shift ?
    By keep smiling in forum Collections and Generics
    Replies: 4
    Last Post: February 26th, 2012, 04:05 PM
  3. Deleting Array Records
    By rainexel in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 11:40 AM
  4. Deleting record from database HELP! :(
    By shando1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 12:36 AM
  5. renaming/deleting file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 15th, 2011, 12:18 AM

Tags for this Thread