Results 1 to 4 of 4
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default When I resize window color of rectangles is changed

    Hi.
    I have written a program with 20x20 rectangles (or squares).
    They have initial red color.
    When I click on rectangle color of rectangle is changed to green.
    Program works fine, but when I resize window (frame), or do something else with frame, green rectangles become white.

    How to solve this problem?

    Java Code:
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class DrawRectangles {
    	
    	
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				MyFrame frame = new MyFrame();
    				frame.setVisible(true);
    			}
    		});
    	}
    }
    class MyFrame extends JFrame {
    	MyDrawPanel panel = new MyDrawPanel(20,20);
    	
    	MyFrame() {
    		setSize(300,300);
    		setLocation(100,100);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().add(panel);
    	}
    }
    
    class MyDrawPanel extends JPanel implements MouseListener {
    	private int xCoord;
    	private int yCoord;
    	private int numberOfRows = 20;
    	private int numberOfColumns = 20;
    	private int positionInRow;
    	private int positionInColumn;
    	private static final int xStartCoord = 15;
    	private static final int yPocetnaKoordinata = 15;
    	private static final int widthOfRectangle = 15;
    	private static final int heightOfRectangle = 15;
    	boolean[][] populatedRectangle;
    	
    	public MyDrawPanel(int numRows, int numColumns) {
    		numberOfRows = numRows;
    		numberOfColumns = numColumns;
    		populatedRectangle = new boolean[numColumns][numRows];
    		for(int i = 0; i < numRows; i++)
    			for(int j = 0; j < numColumns; j++)
    				populatedRectangle[i][j] = false;
    	}
    	public void paint(Graphics g) {
    		addMouseListener(this);
    		for(int i = 0; i < numberOfRows; i++)
    			for(int j = 0; j < numberOfColumns; j++) {
    				if(!populatedRectangle[i][j]) {
    					g.setColor(Color.RED);
    					g.fillRect(xStartCoord + (widthOfRectangle * i), yPocetnaKoordinata + (heightOfRectangle * j), 
    					widthOfRectangle - 1, heightOfRectangle - 1);
    				}
    				else {
    					g.setColor(Color.GREEN);
    					g.fillRect(xStartCoord+(widthOfRectangle * positionInRow), 
    							yPocetnaKoordinata + (heightOfRectangle * positionInColumn), widthOfRectangle - 1, heightOfRectangle - 1);
    				}
    			}
    	}
    	
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		xCoord = e.getX();
    		yCoord = e.getY();
    		positionInRow = (xCoord - xStartCoord) / widthOfRectangle;
    		positionInColumn = (yCoord - yPocetnaKoordinata) / heightOfRectangle;
    		
    		if(!populatedRectangle[positionInRow][positionInColumn]) {
    			populatedRectangle[positionInRow][positionInColumn] = true;
    		}
    		
    		repaint();
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default

    A few remarks:

    1) don't keep adding those listeners in your paint method; remove that method call and stick it in the initialization part of your class.

    2) don't override the paint( ... ) method, override the paintComponent( ... ) method instead.

    3) the painting of your green rectangles is incorrect; it should be identical to the painting of a red rectangle (a few lines above).

    kind regards,

    Jos

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Thank you.
    Your remarks was very useful.
    The problem is solved.

    Working code:
    Java Code:
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import java.awt.Graphics;
    import java.awt.Color;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class DrawRectangles {
    	
    	
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				MyFrame frame = new MyFrame();
    				frame.setVisible(true);
    			}
    		});
    	}
    }
    class MyFrame extends JFrame {
    	MyDrawPanel panel = new MyDrawPanel(20,20);
    	
    	MyFrame() {
    		setSize(300,300);
    		setLocation(100,100);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		getContentPane().add(panel);
    	}
    }
    
    class MyDrawPanel extends JPanel implements MouseListener {
    	private int xCoord;
    	private int yCoord;
    	private int numberOfRows = 20;
    	private int numberOfColumns = 20;
    	private int positionInRow;
    	private int positionInColumn;
    	private static final int xStartCoord = 15;
    	private static final int yStartCoord = 15;
    	private static final int widthOfRectangle = 15;
    	private static final int heightOfRectangle = 15;
    	boolean[][] populatedRectangle;
    	
    	public MyDrawPanel(int numRows, int numColumns) {
    		numberOfRows = numRows;
    		numberOfColumns = numColumns;
    		populatedRectangle = new boolean[numColumns][numRows];
    		for(int i = 0; i < numRows; i++)
    			for(int j = 0; j < numColumns; j++)
    				populatedRectangle[i][j] = false;
    		addMouseListener(this);
    	}
    	public void paintComponent(Graphics g) {
    		for(int i = 0; i < numberOfRows; i++)
    			for(int j = 0; j < numberOfColumns; j++) {
    				if(!populatedRectangle[i][j]) {
    					g.setColor(Color.RED);
    					g.fillRect(xStartCoord + (widthOfRectangle * i), yStartCoord + (heightOfRectangle * j), 
    					widthOfRectangle - 1, heightOfRectangle - 1);
    				}
    				else if(populatedRectangle[i][j]) {
    					g.setColor(Color.GREEN);
    					g.fillRect(xStartCoord+(widthOfRectangle * i), 
    							yStartCoord + (heightOfRectangle * j), widthOfRectangle - 1, heightOfRectangle - 1);
    				}
    			}
    	}
    	
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		xCoord = e.getX();
    		yCoord = e.getY();
    		positionInRow = (xCoord - xStartCoord) / widthOfRectangle;
    		positionInColumn = (yCoord - yStartCoord) / heightOfRectangle;
    		
    		if(!populatedRectangle[positionInRow][positionInColumn]) {
    			populatedRectangle[positionInRow][positionInColumn] = true;
    		}
    		
    		repaint();
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default

    Yep, that's exactly it; good luck with the rest of your code.

    kind regards,

    Jos

Similar Threads

  1. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 04:13 AM
  2. Window resize icon...
    By pele in forum SWT / JFace
    Replies: 3
    Last Post: 06-09-2008, 09:31 AM
  3. Replies: 1
    Last Post: 06-06-2008, 08:55 AM
  4. Replies: 1
    Last Post: 02-17-2008, 12:43 AM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •