Results 1 to 7 of 7
  1. #1
    Verbiage is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Applet+Mouse click

    I am making a program that emulates some basic board games for a class. I recently began converting it to a graphic display from a textual display, using an Applet. Now I am using a mouse click on the game board to select new moves. It would be incredibly helpful if I could make the program wait for a mouseclick at a given line, for example right before running a method to place a new piece with x and y values given from the mouse click. I have a mouselistener that updates x and y values after registering a click. Is there any way to do this? I mean is there really no line that equates to waitformouseclick? Because I can't seem to find anything that will do this and it is going to take a TON of reorganizing of my program in order to accommodate that.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,970
    Rep Power
    8

    Default Re: Applet+Mouse click

    Why don't you just call that method from your MouseListener?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Verbiage is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Applet+Mouse click

    Because as it is I have several board games each with their own addpiece methods. And one of the big problems is how I deal with giving the user infinite tries to enter a valid move. Previously I just used a simple while(!valid) loop that constantly asks for new input put but as it is now I have no way to make it wait in that loop for a click input before checking the new move.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,970
    Rep Power
    8

    Default Re: Applet+Mouse click

    Not really sure what you mean. Why can't you just check the input after the MouseListener is triggered?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Verbiage is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Applet+Mouse click

    I have no problem posting the relevant code if it would help. It is ~300 lines or so. As it is right now the mouseclicked method simply updates the XInput and YInput values, nothing else, and I was hoping to make it wait for a click in pertinent areas. Although contrary to what I just said I also have a version that I started working on that tries to put the main body of code that runs the program inside the mouseclicked method but I still ran into the valid move issue.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,970
    Rep Power
    8

    Default Re: Applet+Mouse click

    If you post code, it should be in the form of an SSCCE.

    Again, why don't you just check where the user clicked, and then call the appropriate method?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Verbiage is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Applet+Mouse click

    This is the most relevant code segment. The cases 1+2 are simply the running code for the other two board games and it looks almost identical to case 0 so I left it out. The drawBoard methods were used for making the textual display. The addpieceO method corresponds with case 0 and the other two addPiece methods correspond with the two cases I edited out. The paint method just draws the board and pieces.

    Very specifically, the lines I add that are just solid *********** are the spots where I need the program to wait for a click.

    I know that it is probably very messy code, I'm sorry.

    Java Code:
    public class Board extends Applet{
        public static int height=0;
        public static int width=0;
        public static char[][] Chips;
        int XInput,YInput, OInput, test;
        boolean start = true;
       public static void fillBoard(int x, int y){
            height = y;
            width = x;
            char[][] t = new char[x][y];
            for(int i = 0;i < height; i++){
                for(int z=0;z<width;z++){
                    t[z][i] = ' ';
                }
            }
            if(x==8&&y==8){
            t[4][3] = 'X';
            t[3][4] = 'X';
            t[3][3] = 'O';
            t[4][4] = 'O';
            }
            Chips = t;
        }   
        @Override
        public void init() {
            setBackground(Color.lightGray);
            Scanner in = new Scanner(System.in);
    
            boolean win, win2 = false;
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    XInput = (e.getX()/100);
                    YInput = (e.getY()/100);
                    OInput = (e.getX()/100);
                    
                }
            });
            
            while (true){
                repaint();
                System.out.println("Click 0 for Othello, 1 for Connect Four, 2 for Tic Tac Toe, 3 to quit.");
                *****************
            switch (OInput) {
                case 0 :{
                    fillBoard(8,8);
                    repaint();
                    while((MoveChecker.possibleMove('X','O'))){   
                     if(MoveChecker.possibleMoveSingle('X', 'O')){
                            System.out.println("Player One's Turn. Click to move.");
                         ****************
                            addPieceO(XInput, YInput, 'X', 'O');
                         repaint();
                      }
                     else System.out.println("Player One has no possible moves.");
                     if(MoveChecker.possibleMoveSingle('O', 'X')){
                            System.out.println("Player Two's Turn. Enter X coordinate then Y coordinate.");
                         *****************    
                            addPieceO(XInput, YInput, 'O', 'X');
                            repaint();
                     }
                      else System.out.println("Player Two has no possible moves.");
                  }
                    repaint();
                 if(ScoreKeeper.totalScore('X')>ScoreKeeper.totalScore('O'))
                     System.out.println("Player One wins with "+
    				 ScoreKeeper.totalScore('X')+" to Player Two's " +
    				 ScoreKeeper.totalScore('O'));
                 if(ScoreKeeper.totalScore('O')>ScoreKeeper.totalScore('X'))
                     System.out.println("Player Two wins with "+
    				 ScoreKeeper.totalScore('O')+" to Player One's " +
    				 ScoreKeeper.totalScore('X'));
                 if(ScoreKeeper.totalScore('X')==ScoreKeeper.totalScore('O'))
                      System.out.println("Tie Game.");   
                 break;
                }   
                case 1:{
                case 2:{  
                case 3:{
                    break;
                }
            }
            }*/
       }
        @Override
        public void paint(Graphics g){
        public void drawBoardO(){
        public void drawBoardC4(){
        public void drawBoardTTT(){
        public void addPieceO(int x, int y, char c, char c2){
            Scanner in = new Scanner(System.in);
            int inX,inY;
            boolean valid = MoveChecker.validMoveA(x,y,c,c2);  
            if (valid) {
                Chips[x][y] = c;
                PieceFlipper.newPiece(x, y, c, c2);
            }
            else while(!valid){
                repaint();
                System.out.println("Invalid Move. Try again.");
                ****************
                if(valid = MoveChecker.validMoveA(inX = XInput,inY = YInput,c,c2)){ 
                    Chips[inX][inY] = c;
                    PieceFlipper.newPiece(inX, inY, c, c2);
                }
            }
        }
        public boolean addPieceC4(int x,int y,char c, char c2){
        public boolean addPieceTTT(int x, int y, char c, char c2){

Similar Threads

  1. Enable mouse right click menu pop up.
    By MarbenOrallo in forum NetBeans
    Replies: 2
    Last Post: 01-17-2012, 05:15 PM
  2. Mouse Click not working.
    By itcmanish in forum AWT / Swing
    Replies: 1
    Last Post: 01-12-2012, 03:25 PM
  3. Mouse click interrupting animation
    By rolledback in forum Java 2D
    Replies: 17
    Last Post: 06-16-2011, 11:01 PM
  4. mouse click listener from textfield
    By ryanmk54 in forum AWT / Swing
    Replies: 2
    Last Post: 05-11-2011, 02:22 AM
  5. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 10:42 PM

Posting Permissions

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