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);
}
}
Related
I've been trying to generate a grid of randomly colored boxes for a brick-breaker game. However given this code, the colors keep changing. I'd like them to be randomly set and stay that way.
for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map [0].length; j++) {
if(map[i][j] > 0) { //make brick if greater than 0, else don't
int color = (int) (Math.random() * 256);
g.setColor(new Color(color, color, color));
g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
}
}
}
Each time your component need to be resized or moved, repaint() method is being called to update it's state. So if you are generating colors in paintComponent like in following example:
public class MyComponent extends JComponent {
#Override
protected void paintComponent(Graphics g) {
// generate colors and draw grid
}
}
then colors will change on resize event or other event that lead to repaint call, since repaint invokes paintComponent method. If you want to display the same colors, just move generate-colors code out of this method:
public class MyComponent extends JComponent {
private final Color[][] gridColors = randomGridColors(5, 5);
private Color[][] randomGridColors(int rows, int columns) {
Color[][] gridColors = new Color[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
gridColors [i][j] = randomColor();
}
}
}
private Color randomColor() {
int rgbValue = (int) (Math.random() * 256);
return new Color(rgbValue, rgbValue, rgbValue);
}
#Override
protected void paintComponent(Graphics g) {
// draw grid
}
}
You currently create the random color every time you draw your bricks, which can be many times per second.
Create some brick class and generate the color for each brick only once.
Something like this:
public class TryThis {
private static final Logger LOG = Logger.getLogger(TryThis.class.getName());
public static void main(String[] args) {
SwingComponent panel = new SwingComponent();
JFrame frame = new JFrame("try me");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setVisible(true);
}
static class SwingComponent extends JPanel {
int W = 10;
int H = 10;
int recwidth = 20;
int recheight = 10;
Brick[][] bricks = new Brick[H][W];
public SwingComponent() {
super();
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
bricks[y][x] = new Brick(createRandomColor(), new Rectangle(x * recwidth, y * recheight, recwidth,
recheight));
}
}
}
#Override
public void paint(Graphics g) {
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
bricks[y][x].draw(g);
}
}
}
Color createRandomColor() {
return new Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256));
}
}
static class Brick {
Color col;
Rectangle rec;
public Brick(Color col, Rectangle rec) {
this.col = col;
this.rec = rec;
}
public void draw(Graphics g) {
g.setColor(col);
g.fillRect(rec.x, rec.y, rec.width, rec.height);
}
}
}
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
I've been tasked to make a java replica of Candy Crush Saga.
Right now im quite stuck on the GUI part.
I've decided that each candy will be represented by a JLabel holding the candy icon, and a mouselistener to control the functionality.
What happens is, after i finish running the screen shows, the mouse listeners respond but the image doesn't show, meaning i can press the labels get a response but cannot see the icons. I take this as the labels are on the panel but somehow not visible or the icon is not loaded correctly - although when checking the ImageIcon.toString it shows the path to the file.
Any ideas?
Here is the code:
public class Board extends JPanel {
Candy[][] board;
static final int TILE_SIZE = 55;
static final int TILES_MARGIN = 8;
public Board() {
setFocusable(true);
board = new Candy[13][13];
Candy c;
for (int i = 0; i < 13; i++)
for (int j = 0; j < 13; j++) {
if (i != 0 && i != 1 && j != 0 && j != 1 && i != 11 && i != 12 && j != 11 && j != 12) {
Random rand = new Random();
int randomNum = rand.nextInt((6 - 1) + 1) + 1;
c = new Basic(randomNum, this);
} else {
c = new Basic(0, this);
}
setAt(i, j, c);
}
repaint();
}
public void drawCandy(Graphics g2, Candy candy, int x, int y) {
Graphics2D g = ((Graphics2D) g2);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
int value = candy.getClr();
int xOffset = offsetCoors(x);
int yOffset = offsetCoors(y);
ImageIcon myImg = candy.switchIcon();
JLabel toAdd = new JLabel(myImg);
toAdd.setIcon(myImg);
toAdd.setLocation(xOffset,yOffset);
toAdd.setSize(TILE_SIZE,TILE_SIZE);
toAdd.addMouseListener(new ButtonPressed(x,y,candy));
toAdd.setVisible(true);
if (value != 0)
add(toAdd);
}
private static int offsetCoors(int arg) {
return (arg-2) * (TILES_MARGIN + TILE_SIZE) + TILES_MARGIN;
}
public void paint(Graphics g) {
super.paint(g);
removeAll();
requestFocusInWindow();
g.setColor(Color.black);
g.fillRect(0, 0, this.getSize().width, this.getSize().height);
for (int x = 2; x < 11; x++) {
for (int y = 2; y < 11; y++) {
drawCandy(g, board[x][y], x, y);
}
}
validate();
}
and the JFrame :
public Game() {
super("Candy Crush Game");
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
setSize(600, 600);
setResizable(false);
setLocationRelativeTo(null);
this.board = new Board();
this.score = 0;
this.moves = 20;
this.getContentPane().add(board, BorderLayout.CENTER);
setVisible(true);
board.checkSquare(2, 2, 10, 10);
}
I'm quite frustrated, any help will be great!
Instead of overriding paint() method use paintComponent() method for JPanel.
#Overrie
public void paintComponent(Graphics g) {
super.paintComponent(g);
//your custom painting here
}
Read more
Painting in AWT and Swing
paintComponent() vs paint() and JPanel vs Canvas in a paintbrush-type GUI
There might be some issue in reading image icon. My another post might help you.
ImageIcon does not work with me
Instead of creating new JLabel simply change it's icon.
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
}
}
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);
}
}