LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-04-2009, 11:09 PM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
Java GUI and repaint() problem


Hello there,

I'm creating a java application with GUI which it draws 2 circles in a panel and when you click on a circle the color of the particular circle should change. I've got it so far to click inside the circle and change color but I call the repaint method to do that and as a result I get both circles changing a color

My code so far:
Code:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyPanel extends JPanel
{
    private MyShapes circle1 = new MyShapes();
    private MyShapes circle2 = new MyShapes();
    private JTextField positions = new JTextField(10);
    
    public MyPanel()
    {
	this.add(positions);
	this.addMouseListener(new MouseListener()
	{

	    @Override
	    public void mouseClicked(MouseEvent e)
	    {
		int x = e.getX();
		int y = e.getY();
		positions.setText("X:"+x+" Y:"+y);
		
		//if((x >= circle1.getX() && x<=circle1.getX()+circle1.getWidth()) && (y >= circle1.getY() && y<=circle1.getY()+circle1.getHeight()))
		if(Math.pow(x-(circle1.getX()+circle1.getRadius()), 2)+Math.pow(y-(circle1.getY()+circle1.getRadius()),2) <= Math.pow((double)(circle1.getWidth()/2), 2))
		    repaint();
		if(Math.pow(x-(circle2.getX()+circle2.getRadius()), 2)+Math.pow(y-(circle2.getY()+circle2.getRadius()),2) <= Math.pow((double)(circle2.getWidth()/2), 2))
		    repaint();
	    }

	    @Override
	    public void mouseEntered(MouseEvent e){}

	    @Override
	    public void mouseExited(MouseEvent e){}

	    @Override
	    public void mousePressed(MouseEvent e){}

	    @Override
	    public void mouseReleased(MouseEvent e) {}
	    
	});
    }
    
    public void paint(Graphics g)
    {
	circle1.getCircle(50, 50, 40, 40, g);
	circle2.getCircle(300, 50, 40, 40, g);
    }
}
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;


public class MyShapes
{
    private int x, y, width, height;
    private Color [] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
    Graphics g;
    
    public void getCircle(int x, int y, int width, int height, Graphics g)
    {
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	
	setColor(g);
	g.fillOval(x, y, width, height);
    }
    
    public void setColor(Graphics g)
    {
	Random generator = new Random();
	int randomColor = generator.nextInt(colors.length);
	g.setColor(colors[randomColor]);
    }
    
    // A method to return the x position of a shape object
    public int getX()
    {
	return x;
    }

    // A method to return the y position of a shape object
    public int getY()
    {
	return y;
    }

    // A method to return the width of a shape object
    public int getWidth()
    {
	return width;
    }
    
    // A method to return the height of a shape object
    public int getHeight()
    {
	return height;
    }
    
    // A method to return the radius of a circle
    public double getRadius()
    {
	return (double)(width/2);
    }
}
Code:
import javax.swing.JFrame;

public class showGUI2 extends JFrame
{
    private MyPanel panel1 = new MyPanel();
    
    public showGUI2(String title)
    {
	super(title);
	this.setContentPane(panel1);
    }
}
Code:
import javax.swing.JFrame;

public class Question2
{
    public static void main(String[] args)
    {
	showGUI2 run = new showGUI2("Random colours");
	run.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	run.setSize(400, 400);
	run.setVisible(true);
    }
}
Any suggestions of what I can do? An alternative way of repaint so I can get only the circle that I'm clicking to change color?

Thanks in advance
 
Old 03-05-2009, 06:21 AM   #2
healyma
Member
 
Registered: Feb 2009
Location: Mayo, Ireland
Distribution: LFS 6.4; Debian 5.4; Mythbuntu & Kubuntu 10.04
Posts: 49

