Results 1 to 7 of 7
- 06-05-2008, 02:45 AM #1
Member
- Join Date
- May 2008
- Posts
- 6
- Rep Power
- 0
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
-
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.
- 06-05-2008, 03:33 AM #3
Member
- Join Date
- May 2008
- Posts
- 6
- Rep Power
- 0
Thank you for the reply. I am using an Applet to do this. Is there a way to do it without using JPanel?
- 06-05-2008, 04:08 AM #4
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
-
- 06-05-2008, 06:59 AM #6
Member
- Join Date
- May 2008
- Posts
- 6
- Rep Power
- 0
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){ }
}
-
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.
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
radio buttons and paint
By gtraylo in forum Java AppletsReplies: 1Last Post: 04-19-2008, 12:43 PM -
[SOLVED] If a object equals another object, do they contain the same data?
By bobleny in forum New To JavaReplies: 1Last Post: 04-17-2008, 10:10 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks