Sunteți pe pagina 1din 2

import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.

*; public class ChessGameDemo extends JFrame implements Mouse istener! MouseMotion istener " J a#ered$ane la#ered$ane; J$anel chess%oard; J abel chess$iece; int x&djustment; int #&djustment; public ChessGameDemo'(" Dimension board)i*e + new Dimension',--! ,--(; .. /se a a#ered $ane 0or this this application la#ered$ane + new J a#ered$ane'(; getContent$ane'(.add'la#ered$ane(; la#ered$ane.set$re0erred)i*e'board)i*e(; la#ered$ane.addMouse istener'this(; la#ered$ane.addMouseMotion istener'this(; ..&dd a chess board to the a#ered $ane chess%oard + new J$anel'(; la#ered$ane.add'chess%oard! J a#ered$ane.D1F&/ 23 &415(; chess%oard.set a#out' new Grid a#out'6! 6( (; chess%oard.set$re0erred)i*e' board)i*e (; chess%oard.set%ounds'-! -! board)i*e.width! board)i*e.height(; 0or 'int i + -; i 7 ,8; i99( " J$anel s:uare + new J$anel' new %order a#out'( (; chess%oard.add' s:uare (; int row + 'i . 6( ; <; i0 'row ++ -( s:uare.set%ac=ground' i ; < ++ - > Color.blue ? Color.white (; else s:uare.set%ac=ground' i ; < ++ - > Color.white ? Color.blue (; @ .. &dd a 0ew pieces to the board J abel piece + new J abel' new AmageAcon'B.home.vinod.amarexamples.chess.jpgB( (; J$anel panel + 'J$anel(chess%oard.getComponent'-(; panel.add'piece(; piece + new J abel'new AmageAcon'B.home.vinod.amarexamples.chessC.jpgB((; panel + 'J$anel(chess%oard.getComponent'CD(; panel.add'piece(; piece + new J abel'new AmageAcon'B.home.vinod.amarexamples.=ing.jpgB((; panel + 'J$anel(chess%oard.getComponent'C,(; panel.add'piece(; piece + new J abel'new AmageAcon'B.home.vinod.amarexamples.camel.jpgB((; panel + 'J$anel(chess%oard.getComponent'<-(; panel.add'piece(; @ public void mouse$ressed'Mouse1vent e(" chess$iece + null; Component c + chess%oard.0indComponent&t'e.getE'(! e.get4'((; i0 'c instanceo0 J$anel(

return; $oint parent ocation + c.get$arent'(.get ocation'(; x&djustment + parent ocation.x F e.getE'(; #&djustment + parent ocation.# F e.get4'(; chess$iece + 'J abel(c; chess$iece.set ocation'e.getE'( 9 x&djustment! e.get4'( 9 #&djustment(; chess$iece.set)i*e'chess$iece.getGidth'(! chess$iece.getHeight'((; la#ered$ane.add'chess$iece! J a#ered$ane.D5&G3 &415(; @ ..Move the chess piece around public void mouseDragged'Mouse1vent me( " i0 'chess$iece ++ null( return; chess$iece.set ocation'me.getE'( 9 x&djustment! me.get4'( 9 #&djustment(; @ ..Drop the chess piece bac= onto the chess board public void mouse5eleased'Mouse1vent e( " i0'chess$iece ++ null( return; chess$iece.setIisible'0alse(; Component c + chess%oard.0indComponent&t'e.getE'(! e.get4'((; i0 'c instanceo0 J abel(" Container parent + c.get$arent'(; parent.remove'-(; parent.add' chess$iece (; @ else " Container parent + 'Container(c; parent.add' chess$iece (; @ chess$iece.setIisible'true(; @ public void mouseClic=ed'Mouse1vent e( " @ public void mouseMoved'Mouse1vent e( " @ public void mouse1ntered'Mouse1vent e(" @ public void mouse1xited'Mouse1vent e( " @ public static void main')tringJK args( " JFrame 0rame + new ChessGameDemo'(; 0rame.setDe0aultCloseLperation'DA)$L)13LM3C L)1 (; 0rame.pac='(; 0rame.set5esi*able'true(; 0rame.set ocation5elative2o' null (; 0rame.setIisible'true(; @ @

S-ar putea să vă placă și