Rep: Reputation: 18
Hi,
You need to move the lines from your paint method to the mouseclicked method (within the corressponding if statement.

At the moment your logic is as follows
if the mouse is clicked within Circle A -> go to repaint
in the mouse is clicked within Circle B -> go to repaint

repaint repaints both circles each time

set you if statement as follows

if the mouse is clicked within Circle A
fill Circle A
repaint

if the mouse is clicked within Circle B
Fill Circle B
repaint


Hope it helps
Mark
 
Old 03-05-2009, 12:34 PM   #3
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Yes but the problem is that paint is called on initialization and it needs a Graphics parameter. Which I don't have in the class.. Also I can only call such a method inside the listener, I can't implement it inside..
 
Old 03-05-2009, 03:38 PM   #4
healyma
Member
 
Registered: Feb 2009
Location: Mayo, Ireland
Distribution: LFS 6.4; Debian 5.4; Mythbuntu & Kubuntu 10.04
Posts: 49

Rep: Reputation: 18
Sorry - forgot about the reference to g.
I guess if you have 2 variables colorA and colorB, set them on initialization and then reset the appropriate one in the mouseClick Event - in other words if the mouse click is within circle A then set colorA to a random value, otherwise leave it as is, same with colorB.

Mark
 
Old 03-05-2009, 06:51 PM   #5
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Hi,

thanks for the reply but I managed with the help of a friend of course to do it in a different way. Actually I implemented my own MouseListener and I'm passing the shapes as a parameter and an integer identification of what type of a shape it is. Because I added 2 squares as well.

Also you need the Graphic reference to set the color again because the method is:
Code:
public void setColor(Graphics g)
{
        ...
        g.setColor[...];
}
Here is the code in case you wonder:
Code:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;


public class MyShapes
{
	private int x, y, width, height;
	private Color [] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW};
	
	public MyShapes(int x, int y, int width, int height)
	{
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	
	/*setColor(g);
	g.fillOval(x, y, width, height);*/
	}
	
	public void getCircle(Graphics g)
	{	
	setColor(g);
	g.fillOval(x, y, width, height);
	}
	
	public void getSquare(Graphics g)
	{	
	setColor(g);
	g.fillRect(x, y, width, height);
	}
	
	public void setColor(Graphics g)
	{
	Random generator = new Random();
	int randomColor = generator.nextInt(colors.length);
	g.setColor(colors[randomColor]);
	}

	// A method to return the x position of a shape object
	public int getX()
	{
	return x;
	}

	// A method to return the y position of a shape object
	public int getY()
	{
	return y;
	}

	// A method to return the width of a shape object
	public int getWidth()
	{
	return width;
	}
	
	// A method to return the height of a shape object
	public int getHeight()
	{
	return height;
	}
	
	// A method to return the radius of a circle
	public double getRadius()
	{
	return (double)(width/2);
	}
}
Code:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyPanel extends JPanel
{
	private MyShapes circle1 = new MyShapes(50, 50, 40, 40);
	private MyShapes circle2 = new MyShapes(300, 50, 40, 40);
	private MyShapes square1 = new MyShapes(50, 300, 50, 50);
	private MyShapes square2 = new MyShapes(300, 300, 50, 50);
	private final int circle = 0;
	private final int square = 1;
	private JTextField positions = new JTextField(10);
	
	public MyPanel()
	{
	this.add(positions);
	this.addMouseListener(new MyListener(this, circle1, circle));
	this.addMouseListener(new MyListener(this, circle2, circle));
	this.addMouseListener(new MyListener(this, square1, square));
	this.addMouseListener(new MyListener(this, square2, square));
	}
	
	public class MyListener implements MouseListener
	{
	JPanel panel;
	MyShapes shape;
	int x, y, onShape;
	
	public MyListener(JPanel panel, MyShapes shape, int onShape)
	{
		this.panel = panel;
		this.shape = shape;
		this.onShape = onShape;
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		Graphics g = panel.getGraphics();
		x = e.getX();
		y = e.getY();
		positions.setText("X:"+x+" Y:"+y);
		
		if(onShape == 0)
		{
		if(Math.pow(x-(shape.getX()+shape.getRadius()), 2)+Math.pow(y-(shape.getY()+shape.getRadius()),2) <= Math.pow(shape.getRadius(), 2))
			shape.getCircle(g);
		} else if (onShape == 1)
		{
		if((x >= shape.getX() && x<=shape.getX()+shape.getWidth()) && (y >= shape.getY() && y<=shape.getY()+shape.getHeight()))
			shape.getSquare(g);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e){}

	@Override
	public void mouseExited(MouseEvent e){}

	@Override
	public void mousePressed(MouseEvent e){}

	@Override
	public void mouseReleased(MouseEvent e){}
	}
	
	public void paint(Graphics g)
	{
	circle1.getCircle(g);
	circle2.getCircle(g);
	square1.getSquare(g);
	square2.getSquare(g);
	}
}
Code:
import javax.swing.JFrame;

public class showGUI2 extends JFrame
{
	private MyPanel panel1 = new MyPanel();
	
	public showGUI2(String title)
	{
	super(title);
	this.setContentPane(panel1);
	}
}
Code:
import javax.swing.JFrame;

public class Question2
{
	public static void main(String[] args)
	{
	showGUI2 run = new showGUI2("Random colours");
	run.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	run.setSize(400, 400);
	run.setVisible(true);
	}
}

Last edited by Cyhaxor; 03-05-2009 at 06:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
acroread scrollbar does not repaint correctly arobinson74 Linux - Software 4 11-09-2007 12:53 PM
Java - repaint on tabbedpane hunterfighter Programming 7 09-25-2006 06:46 AM
xlib and child windows (repaint) SteQve Programming 5 03-10-2005 11:31 AM
edonkey java gui not finding java Sephiroth Linux - Software 0 03-13-2004 08:14 PM
Delay on Frame Repaint in kwin... the.jxc Linux - General 0 08-03-2003 08:52 PM


All times are GMT -5. The time now is 10:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration