Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-05-2008, 04:45 AM
Member
 
Join Date: May 2008
Posts: 6
Devilsfutbol17 is on a distinguished road
How to make a MouseClick paint an object
I am trying to make a MouseClick color a circle. I declared the shape and I just want to make it so when you click it, it turns a different color. I'm trying to create a Connect4 game where when you click the circle that you want your move to be, it changes to that players color. Any help is appreciated. Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-05-2008, 05:27 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 900
Fubarable is on a distinguished road
I assume that you're using Swing here. One way is to have a grid of JPanels each with a MouseListener and with a paintComponent override so that when you click on the panel if the circle hasn't yet been set, then set it.

No matter how you tackle this though, I would strongly recommend that you think through and code the non-GUI model for this and debug it before trying to create the GUI code.

Good luck.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-05-2008, 05:33 AM
Member
 
Join Date: May 2008
Posts: 6
Devilsfutbol17 is on a distinguished road
Thank you for the reply. I am using an Applet to do this. Is there a way to do it without using JPanel?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-05-2008, 06:08 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
I reccommend what he said about doing the model first by I slighty disagree work on which ever one your more comfortable with and work with it untill u get stuck then try working on the model the great thing about programming is the fact that you can separate them. I would say that you should think of some sort of design.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-05-2008, 06:14 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 900
Fubarable is on a distinguished road
Quote:
Originally Posted by Devilsfutbol17 View Post
Thank you for the reply. I am using an Applet to do this. Is there a way to do it without using JPanel?
I recommend using a JApplet and still using the JPanel approach. This way, you could easily port the class to be used in a japplet, a jframe, a jdialog, or inside of another jpanel,... The increase in flexibility and power is worth it.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-05-2008, 08:59 AM
Member
 
Join Date: May 2008
Posts: 6
Devilsfutbol17 is on a distinguished road
Ok, I am very grateful for all the replies, but im basically retarded. I dont understand how to use the JPanel, I have messed with it in the past, but never got it to do what I wanted. This is what I have and I'm trying to create connect for in a simple way, nothing fancy. I am a complete noob and am doing extremely poor in my Java class. You guys will probably think I'm mentally ill after looking at this code, but this is what I got so far. Like I said earlier, I just want the mouseclick to change a circle's color. I'm trying to just get one circle to change right now so I know how to setup the rest. I'm sorry that I can't understand what you guys are trying to teach me to do. Thanks for the help you have given me and any more yet to come.




import java.awt.event.MouseAdapter;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.applet.*;

public class Connect4 extends Applet implements MouseListener{
int x = 405;
int y = 70;
int z = 70;
int u = 70;

// Shape circleShape = new Ellipse2D.Double(x,y,z,u);


public void init() {
// circleshape.addMouseListener;
addMouseListener(this);
;
}

public void paint(Graphics g)
{
Shape circleShape = new Ellipse2D.Double(x,y,z,u);
Graphics2D ga = (Graphics2D)g;

g.setColor(Color.yellow);
g.fillRect(300,60,600,500);

g.setColor(Color.white);
g.fillOval(325,70,70,70);
g.fillOval(325,150,70,70);
g.fillOval(325,230,70,70);
g.fillOval(325,310,70,70);
g.fillOval(325,390,70,70);
g.fillOval(325,470,70,70);

ga.fill(circleShape);
ga.draw(circleShape);
//g.fillOval(405,70,70,70);
g.fillOval(405,150,70,70);
g.fillOval(405,230,70,70);
g.fillOval(405,310,70,70);
g.fillOval(405,390,70,70);
g.fillOval(405,470,70,70);

g.fillOval(485,70,70,70);
g.fillOval(485,150,70,70);
g.fillOval(485,230,70,70);
g.fillOval(485,310,70,70);
g.fillOval(485,390,70,70);
g.fillOval(485,470,70,70);

g.fillOval(565,70,70,70);
g.fillOval(565,150,70,70);
g.fillOval(565,230,70,70);
g.fillOval(565,310,70,70);
g.fillOval(565,390,70,70);
g.fillOval(565,470,70,70);

g.fillOval(645,70,70,70);
g.fillOval(645,150,70,70);
g.fillOval(645,230,70,70);
g.fillOval(645,310,70,70);
g.fillOval(645,390,70,70);
g.fillOval(645,470,70,70);

g.fillOval(725,70,70,70);
g.fillOval(725,150,70,70);
g.fillOval(725,230,70,70);
g.fillOval(725,310,70,70);
g.fillOval(725,390,70,70);
g.fillOval(725,470,70,70);

g.fillOval(805,70,70,70);
g.fillOval(805,150,70,70);
g.fillOval(805,230,70,70);
g.fillOval(805,310,70,70);
g.fillOval(805,390,70,70);
g.fillOval(805,470,70,70);



}

public void mouseClicked(MouseEvent e)
{
/*circleShape = new Ellipse2D.Double(x,y,z,u);
Graphics2D ga = (Graphics2D)g;
ga.setColor(Color.blue);
ga.fill(circleShape);
x = 395;
y = 70;
z = 70;
u = 70;*/
repaint();

}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){ }

}
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-05-2008, 02:54 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 900
Fubarable is on a distinguished road
Much of good programming is about being lazy. You want to write as little code as possible to get the job done in a decent way. So, if I were a decent but lazy programmer, I'd try to simplify and shorten your code above with its lists of fillOval calls. I'd create a JPanel (or if you feel that you must stick with AWT, a Canvas), that holds one and only one circle, would have code in it that allows it to paint that circile if it is clicked on, and I'd create an array of these components and put them into my program.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
radio buttons and paint gtraylo Java Applets 2 05-21-2008 12:45 AM
[SOLVED] If a object equals another object, do they contain the same data? bobleny New To Java 1 04-18-2008 12:10 AM
Creating object of Type Object class venkatv New To Java 3 07-17-2007 05:33 PM
paint() and paintComponent() goldhouse Java 2D 1 07-17-2007 05:43 AM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 05:19 PM


All times are GMT +3. The time now is 03:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org