swing - Java keeps giving me my coordinates multiplied by 93 -


when set x , y values array of jbuttons, correct values multiplied 93. can solve problem dividing value 93 rather find out bug in first place.

i have 2 classes in code, 1 actual program, , 1 button object along coordinates.

here's code:

import javax.swing.*; import java.awt.*; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.gridlayout; import java.io.*; public class connectfour implements actionlistener {      jframe frame = new jframe();      button [][] buttons = new button[6][7];      public connectfour()      {          frame.setsize(700,600);          frame.setdefaultcloseoperation(jframe.exit_on_close);          frame.setlayout(new gridlayout(6,7));          frame.setlocationrelativeto(null);          frame.settitle("connect four");          for(int filler = 0; filler <= 5; filler++)          {                  for(int filler2 = 0; filler2 <= 6; filler2++)                {                    buttons[filler][filler2] = new button();                    buttons[filler][filler2].setx(filler2);                    buttons[filler][filler2].sety(filler);                    //system.out.println(buttons[filler][filler2].getx());                    //system.out.print(buttons[filler][filler2].gety());                    frame.add(buttons[filler][filler2].button);                    buttons[filler][filler2].button.addactionlistener(this);               }          }          frame.setvisible(true);      }      public void actionperformed(actionevent a)      {          jbutton pressedbutton = (jbutton)a.getsource();          system.out.print(pressedbutton.gety() / 93);          system.out.print(pressedbutton.getx() / 93);      }      public static void main(string args[])      {           connectfour gameplay = new connectfour();      } } 

here's button class:

import javax.swing.jbutton; public class button {     jbutton button;     private int x = 0;     private int y = 0;     public button()     {         button = new jbutton();     }     public int getx() {return x;}     public int gety() {return y;}     public void setx(int xindex)      {         x = xindex;     }     public void sety(int yindex)      {         y = yindex;     } }  

you're mixing 2 button classes.

in line, you're adding actionlistener button.button:

buttons[filler][filler2].button.addactionlistener(this); 

because jbutton has methods getx , gety, can call them. when do:

pressedbutton.getx() 

you're getting x position of jbutton, not of button.

what think easiest way solve problem making button extend jbutton , rename x , y row , column, instance:

public class button extends jbutton {     private int row = 0;     private int column = 0;      public button(int row, int column) {         super();          this.row = row;         this.column = column;         }     public int getrow() {return row;}     public int getcolumn() {return column;} }  

you can create buttons as

for(int filler = 0; filler <= 5; filler++) {        for(int filler2 = 0; filler2 <= 6; filler2++) {         buttons[filler][filler2] = new button(filler2, filler);         frame.add(buttons[filler][filler2]);         buttons[filler][filler2].addactionlistener(this);     } } 

and use them in actionlistener as

public void actionperformed(actionevent a) {     button pressedbutton = (button)a.getsource();     system.out.print(pressedbutton.getcolumn());     system.out.print(pressedbutton.getrow()); } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -