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 5 of 5

Thread: How do I save a bunch of images in a JPanel as one?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How do I save a bunch of images in a JPanel as one?

    I made a program where the user selects imageIcons from a JScrollPane and then can place them in a different JScrollPane. They can then save the image they make. The way the program saves the images is as numbers. Each image is given a number and when they place it that number is stored in a 3 dimensional array marking its location. That part works and is functional. However,I would also like to take all of those imageIcons the user places and save them as one .gif file so the image can be viewed without the program having to decipher all of the numbers it saves and put them back into icons. So does anyone know how i could do this? I can paint all the tiles at once and just want to save the contents of the JScrollPane as a .gif file. I need it to be just the image so a screenshot wouldn't work unless it would be the same size as the images and would only have what's in the JScrollPane and could see all of the JScrollPane at once.

    This is the code for where the images I want to save are displayed and the user can place them.


    public class Map extends JFrame
    {
      final int AREA_SIZE_X = 100;
      final int AREA_SIZE_Y = 100;
      JFrame sWindow;
      mapPanel mPanel;
     JScrollPane scrollPane;
      Map() throws IOException
      {
        sWindow = new Selection(this);
        sWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
        sWindow.setSize(270,700);
        sWindow.setVisible(true);
        mPanel = new mapPanel(sWindow);
        this.getContentPane().setLayout(new BorderLayout());
        scrollPane = new JScrollPane(mPanel);
        this.getContentPane().add(scrollPane, BorderLayout.WEST);
        mPanel.requestFocus();      // Give the panel focus.
        this.setTitle("Map maker");
        this.setLocation(400, 0);
        this.setResizable(false);
     
        this.pack();
      }
    }
    class mapPanel extends JPanel implements MouseListener, MouseMotionListener
    {
      //int[][][] back1;
     // int[][][] back2;
      boolean topLayer=false;
      final int AREA_SIZE_X = 100;
      final int AREA_SIZE_Y = 100;
      Point mouseLocation;
      Point myMouse;
      int[][][] tiles = new int[AREA_SIZE_X][AREA_SIZE_Y][4];
      ArrayList<BufferedImage> bFS;
      //ArrayList<ImageIcon> icons=new <ImageIcon>ArrayList();
      BufferedImage tempImage;
      Selection sWindow;
     
      public mapPanel(JFrame selWindow)
      {
        this.addMouseMotionListener(this);
        sWindow = (Selection) selWindow;
        bFS = sWindow.getBuffered();
        for (int i = 0; i < AREA_SIZE_X; i++)
        {
          for (int j = 0; j < AREA_SIZE_Y; j++)
          {
            for (int k = 0; k < 4; k++)
            {
              tiles[i][j][k] = -1;
            }
          }
        }
      //  back1= tiles.clone();
       // back2=tiles.clone();
        this.setBackground(Color.white);
        this.setPreferredSize(new Dimension(10*AREA_SIZE_X, 10*AREA_SIZE_Y));
        this.addMouseListener(this);
       // this.addKeyListener(this);  // This class has its own key listeners.
        this.setFocusable(true);    // Allow panel to get focus
        sWindow.updateTiles(tiles);
      }
      public void fillBackground()
      {
         for (int i = 0; i < AREA_SIZE_X; i++)
        {
          for (int j = 0; j < AREA_SIZE_Y; j++)
          {
            tiles[i][j][0]= sWindow.getSelected();
            for (int k = 1; k < 4; k++)
            {
              tiles[i][j][k] = -1;
            }
          }
        }
      }
      public void paintComponent(Graphics g)
      {
     
        super.paintComponent(g);
        g.setColor(Color.white);
        g.fillRect(0, 0, AREA_SIZE_X * 20, AREA_SIZE_Y * 20);
        mouseLocation = this.getMousePosition();
     
        for (int i = 0; i < AREA_SIZE_X; i++)
        {
          for (int j = 0; j < AREA_SIZE_Y; j++)
          {
            for (int k = 0; k < 4; k++)
            {
              if (tiles[i][j][k] != -1)
              {
                g.drawImage(bFS.get(tiles[i][j][k]), i * 20, j * 20, this);
              }
            }
          }
        }
    //System.out.println(this.hasFocus());
        if (this.hasFocus()&&this.contains(mouseLocation))
        {
          g.drawImage(bFS.get(sWindow.getSelected()), mouseLocation.x - 20, mouseLocation.y - 20, this);
          // g.drawImage(bFS.get(sWindow.getSelected()), mouseLocation.x - 20, mouseLocation.y - 20, this);
          g.setColor(Color.red);
          g.drawRect(((mouseLocation.x - 10) / 20) * 20, ((mouseLocation.y - 10) / 20) * 20, 20, 20);
        }
      }
     
      @Override
      public void mouseClicked(MouseEvent e)
      {
        // updateHistogram(tiles);
        topLayer=sWindow.isLayered();
        // System.out.println(this.hasFocus());
        myMouse = this.getMousePosition();
        // System.out.println((myMouse.x - 10) / 20+"  "+(myMouse.y) / 20);
       if(topLayer)
       {
        for(int k=0;k<4;k++)
        {
          if(tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][k]==-1)
          {
            tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][k] = sWindow.getSelected();
            break;
          }
        }
        //System.out.println(tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][0]);
       // System.out.println(tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][1]);
       }
       else
       {
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][0] = sWindow.getSelected();
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][1] = -1;
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][2] = -1;
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][3] = -1;
       }
        //tiles[1][1] = sWindow.getSelected();
        //System.out.println(sWindow.getSelected());
        this.repaint();
      }
     
      @Override
      public void mousePressed(MouseEvent e)
      {
      }
     
      @Override
      public void mouseReleased(MouseEvent e)
      {
      }
     
      @Override
      public void mouseEntered(MouseEvent e)
      {
        this.requestFocus();
      }
     
      @Override
      public void mouseExited(MouseEvent e)
      {
        //sWindow.requestFocusInWindow()
        sWindow.updateTiles(tiles);
        sWindow.requestFocus();
      }
     
      @Override
      public void mouseDragged(MouseEvent e)
      {
      //  updateHistogram(tiles);
        myMouse = this.getMousePosition();
        topLayer=sWindow.isLayered();
     
        if(topLayer)
       {
        for(int k=0;k<4;k++)
        {
          if(tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][k]==-1)
          {
            tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][k] = sWindow.getSelected();
            break;
          }
        }
       }
       else
       {
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][0] = sWindow.getSelected();
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][1] = -1;
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][2] = -1;
         tiles[(myMouse.x - 10) / 20][(myMouse.y - 10) / 20][3] = -1;
       }
        this.repaint();
      }
     
      @Override
      public void mouseMoved(MouseEvent e)
      {
        // System.out.println("moved");
        this.repaint();
      }

    This is the code for the selection of images to be placed

    public class Selection extends JFrame
    {
     
      selectionPanel panel;
      JScrollPane scrollPane;
     
      public Selection(Map mapFrame) throws IOException
      {
        panel = new selectionPanel(this, mapFrame);
        //  panel.setLayout(new BorderLayout());
        this.getContentPane().setLayout(new BorderLayout());
        //   JLabel instructions = new JLabel("<html><ul><li>Type text.</li>"
        //           + "<li>Use arrow keys to move text.</li>"
        //           + "<li>Press shift arrows to move faster.</li></html>");
        //   this.getContentPane().add(instructions, BorderLayout.NORTH);
        this.setResizable(false);
        scrollPane = new JScrollPane(panel);
        //setViewport(panel);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        this.getContentPane().add(scrollPane, BorderLayout.WEST);
        panel.requestFocus();      // Give the panel focus.
        this.setTitle("Texture Selecter");
        this.pack();
      }
     
      public int getSelected()
      {
        //System.out.println(panel.selected);
        return panel.selected;
      }
     
      public ArrayList<BufferedImage> getBuffered()
      {
        return panel.bFS;
      }
     
      public boolean isLayered()
      {
        return panel.layerB.isSelected();
      }
     
      public void updateTiles(int[][][] updatedTiles)
      {
        panel.updatePanelTiles(updatedTiles);
      }
    }
     
    class selectionPanel extends JPanel implements MouseListener
    {
     
      Map map;
      boolean layered = false;
      ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>();
      protected ArrayList<JButton> buttons = new ArrayList<JButton>();
      ArrayList<BufferedImage> bFS = new ArrayList<BufferedImage>();
      int selected = 1;
      File file;
      BufferedImage tempImage;
      JRadioButton layerB;
      JButton bSave;
      JButton bBackground;
      JButton bOpen;
      int tiles[][][];
      Selection select;
     
      selectionPanel(Selection sel, Map mapFrame) throws IOException
      {
        map = mapFrame;
        select = sel;
        this.setBackground(Color.white);
        this.addMouseListener(this);
        this.setFocusable(true);    // Allow panel to get focus
     
        layerB = new JRadioButton("Layer");
        this.add(layerB);
        layerB.addMouseListener(this);
        bSave = new JButton("SAVE");
        bSave.addMouseListener(this);
        this.add(bSave);
        bOpen = new JButton("OPEN");
        bOpen.addMouseListener(this);
        this.add(bOpen);
        bBackground=new JButton("BACKGROUND");
        bBackground.addMouseListener(this);
        this.add(bBackground);
     
     
        icons.add(createImageIcon("textures/0.gif"));
        JButton tempButton = new JButton("DELETE", icons.get(0));
        buttons.add(tempButton);
        buttons.get(0).addMouseListener(this);
     
        buttons.get(0).setVerticalTextPosition(AbstractButton.BOTTOM);
        buttons.get(0).setHorizontalTextPosition(AbstractButton.CENTER);
        file = new File("textures/0.gif");
        tempImage = ImageIO.read(file);
        bFS.add(tempImage);
        this.add(buttons.get(0));
     
        int counter = 1;
     
        while (!(createImageIcon("textures/" + (counter) + ".gif") == null))
        {
          icons.add(createImageIcon("textures/" + (counter) + ".gif"));
     
          tempButton = new JButton("" + counter, icons.get(counter));
          buttons.add(tempButton);
          buttons.get(counter).addMouseListener(this);
     
          buttons.get(counter).setVerticalTextPosition(AbstractButton.BOTTOM);
          buttons.get(counter).setHorizontalTextPosition(AbstractButton.CENTER);
          //file=new File(RouteBuilderv2.class.getResource("textures/" + (counter) + ".gif").getFile());
          file = new File("textures/" + (counter) + ".gif");
        // System.out.println("textures/" + (counter) + ".gif");
        //System.out.println(file.canRead());
        //  System.out.println(file.getAbsoluteFile());
          tempImage = ImageIO.read(file);
          bFS.add(tempImage);
          this.add(buttons.get(counter));
          counter++;
          //adds each icon to array without hardcoding number of icons in
        }
        this.setPreferredSize(new Dimension(250, 15 * buttons.size()));
      }
     
      protected static ImageIcon createImageIcon(String path) throws MalformedURLException
      {
        //java.net.URL imgURL = RouteBuilderv2.class.getResource(path);
        //java.net.URL imgURL=new URL(path);
        File someFile=new File(path);
        if (someFile.exists())
        {
         //System.out.println(path);
          return new ImageIcon(path);
        }
        else
        {
          return null;
        }
      }
     
      public void updatePanelTiles(int[][][] updatedTiles)
      {
        tiles = updatedTiles;
      }
     
      @Override
      public void mouseClicked(MouseEvent e)
      {
     
        for (int i = 0; i < buttons.size(); i++)
        {
          if (buttons.get(i).equals(e.getComponent()))
          {
            selected = i;
            //System.out.println(selected);
          }
        }
        if(e.getComponent().equals(bBackground))
        {
          map.mPanel.fillBackground();
        }
        if (e.getComponent().equals(bSave))
        {
          Saver saver = new Saver(tiles);
          saver.setVisible(true);
          saver.requestFocus();
        }
        if (e.getComponent().equals(bOpen))
        {
          Opener opener = new Opener(select);
          opener.setVisible(true);
          opener.requestFocus();
     
        }
      }
     
      public void updateFileTiles(int[][][] fileTiles)
      {
        tiles = fileTiles;
        map.mPanel.tiles = tiles;
        map.mPanel.repaint();
      }
     
      @Override
      public void mousePressed(MouseEvent e)
      {
      }
     
      @Override
      public void mouseReleased(MouseEvent e)
      {
      }
     
      @Override
      public void mouseEntered(MouseEvent e)
      {
      }
     
      @Override
      public void mouseExited(MouseEvent e)
      {
      }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I save a bunch of images in a JPanel as one?

    See
    Lesson: Working with Images (The Java™ Tutorials > 2D Graphics)
    If I understand your requirements correctly, create a BufferedImage the size of the JPanel, pass the image Graphics to the JPanel's paint method, dispose of the Graphics object, and save the BufferedImage using ImageIO

  3. The Following User Says Thank You to copeg For This Useful Post:

    Yoyo_Guru (June 14th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I save a bunch of images in a JPanel as one?

    How do I make the BufferedImage hold the same information that the Graphics object is holding?

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I save a bunch of images in a JPanel as one?

    Quote Originally Posted by Yoyo_Guru View Post
    How do I make the BufferedImage hold the same information that the Graphics object is holding?
    I am not sure I understand. Create a BufferedImage of the appropriate size, call getGraphics() on the object, and pass this to the paintComponent method which paints to the Graphics object (eg to the object that created it, in this case the BufferedImage)

  6. The Following User Says Thank You to copeg For This Useful Post:

    Yoyo_Guru (June 14th, 2012)

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I save a bunch of images in a JPanel as one?

    Ok thanks a lot. That worked.

Similar Threads

  1. How to save Image into a package by JButton save?
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2012, 07:02 PM
  2. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  3. Save JPanel as image
    By fahien in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 08:20 AM
  4. problem with drawing images on JPanel
    By Asido in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 19th, 2010, 03:07 PM
  5. Hello ya bunch of programmers...
    By kaylors in forum Member Introductions
    Replies: 7
    Last Post: October 7th, 2009, 03:06 AM

Tags for this Thread