Results 1 to 7 of 7
  1. #1
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    Thank you for the reply. I am using an Applet to do this. Is there a way to do it without using JPanel?

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    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){ }

    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Replies: 2
    Last Post: 11-26-2010, 03:12 AM
  2. radio buttons and paint
    By gtraylo in forum Java Applets
    Replies: 1
    Last Post: 04-19-2008, 01:43 PM
  3. Replies: 1
    Last Post: 04-17-2008, 11:10 PM
  4. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04: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
  •