Last active
January 20, 2022 17:52
-
-
Save memish/b564172e920fd3b8650104e96401fb58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.swing.JFrame; | |
| import javax.swing.JPanel; | |
| import javax.swing.WindowConstants; | |
| import java.awt.Dimension; | |
| import java.awt.Color; | |
| import java.awt.Graphics; | |
| import java.awt.Graphics2D; | |
| import java.awt.Point; | |
| import java.awt.event.MouseListener; | |
| import java.awt.event.MouseEvent; | |
| public class DrawGrid { | |
| private JFrame frame; | |
| public DrawGrid() { | |
| frame = new JFrame("DrawGrid"); | |
| frame.setSize(600, 400); | |
| frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
| frame.setPreferredSize(frame.getSize()); | |
| frame.add(new MultiDraw(frame.getSize())); | |
| frame.pack(); | |
| frame.setVisible(true); | |
| } | |
| public static void main(String... argv) { | |
| new DrawGrid(); | |
| } | |
| public static class MultiDraw extends JPanel implements MouseListener { | |
| int startX = 10; | |
| int startY = 10; | |
| int cellWidth = 40; | |
| int turn = 2; | |
| int rows = 6; | |
| int cols = 7; | |
| Color[][] grid = new Color[rows][cols]; | |
| public MultiDraw(Dimension dimension) { | |
| setSize(dimension); | |
| setPreferredSize(dimension); | |
| addMouseListener(this); | |
| //1. initialize array here | |
| int x = 0; | |
| for (int row = 0; row < grid.length; row++) { | |
| for (int col = 0; col < grid[0].length; col++) { | |
| } | |
| } | |
| } | |
| @Override | |
| public void paintComponent(Graphics g) { | |
| Graphics2D g2 = (Graphics2D)g; | |
| Dimension d = getSize(); | |
| g2.setColor(new Color(0, 0, 0)); | |
| g2.fillRect(0,0,d.width,d.height); | |
| startX = 0; | |
| startY = 0; | |
| //2) draw grid here | |
| for (int row = 0; row < grid.length; row++) { | |
| for (int col = 0; col < grid[0].length; col++) { | |
| } | |
| } | |
| g2.setColor(new Color(255, 255, 255)); | |
| g2.drawString("Red's Turn",400,20); | |
| } | |
| public void mousePressed(MouseEvent e) { | |
| int x = e.getX(); | |
| int y = e.getY(); | |
| // System.out.println(x + " " + y); | |
| turn++; | |
| repaint(); | |
| } | |
| public void mouseReleased(MouseEvent e) { | |
| } | |
| public void mouseEntered(MouseEvent e) { | |
| } | |
| public void mouseExited(MouseEvent e) { | |
| } | |
| public void mouseClicked(MouseEvent e) { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment