| | |
How to call a class that draws a circle in a MouseMoved method ??
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
java Syntax (Toggle Plain Text)
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.RenderingHints; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.Ellipse2D; import javax.swing.BorderFactory; import javax.swing.JLabel; public class Board extends javax.swing.JFrame { InsertPiece[] insertionRow; BoardPiece[][] board; Player currentPlayer; // player for this turn static final int ROWS = 5; static final int COLS = 6; public Board() { initComponents(); buildBoard(); setSize(500,500); currentPlayer = new Player("Smilin Bob", Color.BLUE); } private void buildBoard(){ GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx=0; gbc.gridy=0; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1; gbc.weighty = 1; // build the insertion row above the board insertionRow = new InsertPiece[COLS]; for (int col=0; col<COLS; col++){ insertionRow[col] = new InsertPiece(col); insertionRow[col].setOpaque(false); getContentPane().add(insertionRow[col],gbc); gbc.gridx++; } // build board rows board = new BoardPiece[ROWS][COLS]; for (int i=0; i<ROWS; i++){ gbc.gridy++; gbc.gridx = 0; for (int j=0; j<COLS; j++){ board[i][j] = new BoardPiece(); board[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK)); board[i][j].setOpaque(false); getContentPane().add(board[i][j],gbc); gbc.gridx++; } } pack(); } private void placePiece(int column){ for(int row = ROWS-1; row >= 0; row--){ BoardPiece piece = board[row][column]; if (!piece.isOccupied()){ piece.setPlayerColor(currentPlayer.getPlayColor()); piece.setOccupied(true); break; } } repaint(); } class BoardPiece extends JLabel{ Color playerColor = Color.RED; boolean occupied = false; public BoardPiece(){ super(); Dimension size = new Dimension(50,50); setMaximumSize(size); setMinimumSize(size); setPreferredSize(size); } public void setOccupied(boolean occupied){ this.occupied = occupied; } public boolean isOccupied(){ return occupied; } public void setPlayerColor(Color c){ playerColor = c; } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); Ellipse2D.Float ellipse = new Ellipse2D.Float(10,10,getWidth()-20,getHeight()-20); if (occupied){ g2.setColor(playerColor); g2.fill(ellipse); } else { g2.setColor(Color.BLACK); g2.draw(ellipse); } } } class InsertPiece extends JLabel implements MouseListener{ int column=0; boolean filled = false; Color textColor = Color.WHITE; public InsertPiece(int column){ super(); this.column = column; Dimension size = new Dimension(50,50); setMaximumSize(size); setMinimumSize(size); setPreferredSize(size); setHorizontalAlignment(CENTER); setForeground(textColor); addMouseListener(this); } public void paintComponent(Graphics g){ if (filled){ Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(currentPlayer.getPlayColor()); Ellipse2D.Float ellipse = new Ellipse2D.Float(10,10,getWidth()-20,getHeight()-20); g2.fill(ellipse); } // usually want to call super.paintComponent before any custom code // but here we want to render the circle first and let the label // paint it's text on top of the image super.paintComponent(g); } public void mouseClicked(MouseEvent e) { setText(null); filled = false; placePiece(column); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { setText("1"); // set whatever count you need here filled = true; repaint(); } public void mouseExited(MouseEvent e) { setText(null); filled = false; repaint(); } } class Player { private String name; private Color playColor; public Player(String name, Color playColor){ this.name = name; this.playColor = playColor; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Color getPlayColor() { return playColor; } public void setPlayColor(Color playColor) { this.playColor = playColor; } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.GridBagLayout()); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Board().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration }
![]() |
Other Threads in the Java Forum
- Previous Thread: exceptions
- Next Thread: Splitting double into its integer portion and decimal portion
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying oracle page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string swing system thread threads time tree variablebinding windows xor






