Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: StumpyStrust (posted 2013-09-11 01:35:33, viewed 140 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229  
package com.game.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import com.game.main.Actor;
import com.game.main.StateManager;
import com.game.main.Window;
import com.game.utils.GameUtils;

public class Button extends Component {

   private static AlphaComposite alphaComp;
   
   
   protected Action          actionActivate;
   protected Action          actionDeactivate;
   protected Action          actionClick;
   private boolean       active             = false;
   protected boolean       useText          = false;
   private float          fade             = 1;
   private int          fx                = -1;
   
   protected String       text            = "Text";
   protected Font          f                = Font.getFont("Test");
   protected Color       c                = Color.black;
   protected Rectangle2D    textBounds; 
   
   public Button(int x, int y, int width, int height, BufferedImage[] image)
   {
      super(x,y,width,height,new Actor(image));
   }
   
   public void setImage(BufferedImage[] i)
   {
      image.setImages(i);
   }
   
   public int getFX()
   {
      return fx;
   }
   
   public void setFX(int num)
   {
      fx = num;
   }
   
   public void setColor(Color t)
   {
      c = t;
   }
   
   public void setActive(boolean c)
   {
      active = c;
   }
   
   public void setText(String str)
   {
      text = str;
   }
   
   public void setFont(Font t)
   {
      f = t;
   }
   
   public void setLabel(String str, Color col, Font t)
   {
      f = t;
      text = str;
      c = col;
      useText = true;
      StateManager.getGraphics().setFont(t);
      textBounds = StateManager.getGraphics().getFontMetrics().getStringBounds(text, StateManager.getGraphics());
   }
   
   public void setText(boolean c)
   {
      useText = c;
   }
   
   public void setActionActivate(Action act)
   {
      actionActivate = act;
   }
   
   public void setActionDeactivate(Action act)
   {
      actionDeactivate = act;
   }
   
   public void setActionClick(Action act)
   {
      actionClick = act;
   }
   
   public void setTrans( float num )
   {
      if(num >= 0 && num <= 1)
         fade = num;
   }
   
   public Rectangle2D getTextBounds()
   {
      return textBounds;
   }
   
   public void render(Graphics g)
   {
      Graphics2D g2d = (Graphics2D) g.create();
      alphaComp = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, (fade));
      g2d.setComposite(alphaComp);
      if( !active && image.getImage(0) != null)
      {
         g2d.drawImage(image.getImage(0),x,y,width,height,null);
      }
      else if(image.getImage(1) != null)
      {
         g2d.drawImage(image.getImage(1),x,y,width,height,null);
      }
      else if(active)
      {
         g2d.setColor(Color.RED);
         g2d.fillRect(x, y, width, height);
      }
      else
      {
         g2d.setColor(Color.BLUE);
         g2d.fillRect(x, y, width, height);
      }
      if(useText)
      {
         if(active)
            g2d.setColor(c.brighter());
         else
            g2d.setColor(c);
         g2d.setFont(f);
         g2d.drawString(text, (int)((x+(width/2)) - textBounds.getWidth()/2), (int)((y + height/2 - textBounds.getHeight()/2)+height*.5));
      }
      g2d.dispose();
   }
   
   public void update()
   {
      if(active && StateManager.mouseClick() && actionClick != null)
      {
            actionClick.action(this);
            StateManager.setMouseClick(false);
            return;
      }
      if(active && GameUtils.collision(x, y, width, height, Window.mouseX, Window.mouseY, 1, 1) ) 
         return;
      else if( GameUtils.collision(x, y, width, height, Window.mouseX, Window.mouseY, 1, 1))
      {
         active  = true;

         if(actionActivate != null)
            actionActivate.action(this);
         return;
      }
      else
      {
         if(actionDeactivate != null)
            actionDeactivate.action(this);
         active  = false;
         return;
      }      
   }

}


package com.game.ui;

import java.awt.Graphics;

import com.game.main.Actor;

public class Component {
   
   public int x,y;
   public int width, height;
   public Actor image;
   
   public Component( int x1, int y1, int width1, int height1, Actor image1)
   {
      x = x1;
      y = y1;
      width = width1;
      height = height1;
      image = image1;
   }
   
   public void render( Graphics g )
   {
      if(image == null)
      {
         g.fillRect(x, y, width, height);
      }
   }
   
   public void update()
   {
      
   }
   
   public void resize(int newx, int newy, int sizewidth, int sizeheight)
   {
      x = newx;
      y = newy;
      width = sizewidth;
      height = sizeheight;
   }
   
   public void jump(int dx, int dy)
   {
      x = dx;
      y = dy;
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!