// // GamePaddle class // part of the set of documents known as Java no sugar. // Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // import java.awt.*; public class GamePaddle extends Canvas { //################################################################## // //################################################################## public static final int NONE_PRESSED = 0; public static final int PRESSED_NORTH = 1; public static final int PRESSED_EAST = 2; public static final int PRESSED_SOUTH = 4; public static final int PRESSED_WEST = 8; private static final double ROOT2 = 1.414213562373; private static final double FUDGEFACTOR = 1.66666; public int CLICK_INTERVAL = 100; //milliseconds int button_state = NONE_PRESSED; long button_down_time; //################################################################## // //################################################################## public GamePaddle() { } public int direction() { return(button_state); } //****************************************************************** // initial size must be set in addNotify, not in constructor or // tharr be null pointers public void addNotify() { super.addNotify(); resize(this.preferredSize()); } //****************************************************************** public void paint (Graphics g) { Dimension size; int x, y, text_width, text_height, oval_width, oval_height; Color colour; //-------------------------------------------------------------- size = size(); //------draw button shape --- g.setColor( getBackground() ); g.fillRect(0,0,size.width, size.height); switch(button_state) { case PRESSED_NORTH: paint_pressed_north(g); break; case PRESSED_EAST: paint_pressed_east(g); break; case PRESSED_SOUTH: paint_pressed_south(g); break; case PRESSED_WEST: paint_pressed_west(g); break; default: paint_pressed_none(g); } } //****************************************************************** private Polygon north_polygon() { Dimension size; Polygon polygon; int h1,h2,h3; int w1,w2,w3; size=CellSize(); h1 = size.height-1; h2 = h1+size.height; h3 = h2+size.height; w1 = size.width-1; w2 = w1+size.width; w3 = w2+size.width; polygon = new Polygon(); polygon.addPoint( w1 +4, 0); polygon.addPoint( w2 -4, 0); polygon.addPoint( w2 -2, h1); polygon.addPoint( w3 -2, h1); polygon.addPoint( w3, h2); polygon.addPoint( w2, h2); polygon.addPoint( w2 +2, h3); polygon.addPoint( w1 -2, h3); polygon.addPoint( w1, h2); polygon.addPoint( 0, h2); polygon.addPoint( 2, h1); polygon.addPoint( w1+2, h1); polygon.addPoint( w1+4, 0); return(polygon); } //****************************************************************** private Polygon west_polygon() { Dimension size; Polygon polygon; int h1,h2,h3; int w1,w2,w3; size=CellSize(); h1 = size.height-1; h2 = h1+size.height; h3 = h2+size.height; w1 = size.width-1; w2 = w1+size.width; w3 = w2+size.width; polygon = new Polygon(); polygon.addPoint( w1 , 2); polygon.addPoint( w2, 0); polygon.addPoint( w2, h1); polygon.addPoint( w3, h1 -2); polygon.addPoint( w3, h2 +2); polygon.addPoint( w2, h2); polygon.addPoint( w2, h3); polygon.addPoint( w1, h3-2); polygon.addPoint( w1, h2-2); polygon.addPoint( 0, h2-4); polygon.addPoint( 0, h1+4); polygon.addPoint( w1, h1 +2); polygon.addPoint( w1, 2); return(polygon); } //****************************************************************** private void paint_pressed_north(Graphics g) { paint_button_face(g,north_polygon()); } //****************************************************************** private void paint_pressed_east(Graphics g) { Polygon west_polygon, east_polygon; Dimension size; int npoints,point, y; west_polygon = west_polygon(); size=size(); npoints = west_polygon.npoints; // --------------------flip polygon------------------------ for (point=0; point < npoints; point++) west_polygon.xpoints[point] = size.width -west_polygon.xpoints[point]; //-------now anticlockwise, so reverse points-------------- east_polygon = new Polygon(); for (point=npoints-1; point >=0; point--) east_polygon.addPoint( west_polygon.xpoints[point], west_polygon.ypoints[point]); paint_button_face(g, east_polygon); } //****************************************************************** private void paint_pressed_south(Graphics g) { Polygon north_polygon, south_polygon; Dimension size; int npoints,point, y; north_polygon = north_polygon(); size=size(); npoints = north_polygon.npoints; // --------------------flip polygon------------------------ for (point=0; point < npoints; point++) north_polygon.ypoints[point] = size.height -north_polygon.ypoints[point]; //-------now anticlockwise, so reverse points-------------- south_polygon = new Polygon(); for (point=npoints-1; point >=0; point--) south_polygon.addPoint( north_polygon.xpoints[point], north_polygon.ypoints[point]); paint_button_face(g, south_polygon); } //****************************************************************** private void paint_pressed_west(Graphics g) { paint_button_face(g, west_polygon()); } //****************************************************************** private void paint_pressed_none(Graphics g) { Dimension size; Polygon polygon; int h1,h2,h3; int w1,w2,w3; size=CellSize(); h1 = size.height; h2 = h1+size.height; h3 = h2+size.height; w1 = size.width; w2 = w1+size.width; w3 = w2+size.width; polygon = new Polygon(); polygon.addPoint( w1, 0); polygon.addPoint( w2, 0); polygon.addPoint( w2, h1); polygon.addPoint( w3, h1); polygon.addPoint( w3, h2); polygon.addPoint( w2, h2); polygon.addPoint( w2, h3); polygon.addPoint( w1, h3); polygon.addPoint( w1, h2); polygon.addPoint( 0, h2); polygon.addPoint( 0, h1); polygon.addPoint( w1, h1); polygon.addPoint( w1, 0); paint_button_face(g,polygon); } //****************************************************************** private void paint_button_face(Graphics g, Polygon p) { int point; int x1,y1,x2,y2; int dx1,dy1,dx2,dy2; boolean first_point; //---draw the basic button shape------------------- g.setColor( Color.lightGray); g.fillPolygon(p); //---draw the dark grey shading on downward faces--- g.setColor( Color.gray); first_point = true; x1 = y1 = 0; for ( point =0; point < p.npoints; point++) { //- - - - - - - - - - - - - - - - - - - - - - - - - - - - x2 = p.xpoints[point]; y2 = p.ypoints[point]; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - if ((! first_point) & ( y2 > y1)) { if (x2 <= x1) //dark bit is above line { dx1 = -1; dy1 = 0; dx2 = 0; dy2 = 1; } else if (x2 == x1) { dx1 = -1; dy1 = 0; dx2 = -1; dy2 = 0; } else { dx1 = 0; dy1 = -1; dx2 = -1; dy2 = 0; } g.drawLine(x1+dx1, y1+dy1, x2+dx2 , y2+dy2); } else first_point = false; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - x1 = x2; y1 = y2; } //---draw the black edges of downward faces--- g.setColor( Color.black); first_point = true; x1 = y1 = 0; for ( point =0; point < p.npoints; point++) { //- - - - - - - - - - - - - - - - - - - - - - - - - - - - x2 = p.xpoints[point]; y2 = p.ypoints[point]; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - if ((! first_point) && (y2 > y1)) { g.drawLine(x1,y1,x2,y2); } else first_point = false; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - x1 = x2; y1 = y2; } //---draw the highlights on the upward faces-------- g.setColor( Color.white); first_point = true; x1 = y1 = 0; for ( point =0; point < p.npoints; point++) { //- - - - - - - - - - - - - - - - - - - - - - - - - - - - x2 = p.xpoints[point]; y2 = p.ypoints[point]; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - if ((! first_point) && (y2 <= y1)) { g.drawLine(x1,y1,x2,y2); } else first_point = false; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - x1 = x2; y1 = y2; } } //****************************************************************** private Dimension CellSize() { Dimension size; int cell_width, cell_height; size = size(); cell_width = size.width / 3; cell_height = size.width / 3; return new Dimension(cell_width, cell_height); } //****************************************************************** public Dimension minimumSize() { return ( new Dimension (100,100)); } //****************************************************************** public Dimension preferredSize() { return minimumSize(); } //****************************************************************** public boolean handleEvent(Event e) { long time_diff; int cell_number; Dimension cell_size; switch (e.id) { case Event.MOUSE_DOWN: button_down_time = System.currentTimeMillis(); cell_size = CellSize(); cell_number = 3 *(e.y / cell_size.height) + (e.x / cell_size.width); switch (cell_number) { case 1: button_state = PRESSED_NORTH; break; case 3: button_state = PRESSED_WEST; break; case 5: button_state = PRESSED_EAST; break; case 7: button_state = PRESSED_SOUTH; break; default: button_state = NONE_PRESSED; break; } if (button_state != NONE_PRESSED) repaint(); return true; case Event.MOUSE_UP: if (button_state != NONE_PRESSED) { time_diff = System.currentTimeMillis() - button_down_time; if (time_diff <= CLICK_INTERVAL) postEvent(new Event(this,Event.ACTION_EVENT,this)); button_state = NONE_PRESSED; repaint(); } return true; default: return super.handleEvent(e); } } }
file: /Techref/language/JAVA/nosugar/custom/paddle/code/GamePaddle.java, 10KB, , updated: 1996/11/1 22:58, local time: 2024/12/3 22:41,
3.14.250.187:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/Techref/language/JAVA/nosugar/custom/paddle/code/GamePaddle.java"> language JAVA nosugar custom paddle code GamePaddle</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.