How to call a class that draws a circle in a MouseMoved method ??

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to call a class that draws a circle in a MouseMoved method ??

 
0
  #11
Feb 28th, 2008
Ok, this kind of goes in a much different direction than you started, but if you aren't committed to doing the entire thing with your own 2D graphics then leveraging Swing components to do some of the work for you will really reduce a lot of the complexity. This would allow you to concentrate more on the game itself and spend less time doing calculations for the graphics (area calcs, hit testing, etc).

I worked up this example that uses a grid of components that extend JLabel for the board and the "insertion row" (where the player hovers to drop a piece). These components do have custom painting code, but that is the only graphics work that needs to be done. If you are more interested in making the game work than learning the nuances of 2D graphics, I'd recommend going in this direction.
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.RenderingHints;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.geom.Ellipse2D;
  10. import javax.swing.BorderFactory;
  11. import javax.swing.JLabel;
  12.  
  13. public class Board extends javax.swing.JFrame {
  14. InsertPiece[] insertionRow;
  15. BoardPiece[][] board;
  16.  
  17. Player currentPlayer; // player for this turn
  18.  
  19. static final int ROWS = 5;
  20. static final int COLS = 6;
  21.  
  22. public Board() {
  23. initComponents();
  24.  
  25. buildBoard();
  26. setSize(500,500);
  27.  
  28. currentPlayer = new Player("Smilin Bob", Color.BLUE);
  29. }
  30.  
  31. private void buildBoard(){
  32. GridBagConstraints gbc = new GridBagConstraints();
  33. gbc.gridx=0;
  34. gbc.gridy=0;
  35. gbc.fill = GridBagConstraints.BOTH;
  36. gbc.weightx = 1;
  37. gbc.weighty = 1;
  38.  
  39. // build the insertion row above the board
  40. insertionRow = new InsertPiece[COLS];
  41. for (int col=0; col<COLS; col++){
  42. insertionRow[col] = new InsertPiece(col);
  43. insertionRow[col].setOpaque(false);
  44. getContentPane().add(insertionRow[col],gbc);
  45. gbc.gridx++;
  46. }
  47. // build board rows
  48. board = new BoardPiece[ROWS][COLS];
  49. for (int i=0; i<ROWS; i++){
  50. gbc.gridy++;
  51. gbc.gridx = 0;
  52. for (int j=0; j<COLS; j++){
  53. board[i][j] = new BoardPiece();
  54. board[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
  55. board[i][j].setOpaque(false);
  56. getContentPane().add(board[i][j],gbc);
  57. gbc.gridx++;
  58. }
  59. }
  60. pack();
  61. }
  62.  
  63. private void placePiece(int column){
  64. for(int row = ROWS-1; row >= 0; row--){
  65. BoardPiece piece = board[row][column];
  66. if (!piece.isOccupied()){
  67. piece.setPlayerColor(currentPlayer.getPlayColor());
  68. piece.setOccupied(true);
  69. break;
  70. }
  71. }
  72. repaint();
  73. }
  74.  
  75. class BoardPiece extends JLabel{
  76. Color playerColor = Color.RED;
  77. boolean occupied = false;
  78.  
  79. public BoardPiece(){
  80. super();
  81. Dimension size = new Dimension(50,50);
  82. setMaximumSize(size);
  83. setMinimumSize(size);
  84. setPreferredSize(size);
  85. }
  86.  
  87. public void setOccupied(boolean occupied){
  88. this.occupied = occupied;
  89. }
  90.  
  91. public boolean isOccupied(){
  92. return occupied;
  93. }
  94.  
  95. public void setPlayerColor(Color c){
  96. playerColor = c;
  97. }
  98.  
  99. public void paintComponent(Graphics g){
  100. super.paintComponent(g);
  101. Graphics2D g2 = (Graphics2D)g;
  102. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  103. Ellipse2D.Float ellipse = new Ellipse2D.Float(10,10,getWidth()-20,getHeight()-20);
  104. if (occupied){
  105. g2.setColor(playerColor);
  106. g2.fill(ellipse);
  107. } else {
  108. g2.setColor(Color.BLACK);
  109. g2.draw(ellipse);
  110. }
  111. }
  112. }
  113.  
  114. class InsertPiece extends JLabel implements MouseListener{
  115. int column=0;
  116. boolean filled = false;
  117. Color textColor = Color.WHITE;
  118.  
  119. public InsertPiece(int column){
  120. super();
  121. this.column = column;
  122. Dimension size = new Dimension(50,50);
  123. setMaximumSize(size);
  124. setMinimumSize(size);
  125. setPreferredSize(size);
  126. setHorizontalAlignment(CENTER);
  127. setForeground(textColor);
  128. addMouseListener(this);
  129. }
  130.  
  131. public void paintComponent(Graphics g){
  132. if (filled){
  133. Graphics2D g2 = (Graphics2D)g;
  134. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  135. g2.setColor(currentPlayer.getPlayColor());
  136. Ellipse2D.Float ellipse = new Ellipse2D.Float(10,10,getWidth()-20,getHeight()-20);
  137. g2.fill(ellipse);
  138. }
  139. // usually want to call super.paintComponent before any custom code
  140. // but here we want to render the circle first and let the label
  141. // paint it's text on top of the image
  142. super.paintComponent(g);
  143. }
  144.  
  145. public void mouseClicked(MouseEvent e) {
  146. setText(null);
  147. filled = false;
  148. placePiece(column);
  149. }
  150.  
  151. public void mousePressed(MouseEvent e) {
  152.  
  153. }
  154.  
  155. public void mouseReleased(MouseEvent e) {
  156.  
  157. }
  158.  
  159. public void mouseEntered(MouseEvent e) {
  160. setText("1"); // set whatever count you need here
  161. filled = true;
  162. repaint();
  163. }
  164.  
  165. public void mouseExited(MouseEvent e) {
  166. setText(null);
  167. filled = false;
  168. repaint();
  169. }
  170.  
  171. }
  172.  
  173. class Player {
  174. private String name;
  175. private Color playColor;
  176.  
  177. public Player(String name, Color playColor){
  178. this.name = name;
  179. this.playColor = playColor;
  180. }
  181.  
  182. public String getName() {
  183. return name;
  184. }
  185.  
  186. public void setName(String name) {
  187. this.name = name;
  188. }
  189.  
  190. public Color getPlayColor() {
  191. return playColor;
  192. }
  193.  
  194. public void setPlayColor(Color playColor) {
  195. this.playColor = playColor;
  196. }
  197.  
  198. }
  199.  
  200. /** This method is called from within the constructor to
  201.   * initialize the form.
  202.   * WARNING: Do NOT modify this code. The content of this method is
  203.   * always regenerated by the Form Editor.
  204.   */
  205. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  206. private void initComponents() {
  207.  
  208. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  209. getContentPane().setLayout(new java.awt.GridBagLayout());
  210.  
  211. pack();
  212. }// </editor-fold>
  213.  
  214. /**
  215.   * @param args the command line arguments
  216.   */
  217. public static void main(String args[]) {
  218. java.awt.EventQueue.invokeLater(new Runnable() {
  219. public void run() {
  220. new Board().setVisible(true);
  221. }
  222. });
  223. }
  224.  
  225. // Variables declaration - do not modify
  226. // End of variables declaration
  227.  
  228. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC