3 Replies - 324 Views - Last Post: 14 September 2012 - 05:31 PM Rate Topic: -----

#1 M4trixSh4d0w  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 79
  • Joined: 07-May 10

Java JScrollPane update image

Posted 14 September 2012 - 04:10 PM

Hi, I'm making a program that has an image that you scroll around on, and I can't figure out how to update the image if a button is pressed (For example: Adds a Green Ellipse to the image.) It already draws the image into the JScrollPane and you can scroll around, but when you click a button it doesn't refresh the image. (more details in code)
Here is the code:

public class PegMaster extends JPanel implements ActionListener {
	//Note: not complete code
	public PegBox[] pegbox = new PegBox[9];
	
	public static Dimension size = new Dimension(520, 500);
	
	public BufferedImage canvas;
	public Graphics2D g2d;
	public JScrollPane scroller;
	JPanel panel;
	private Canvas window;
	
	JScrollPane pictureScrollPane;
	
	public PegMaster() {
		
		JButton button = new JButton("test");
		button.addActionListener(this);
		add(button);
		
		canvas = new BufferedImage((int)size.getWidth()-30, 75 * GUESSES, BufferedImage.TYPE_INT_RGB);
		g2d = canvas.createGraphics();
	for(int i = 0;i<=pegbox.length-1;i++) {
        	pegbox[i] = new PegBox(i, g2d);
        }
	window = new Canvas(new ImageIcon(toImage(canvas)), 1);//Class Canvas is a Scrollable JLabel to draw to (the image)
        pictureScrollPane = new JScrollPane(window);
        pictureScrollPane.setPreferredSize(new Dimension((int)size.getWidth()-10, (int)size.getHeight()-20));
        pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
        add(pictureScrollPane);
//adds the scrollpane, but can't update the image in it
	}
	
	
	
	public static void main(String args[]) {
		 SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                createGUI();//just adds the scrollpane
	            }
	        });
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		
		for(int i = 0;i<=pegbox.length-1;i++) {
        //	pegbox[i] = new PegBox(i);
        	pegbox[i].draw(g2d);
        	
        }
		try {
			Thread.sleep(20);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}	

		//tried re-making the scrollpane, didn't work.
//		window = new Canvas(new ImageIcon(toImage(canvas)), 1);
  //      pictureScrollPane = new JScrollPane(window);
      //  pictureScrollPane.setPreferredSize(new Dimension((int)size.getWidth()-10, (int)size.getHeight()-20));
        //pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));
		//tried imageupdate: pictureScrollPane.imageUpdate(canvas, 0, 0, 0, (int)size.getWidth()-10, (int)size.getHeight()-20);
       // remove(pictureScrollPane);
//tried this: pictureScrollPane.revalidate();
        
		repaint();
		
	}
	


 
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Java JScrollPane update image

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1984
  • View blog
  • Posts: 4,827
  • Joined: 10-September 10

Re: Java JScrollPane update image

Posted 14 September 2012 - 04:30 PM

Can you show us the version that actually has the functionality you describe? From there, describe what you'd like it to do with any code you've tried to get there. What you posted is limited, and it's hard to tell what you had working and then what you tried to add to it that broke it. It looks all broken as it is.
Was This Post Helpful? 0
  • +
  • -

#3 M4trixSh4d0w  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 79
  • Joined: 07-May 10

Re: Java JScrollPane update image

Posted 14 September 2012 - 04:34 PM

I have a JScrollPane that scrolls around an Image, that is painted to from my other classes to make my main menu. And when a user clicks a button (outside of the scrollpane) i want to update the image inside of the JScrollPane, like draw a circle, but i can't figure out how to update the image in the JScrollPane. Sorry i have to go, ill post more details when i get back. Thanks for trying to help me.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8027
  • View blog
  • Posts: 31,161
  • Joined: 06-March 08

Re: Java JScrollPane update image

Posted 14 September 2012 - 05:31 PM

It is completly useless to Thread.sleep() in the paint() method.
paint() draws in memory, it is only when paint() exit that the hardware (the screen) is refreshed based on what was drwn in memory
calling repaint() from paint() is not a good idea neither. Use a Swing.Timer for that
http://docs.oracle.c...wing/Timer.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1