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) { } }