I have a frame that I painted half black and half white with Java Swing's grid methods. And on this frame, there is a white ball that starts moving on the black part and a black ball that starts moving on the white part. I want these balls to bounce back if they touch grids that are the same color as them and paint the grid they touch in the opposite color. I have completed everything except the last feature, but I can't figure out how to implement the last feature in the application.
Interface.java
import javax.swing.*;
import java.awt.*;
public class Interface extends JFrame{
private int frameHeight = 637;
private int frameWidth = 1214;
public Interface() {
setSize(frameWidth, frameHeight);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setTitle("Bouncing Ball");
add(new Core());
setVisible(true);
}
public static void main(String[] args){
new Interface();
}
}
Core.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Core extends JPanel{
int xw = 10, yw = 10, xb = 1000, yb = 400;
int bounds = 20, gridSize = 20;
int vxw = 5, vyw = 5, vxb = 5, vyb = 5;
int rows, columns;
public Core() {
rows = getHeight() / gridSize;
columns = getWidth() / gridSize;
Timer timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(getHeight());
System.out.println(getWidth());
xw += vxw;
yw += vyw;
xb -= vxb;
yb -= vyb;
if (xw > getWidth() - bounds || xw < 0) {vxw = -vxw;}
if (yw > getHeight() - bounds || yw < 0) {vyw = -vyw;}
if (xb > getWidth() - bounds || xb < 0) {vxb = -vxb;}
if (yb > getHeight() - bounds || yb < 0) {vyb = -vyb;}
repaint();
}
});
timer.start();
}
public void paintComponent(Graphics g) {
Graphics2D g2dw = (Graphics2D) g;
Graphics2D g2db = (Graphics2D) g;
Graphics2D grid = (Graphics2D) g;
super.paintComponent(g);
for (int k = 0; k < getWidth(); k += gridSize) {
for (int z = 0; z < getHeight(); z += gridSize) {
grid.drawRect(k, z, gridSize, gridSize);
if (k < getWidth() / 2) {
grid.setColor(Color.BLACK);
grid.fillRect(k, z, gridSize, gridSize);
} else {
grid.setColor(Color.WHITE);
grid.fillRect(k, z, gridSize, gridSize);
}
}
}
g2dw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2dw.setColor(Color.white);
g2dw.fillOval(xw, yw, bounds, bounds);
//g2d.dispose();
g2db.setColor(Color.black);
g2db.fillOval(xb, yb, bounds, bounds);
}
public static void main(String[] args){
}
}
Example Output: https://github.com/vnglst/pong-wars
I'm trying to learn how to write a feature that will make the balls bounce when they touch a grid with the same color as them and paint the grid they touch with the opposite color. Any hints or guidance would be much appreciated too, if you don't want to bother with writing Java code.
I tried creating a 2D array that contains 1 or 0 values (1 for white grid, 0 for black) but encountered with IndexBound errors.
According to camickr's instructions, there is the Java code:
Core.java
Interface.java
There was a logic error in your code, I fixed it: