I did till this however it is not showing any output.
import acm.program.*;
import acm.graphics.*;
import java.awt.*;
public class Test5 extends GraphicsProgram {
public void run() {
GRect rect = new GRect(0,0,50,50);
for(int i=1;i<=8;i++){
for(int j=1;j<=8;j++)
{
if(((i + j) % 2 == 0))
{
rect.setColor(Color.WHITE);}
else
{
rect.setColor(Color.BLACK); }
add(rect);
}
}
}
}
A simple approach:
Check if both i and j are even or if both i and j are odd.
Better yet, you can just compare the evenness/oddness of i and j for equality.
// Fill black.
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
// Color red tiles.
g.setColor(Color.RED);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i % 2 == j % 2) {
g.fillRect(i * tileSize, j * tileSize, tileSize, tileSize);
}
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ChessBoard extends JPanel {
private int size;
public ChessBoard(int size) {
this.setPreferredSize(new Dimension(size, size));
this.size = size;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int tileSize = size / 8;
// Fill black.
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
// Color red tiles.
g.setColor(Color.RED);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i % 2 == j % 2) {
g.fillRect(i * tileSize, j * tileSize, tileSize, tileSize);
}
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
Container c = new ChessBoard(400);
f.setContentPane(c);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.pack();
f.setVisible(true);
}
}
Related
I am trying to make a game of life simulation using Java Graphics but when a run my code the left one third of the screen is grey.I want the whole screen to be white with black squares representing living squares. I am confused about all the java containers/panels and frames.
Here's my code:
public class ConwayGame {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException{
int [][] array = new int [40][40];
/*
* Set the pattern for Conway's Game of Life by manipulating the array below.
*/
/*Acorn
*/
array[19][15]=1;
array[19][16]=1;
array[19][19]=1;
array[19][20]=1;
array[19][21]=1;
array[18][18]=1;
array[17][16]=1;
panel = new JPanel();
Dimension dim = new Dimension(400, 400);
panel.setPreferredSize(dim);
frame = new JFrame();
frame.setSize(400,400 );
Container contentPane = frame.getContentPane();
contentPane.add(panel);
frame.setVisible(true);
/*
* Runs the Game of Life simulation "a" number of times.
*/
Graphics g = panel.getGraphics();
drawArray(array, g);
//paint(g);
//Thread.sleep(125);
//g.dispose();
}
/*
* Creates the graphic for Conway's game of life.
*/
public static void drawArray(int[][] array, Graphics g) {
int BOX_DIM = 10;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
g.drawRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
if (array[i][j] == 0) {
g.setColor(Color.WHITE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}
}
}
}
Here's a picture of what is generated:
DON'T use Graphics g = panel.getGraphics(); EVER. This is not how custom painting works in Swing. Take a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works
For example...
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ConwayGame {
static JPanel panel;
static JFrame frame;
public static void main(String[] args) throws InterruptedException {
int[][] array = new int[40][40];
array[19][15] = 1;
array[19][16] = 1;
array[19][19] = 1;
array[19][20] = 1;
array[19][21] = 1;
array[18][18] = 1;
array[17][16] = 1;
panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawArray(array, g);
}
};
frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
/*
* Creates the graphic for Conway's game of life.
*/
public static void drawArray(int[][] array, Graphics g) {
int BOX_DIM = 10;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
g.drawRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
if (array[i][j] == 0) {
g.setColor(Color.WHITE);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
if (array[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillRect(i * BOX_DIM, j * BOX_DIM, 10, 10);
}
}
}
}
}
Now, personally, I would create a custom component, which extended from JPanel and would place all the logic you needed into that class
can somebody please help me create a chess like board. I need to change the color the grid to black and white. I tried to use a an if statement if (r % 2 = 0) then rectfilcolor, but it colors the hall row.
package grid;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class grid extends JPanel {
public static int High=640;
public static int width=617;
public static int row=3,column=3;
public static JFrame Frame;
public static void main(String[] args) {
grid gride= new grid();
Frame= new JFrame();
Frame.setSize(width, High);
Frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
Frame.setVisible(true);
Frame.add(gride);
gride.setBackground(Color.cyan);
}
public void paintComponent(Graphics g) {
for (int r=0; r<4; r++) {
g.drawLine(r*(600/3), 0, r*(600/3), 600);
for (int c=0; c<4; c++) {
g.drawLine(0,(c*(600/3)), 600, (c*(600/3)));
}
}
}
}
-------------------------------------Edited----------------------------------
public void paintComponent(Graphics g){
for (int r=0;r<4;r++){
g.drawLine(r*(600/3), 0, r*(600/3), 600);
if (r%2!=0){
g.setColor(Color.white);
g.fillRect(r*(600/3), 0, r*(600/3), 600);
}
for (int c=0;c<4;c++){
g.drawLine(0,(c*(600/3)), 600, (c*(600/3)));
if(c%2!=0){
g.setColor(Color.black);
g.fillRect(0,(c*(600/3)), 600, (c*(600/3)));
}
}
}
}
}
Always remember to call super.paintComponent(g) to initialize the JPanel canvas correctly.
You can use g.fillRect(x, y, width, height) method to draw each chess cell. Use g.setColor(color) to change the color of the painting.
Therefore:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color[] colors = {Color.BLACK, Color.WHITE};
int lengthUnit = (600 / 3);
for (int row = 0; row < 3; ++ row) {
for (int col = 0; col < 3; ++col) {
g.setColor(colors[(row + col) % 2]); // alternate between black and white
g.fillRect(row * lengthUnit, col * lengthUnit, lengthUnit, lengthUnit);
}
}
}
Edit: you are almost there, just need to remove some redundant statements in the nested for loop...
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
if ((c + r) % 2 != 0) {
g.setColor(Color.black);
} else {
g.setColor(Color.white);
}
g.fillRect(r * (600 / 3), (c * (600 / 3)), 200, 200);
}
}
I'm trying to make a Connect Four Game in Java by using user input and drawing panel. Two people switch off turns typing the column they want their piece to go in. I'm having trouble making it so discs already placed stay on the board the next turn (because the a new drawing panel pops up with the updated board each time)(we tried using GUI but failed). I am also having trouble switching off turns and making the color change each time. Thank you so much, here is what I have so far.
import java.util.*;
import java.awt.*;
public class ConnectFour{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
char [][] board = new char [7][6];
for (int i = 0; i< 7; i++){
for (int j= 0; j< 6; j++){
board[i][j] = ' ';
}
}
boolean isBlack = true ; //black discs turn
boolean isRed = false ; //red discs turn
boolean playersTurn = true ;
while (playersTurn){
if (isBlack == true ){
System.out.println("Black's Turn");
}
else {
System.out.println("Red's Turn");
}
System.out.print("Choose a column to place your disk (1-7): ");
int column = console.nextInt()-1;
if (column < 0 || column > 7) { //while loop?
System.out.print( "Column needs to be within 1-7"); //try catch?
}
drawBoard(column, board, isBlack, isRed);
// connectedFour(board);
// playersTurn = !playersTurn;
}
// isBlack = !isBlack; INVERT WHOSE TURN IT IS!!! unreachable statement??
}
public static void drawBoard(int column, char [][] board, boolean isBlack, boolean isRed) {
DrawingPanel panel = new DrawingPanel(550,550);
int rowAvailable;
Graphics g = panel.getGraphics();
g.drawLine(0,0,0,500);
g.drawLine(0,0,500,0);
g.drawLine(500,0,500,427);
g.drawLine(0,427,500,427);
for (int i = 0; i< 6; i++){
for (int j= 0; j<= 6; j++){
g.setColor(Color.YELLOW);
g.fillRect(j*71,i*71,71,71);
g.setColor(Color.WHITE);
g.fillOval(j*71,i*71,71,71);
}
}
int x = 0;
int row = 5;
while (board[column][row-x] != ' '){
x++;
}
row = 5-x;
if (isBlack == true) {
g.setColor(Color.BLACK);
board[column][row-x] = 'b';
}
else {
g.setColor(Color.RED);
board[column][row-x] = 'r';
}
// I KNOW THIS FOR LOOP DOES NOT WORK SUCCESSFULLY
for (int i = 0; i< 6; i++){
for (int j= 0; j<= 6; j++){
if(board[i][j] != 'b'){
g.fillOval((i * 71),j*71, 71,71);
}
}
}
// g.fillOval((column * 71),row*71, 71,71); //number 142 is just a test
//board[i][j] = WHOSE TURN IT IS (to update array)
// if(isBlack){
// board[column][row] = 'b';
// }
// else{
// board[column][row] = 'r';
// }
}
//get whose turn it is as parameter?? a color string? boolean?
public static boolean connectedFour( char[][] board){
int verticalCount = 0;
for (int i = 0; i< 6; i++){ //goes down each column //EXCEPTION HERE BECAUSE 0
for( int j=0; j<=6; j++){
if (board[i][j]== board[i-1][j]){
verticalCount ++;
}
}
}
int horizontalCount = 0;
for (int i =0; i<=6; i++){
for (int j =0; j<6; j++){
if (board[i][j-1] == board[i][j]){
horizontalCount++;
}
}
}
int diagonalCount = 0;
//get diagonal here
if (verticalCount >= 4 || horizontalCount >= 4|| diagonalCount >=4){
return true ; //return who the winner is. String?
//
}
else {
return false ;
}
}
}
Don't mix console based input with GUI output, these are two different user paradigms which require different work flows and approaches to manage
Don't use getGraphics, this is not how painting works. Swing has a defined paint process which you should use to ensure you are been notified when an update needs to be performed. See Painting in AWT and Swing and Performing Custom Painting for more details.
The following is a very basic example of how you might work with the API, instead of against it.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ConnectFour {
public static void main(String[] args) {
new ConnectFour();
}
public ConnectFour() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public enum Player {
RED, BLUE, NONE;
}
public class GamePane extends JPanel {
private BoardPane boardPane;
private JLabel label;
private Player player = null;
public GamePane() {
setLayout(new BorderLayout());
boardPane = new BoardPane();
add(boardPane);
label = new JLabel("...");
label.setHorizontalAlignment(JLabel.CENTER);
label.setForeground(Color.WHITE);
label.setOpaque(true);
label.setBorder(new EmptyBorder(10, 10, 10, 10));
add(label, BorderLayout.NORTH);
updatePlayer();
boardPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
updatePlayer();
}
});
}
protected void updatePlayer() {
String text = "...";
Color color = null;
if (player == null || player.equals(Player.BLUE)) {
player = Player.RED;
text = "Red";
color = Color.RED;
} else if (player.equals(Player.RED)) {
player = Player.BLUE;
text = "Blue";
color = Color.BLUE;
}
label.setText(text);
label.setBackground(color);
boardPane.setPlayer(player);
}
}
public class BoardPane extends JPanel {
private Player[][] board;
private Player player;
private int hoverColumn = -1;
public BoardPane() {
board = new Player[8][8];
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = Player.NONE;
}
}
MouseAdapter mouseHandler = new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
hoverColumn = getColumnAt(e.getPoint());
repaint();
}
#Override
public void mouseExited(MouseEvent e) {
hoverColumn = -1;
repaint();
}
#Override
public void mouseClicked(MouseEvent e) {
if (hoverColumn > -1) {
addPieceTo(hoverColumn);
repaint();
}
}
};
addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
}
public void addChangeListener(ChangeListener listener) {
listenerList.add(ChangeListener.class, listener);
}
public void removeChangeListener(ChangeListener listener) {
listenerList.add(ChangeListener.class, listener);
}
public void setPlayer(Player player) {
this.player = player;
}
protected void addPieceTo(int col) {
boolean added = false;
if (col >= 0 && col < board[0].length) {
for (int row = 0; row < board.length; row++) {
if (board[row][col] != Player.NONE) {
if (row >= 0) {
board[row - 1][col] = player;
added = true;
}
break;
}
}
}
if (!added) {
if (board[0][col] == Player.NONE) {
board[board.length - 1][col] = player;
added = true;
}
}
if (added) {
fireStateChanged();
}
repaint();
}
protected void fireStateChanged() {
ChangeListener[] listeners = listenerList.getListeners(ChangeListener.class);
if (listeners != null && listeners.length > 0) {
ChangeEvent evt = new ChangeEvent(this);
for (ChangeListener listener : listeners) {
listener.stateChanged(evt);
}
}
}
protected int getColumnAt(Point p) {
int size = Math.min(getWidth() - 1, getHeight() - 1);
int xOffset = (getWidth() - size) / 2;
int yOffset = (getHeight() - size) / 2;
int padding = getBoardPadding();
int diameter = (size - (padding * 2)) / 8;
int xPos = p.x - xOffset;
int column = xPos / diameter;
return Math.min(Math.max(0, column), board[0].length - 1);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected int getBoardPadding() {
return 10;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
int size = Math.min(getWidth() - 1, getHeight() - 1);
int xOffset = (getWidth() - size) / 2;
int yOffset = (getHeight() - size) / 2;
g2d.fill(new RoundRectangle2D.Double(xOffset, yOffset, size, size, 20, 20));
int padding = getBoardPadding();
int diameter = (size - (padding * 2)) / 8;
for (int row = 0; row < board.length; row++) {
int yPos = (yOffset + padding) + (diameter * row);
for (int col = 0; col < board[row].length; col++) {
int xPos = (xOffset + padding) + (diameter * col);
switch (board[row][col]) {
case RED:
g2d.setColor(Color.RED);
break;
case BLUE:
g2d.setColor(Color.BLUE);
break;
default:
g2d.setColor(getBackground());
break;
}
g2d.fill(new Ellipse2D.Double(xPos, yPos, diameter, diameter));
}
}
if (hoverColumn > -1) {
int yPos = (yOffset + padding) + (diameter * 0);
int xPos = (xOffset + padding) + (diameter * hoverColumn);
if (player != null) {
switch (player) {
case RED:
g2d.setColor(Color.RED);
break;
case BLUE:
g2d.setColor(Color.BLUE);
break;
default:
g2d.setColor(getBackground());
break;
}
g2d.fill(new Ellipse2D.Double(xPos, yPos, diameter, diameter));
}
}
g2d.dispose();
}
}
}
I would like to know if there is any algorithm that does something like this:
Given a specific surface it divides it into smaller rectangles of the same size.
Something like this example figure:
The grey area is the surface, and the red squares is the partition itself.
I am thinking if there is a optimized way to do this.
A very bad approach would be a for loop in all the pixels and check if there is a rectangle for that specific spot, if not, would create a rectangle, and so on..
Maybe someone knows a algorithm already done? or a better solution?
Thanks alot in advance ;)
Here's one way to go about it.
Create a mask of the image. (I just used Photoshop)
Steal AndrewThompson's code for Creating an Area from an Image and use it to create an Area of the image.
Area imageArea = getOutline(Color.BLACK, imageMask);
Create a grid Rectangle2D objects for the entirety of the image.
Rectangle2D[][] grid = new Rectangle2D[rows][cols];
for (int i = 0; i < grid.length; i++) {
int y = i * CELL_SIZE;
for (int j = 0; j < grid[i].length; j++) {
int x = j * CELL_SIZE;
grid[i][j] = new Rectangle2D.Double(x, y, cellSize, cellSize);
}
}
Once you have the grid, you can just traverse the Rectangle2D objects and check if the Area.contains each individual Rectangle2D in the grid, and you can just add it to a List<Rectangle2D>. Only rectangles contained in the area will be added, giving you your final grid of rectangles to draw. In the example below, I just painted to rectangles as a visual.
for (Rectangle2D[] rects : imageGrid) {
for (Rectangle2D rect : rects) {
if (imageArea.contains(rect)) {
g2.drawRect((int) rect.getX(), (int) rect.getY(),
(int) rect.getWidth(), (int) rect.getHeight());
}
}
}
Full example
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SquaresInArea extends JPanel {
private static final int CELL_SIZE = 30;
BufferedImage image;
BufferedImage imageMask;
Area imageArea;
Rectangle2D[][] imageGrid;
public SquaresInArea() {
try {
image = ImageIO.read(getClass().getResource("/resources/floorplan.png"));
imageMask = ImageIO.read(getClass().getResource("/resources/floorplan-black.png"));
} catch (IOException ex) {
Logger.getLogger(SquaresInArea.class.getName()).log(Level.SEVERE, null, ex);
}
imageArea = getOutline(Color.BLACK, imageMask);
imageGrid = createGrid();
}
private Rectangle2D[][] createGrid() {
int width = image.getWidth();
int height = image.getHeight();
int rows = height / CELL_SIZE;
int cols = width / CELL_SIZE;
Rectangle2D[][] grid = new Rectangle2D[rows][cols];
for (int i = 0; i < grid.length; i++) {
int y = i * CELL_SIZE;
for (int j = 0; j < grid[i].length; j++) {
int x = j * CELL_SIZE;
grid[i][j] = new Rectangle2D.Double(x, y, CELL_SIZE, CELL_SIZE);
}
}
return grid;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, 0, 0, this);
g2.setColor(Color.YELLOW);
g2.setStroke(new BasicStroke(3f));
for (Rectangle2D[] rects : imageGrid) {
for (Rectangle2D rect : rects) {
if (imageArea.contains(rect)) {
g2.drawRect((int) rect.getX(), (int) rect.getY(),
(int) rect.getWidth(), (int) rect.getHeight());
}
}
}
}
#Override
public Dimension getPreferredSize() {
return image == null ? new Dimension(300, 300)
: new Dimension(image.getWidth(), image.getHeight());
}
private Area getOutline(Color target, BufferedImage bi) {
// construct the GeneralPath
GeneralPath gp = new GeneralPath();
boolean cont = false;
int targetRGB = target.getRGB();
for (int xx = 0; xx < bi.getWidth(); xx++) {
for (int yy = 0; yy < bi.getHeight(); yy++) {
if (bi.getRGB(xx, yy) == targetRGB) {
if (cont) {
gp.lineTo(xx, yy);
gp.lineTo(xx, yy + 1);
gp.lineTo(xx + 1, yy + 1);
gp.lineTo(xx + 1, yy);
gp.lineTo(xx, yy);
} else {
gp.moveTo(xx, yy);
}
cont = true;
} else {
cont = false;
}
}
cont = false;
}
gp.closePath();
// construct the Area from the GP & return it
return new Area(gp);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new SquaresInArea());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
here's another view for clarity
private final BasicStroke thin = new BasicStroke(1f);
private final BasicStroke thick = new BasicStroke(4f);
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, 0, 0, this);
for (Rectangle2D[] rects : imageGrid) {
for (Rectangle2D rect : rects) {
if (imageArea.contains(rect)) {
g2.setStroke(thick);
g2.setColor(Color.GREEN);
g2.draw(rect);
} else {
g2.setStroke(thin);
g2.setColor(Color.RED);
g2.draw(rect);
}
}
}
}
Do you just want to fill it with squares - or do you want to fill it with the optimal number of squares?
An algorithm for the second is harder.
For the first just step through the image a square-size at a time. If the pixel at that point is filled then scan the full square, if it's all filled in then draw the square. If not then step to the next point.
i.e. if squares are 10*10 pixels:
for (int x=0;x<width;x+=SQUARE_SIZE) {
for (int y=0;y<height;y+=SQUARE_SIZE) {
// Now check if you can put a valid square here, if so draw it
}
}
Sometimes the mousePressed event gets executed , but at other times it won't. It also seems to depend on the time you press it. How do I get it to ALWAYS work?
I'm not sure which part of the code is faulty, so here's the whole class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
Timer timer = new Timer(500, this);
private boolean[][] board;
private boolean isActive = false;
private int height;
private int width;
private int multiplier = 20;
private JButton btnRun;
private JButton btnRand;
private JButton btnClear;
public Board() {
this(new boolean[20][20]);
}
public Board(final boolean[][] board) {
this.board = board;
height = board.length;
width = board[0].length;
setBackground(Color.black);
btnRun = new JButton("Run");
add(btnRun);
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isActive = !isActive;
btnRun.setText(isActive ? "Pause" : "Run");
}
});
btnRand = new JButton("Random");
add(btnRand);
btnRand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setBoard(randomBoard());
}
});
btnClear = new JButton("Clear");
add(btnClear);
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setBoard(clearBoard());
}
});
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
getBoard()[e.getY() / multiplier][e.getX() / multiplier] = !getBoard()[e.getY() / multiplier][e.getX() / multiplier];
}
});
timer.start();
}
public int getMultiplier() {
return multiplier;
}
public boolean[][] getBoard() {
return board;
}
public void setBoard(boolean[][] boardToSet) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
board[i][j] = boardToSet[i][j];
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
g.setColor(board[i][j] ? Color.green : Color.gray);
g.fillRect(j * multiplier, i * multiplier, multiplier - 1, multiplier - 1);
}
}
if (isActive) {
timer.start();
}
else {
timer.stop();
repaint();
}
}
public void actionPerformed(ActionEvent e) {
board = nextGeneration();
repaint();
}
public boolean[][] randomBoard() {
Random rand = new Random();
boolean[][] randBoard = new boolean[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
randBoard[i][j] = rand.nextBoolean();
}
}
return randBoard;
}
public boolean[][] clearBoard() {
boolean[][] emptyBoard = new boolean[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
emptyBoard[i][j] = false;
}
}
return emptyBoard;
}
public int countSurrounding(int a, int b) {
int count = 0;
int[][] surrounding = {{a - 1, b - 1},
{a - 1, b },
{a - 1, b + 1},
{a , b - 1},
{a , b + 1},
{a + 1, b - 1},
{a + 1, b },
{a + 1, b + 1}};
for (int[] i: surrounding) {
try {
if (board[i[0]][i[1]]) {
count++;
}
}
catch (ArrayIndexOutOfBoundsException e) {}
}
return count;
}
public boolean[][] nextGeneration() {
boolean[][] nextBoard = new boolean[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
nextBoard[i][j] = board[i][j];
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (board[i][j] && !(countSurrounding(i, j) == 2 || countSurrounding(i, j) == 3)) {
nextBoard[i][j] = false;
}
else if (!board[i][j] && countSurrounding(i, j) == 3) {
nextBoard[i][j] = true;
}
}
}
return nextBoard;
}
}
Add a System.out statement in mousePressed() and you will see that it is always called:
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
getBoard()[e.getY() / multiplier][e.getX() / multiplier] = !getBoard()[e.getY() / multiplier][e.getX() / multiplier];
repaint();
}
Like others suggested in your other very similar question, the problem stems from your use of timers in your paint method.
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
g.setColor(board[i][j] ? Color.green : Color.gray);
g.fillRect(j * multiplier, i * multiplier, multiplier - 1, multiplier - 1);
}
}
//if (isActive) { // take this stuff out...
// timer.start();
//}
//else {
// timer.stop();
// repaint();
//}
}
The paintComponent() method should be used to draw your components only. Do not use it to start/stop timers, make additional calls to repaint(), or otherwise call any other kind of program logic. Rethink your design, especially in paintComponent().
In addition to what whiskeyspider said, if you click on a cell while it's running, you won't see the cell light up until the next generation, and only if that position is considered alive, after processing, in the next generation.