Making my own buttons in a pixelated engine in java -


i have been working on own 2d game using java. game made scratch , not using built-in java libraries create default button. stuck on problem can not figure out. in game using pixelated look, gui. have created following button class:

package com.theshaft.client.gui;  import java.awt.event.mouseevent;  import com.theshaft.client.gfx.screen;  public abstract class button {  protected int x; protected int y; protected int width; protected int height;  protected boolean ispressed; protected boolean ishovered;  protected boolean isactivated;  protected buttonmanager bm;  public button(buttonmanager bm, int x, int y, int w, int h) {      this.bm = bm;     this.x = x;     this.y = y;     this.width = w;     this.height = h; }  public void onactivate() {      this.isactivated = true; }  public abstract void tick();  public abstract void render(screen s);  public void onpress(mouseevent e) {      if(e.getx() >= x && e.getx() <= x + width &&         e.gety() >= y && e.gety() <= y + height) {          this.ispressed = true;     } }  public void onrelease(mouseevent e) {      if(ispressed) {          if(e.getx() >= x && e.getx() <= x + width &&             e.gety() >= y && e.gety() <= y + height) {              onactivate();         }     }      this.ispressed = false; }  public void onmove(mouseevent e) {      if(e.getx() >= x && e.getx() <= x + width &&         e.gety() >= y && e.gety() <= y + height) {              ishovered = true;     } else {          ishovered = false;         ispressed = false;     } }  public int getx() {      return x; }  public int gety() {      return y; }  public int getwidth() {      return width; }  public int getheight() {      return height; }  public boolean ispressed() {      return ispressed; }  public boolean ishovered() {      return ispressed; }  public boolean isactivated() {      if(isactivated) {          isactivated = false;         return true;     } else {          return false;     } } 

there 3 methods in there called onpress(), onrelease() , onmove(). these methods keep track of mouse because pixels bigger on screen button in different location rendering. have tried putting getpixelsize() method in main class. have idea how can fix button 'collission box'?

to clarify more: use following 2 lines in main class:

    /** image rendered onto jframe */     private bufferedimage image = new bufferedimage(width,height,bufferedimage.type_int_argb);      /** pixels array, holding pixels should rendered */     private int[] pixels = ((databufferint) image.getraster().getdatabuffer()).getdata(); 

every time render game, update pixels array, holds rgb values pixels on screen should have. array updates image, stretch out on whole game screen , draw image onto screen.

if familiar type of rendering, please tell me how managed buttons working.

ok, fixed own problem. ended using getpixelwidth() , getpixelheight() method scale buttons desired size. these methods follows:

public double getpixelsizew() {      double var = ((double) frame.getcontentpane().getwidth()) / this.width;      return var; } 

my new button class looks follows:

   package com.theshaft.client.gui;     import java.awt.event.mouseevent;     import com.theshaft.client.gfx.screen;     public abstract class button {  protected int x; protected int y; protected int width; protected int height;  protected boolean ispressed; protected boolean ishovered;  protected boolean isactivated;  protected buttonmanager bm;  public button(buttonmanager bm, int x, int y, int w, int h) {      this.bm = bm;     this.x = x;     this.y = y;     this.width = w;     this.height = h; }  public void onactivate() {      this.isactivated = true; }  public abstract void tick();  public abstract void render(screen s);  public void onpress(mouseevent e) {      double pw = bm.getpanel().getpixelsizew();     double ph = bm.getpanel().getpixelsizeh();      if(e.getx() >= x * pw && e.getx() <= x * pw + width * pw &&         e.gety() >= y * ph && e.gety() <= y * ph + y * ph) {          this.ispressed = true;     } }  public void onrelease(mouseevent e) {      double pw = bm.getpanel().getpixelsizew();     double ph = bm.getpanel().getpixelsizeh();      if(ispressed) {          if(e.getx() >= x * pw && e.getx() <= x * pw + width * pw &&             e.gety() >= y * ph && e.gety() <= y * ph + y * ph) {              onactivate();         }     }      this.ispressed = false; }  public void onmove(mouseevent e) {      double pw = bm.getpanel().getpixelsizew();     double ph = bm.getpanel().getpixelsizeh();      if(e.getx() >= x * pw && e.getx() <= x * pw + width * pw &&         e.gety() >= y * ph && e.gety() <= y * ph + y * ph) {              ishovered = true;     } else {          ishovered = false;         ispressed = false;     } }  public int getx() {      return x; }  public int gety() {      return y; }  public int getwidth() {      return width; }  public int getheight() {      return height; }  public boolean ispressed() {      return ispressed; }  public boolean ishovered() {      return ispressed; }  public boolean isactivated() {      if(isactivated) {          isactivated = false;         return true;     } else {          return false;     } } 

}

thank helping out if did , hope people find usefull. works if manually resize frame, problem before.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -