I'm trying to write two for loops that will iterate through two arrays and assign the variables from the array to their corresponding textboxs from an array of textboxes. I just need to see if my code is correct. I would test it myself but I do not have a compiler at my disposal at the moment. Here is the code..
'this array literal uses the first variable from four different arrays as its variables
'I didn't tt java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
public class Tag extends JPanel
implements KeyListener{
static JFrame f = new JFrame();
JPanel content = new JPanel();
char shape = 'r';
JLabel ScoreLabel;
JLabel label;
JLabel Winner;
JPanel Weinner;
JPanel panel1;
JLabel ScoreLabel1;
JPanel panel11;
int RandomBox = 0;
int BoxX = 145;
int BoxY = 280;
int SnakeX1 = 300;
int SnakeY1 = 200;
int SnakeX = 340;
int SnakeY = 300;
int direction = 0;
int direction1 = 0;
int score = 0;
int score1 = 0;
Random randomGenerator = new Random();
public Tag() {
setFocusable(true);
addKeyListener(this);;
}
public static void main(String[] args) throws IOException {
String path = "\\H:\\ddown.jpg";
File pic = new File(path);
BufferedImage image = ImageIO.read(pic);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame Title = new JFrame("Snake");
Tag canvas = new Tag();
canvas.makeGUI();
Title.getContentPane().add(canvas);
Title.setSize(700,700);
Title.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
});
Title.setVisible(true);
}
public void makeGUI() {
panel1 = new JPanel();
}
public int BoxXGenerator(int BoxX,Random randomGenerator,int RandomBox) {
RandomBox = randomGenerator.nextInt(5)+1;
if(RandomBox==1) {
BoxX = 340;
}
else if(RandomBox==2) {
BoxX = 135;
}
else if(RandomBox==3) {
BoxX = 505;
}
else if(RandomBox==4) {
BoxX = 520;
}
else if(RandomBox==5) {
BoxX = 375;
}
return BoxX;
}
public int BoxYGenerator(int BoxY,Random randomGenerator,int RandomBox) {
RandomBox = randomGenerator.nextInt(5)+1;
if(RandomBox==1) {
BoxY = 95;
}
else if(RandomBox==2) {
BoxY = 400;
}
else if(RandomBox==3) {
BoxY = 425;
}
else if(RandomBox==4) {
BoxY = 170;
}
else if(RandomBox==5) {
BoxY = 560;
}
return BoxY;
}
//buttons pressed
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
direction=1;
}
if(e.getKeyCode()==KeyEvent.VK_ESCAPE) {
System.exit(0);
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT) {
direction=2;
}
else if(e.getKeyCode()==KeyEvent.VK_UP) {
direction=3;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN) {
direction=4;
}
if(e.getKeyCode()==KeyEvent.VK_D) {
direction1=1;
}
else if(e.getKeyCode()==KeyEvent.VK_A) {
direction1=2;
}
else if(e.getKeyCode()==KeyEvent.VK_W) {
direction1=3;
}
else if(e.getKeyCode()==KeyEvent.VK_S) {
direction1=4;
}
shape = e.getKeyChar();
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void delay() {
try{
Thread.sleep(15);
}
catch(Exception e){
}
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
Dimension d = getSize();
g.fillRect(0,0, d.width, d.height);
// g.translate(x,y);
//each line is 520
g.setColor(Color.YELLOW);
//Top Border
g.drawLine(80,80,600,80);
//Right Border
g.drawLine(600,80,600,600);
//Bottom Border
g.drawLine(600,600,80,600);
//Left Border
g.drawLine(80,80,80,600);
// y-max is 600
// y-min is 80
// x-max is 600
// x-min is 80
//Reset position if X/Y goes past border
if(SnakeX<80){
SnakeX=600;
}
if(SnakeX>600){
SnakeX=80;
}
if(SnakeY<80) {
SnakeY=600;
}
if(SnakeY>600) {
SnakeY=80;
}
if(SnakeX1<80){
SnakeX1=600;
}
if(SnakeX1>600){
SnakeX1=80;
}
if(SnakeY1<80) {
SnakeY1=600;
}
if(SnakeY1>600) {
SnakeY1=80;
}
//Up, down, left, right
if(direction==1) {
SnakeX=SnakeX+5;
repaint();
delay(); }
if(direction==2) {
SnakeX=SnakeX-5;
repaint();
delay();
}
if(direction==3) {
SnakeY=SnakeY-5;
repaint();
delay();
}
if(direction==4) {
String path = "\\H:\\ddown.jpg";
File pic = new File(path);
BufferedImage image = null;
try {
image = ImageIO.read(pic);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel label = new JLabel(new ImageIcon(image));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(SnakeX,SnakeY);
f.setVisible(true);
pic(SnakeX,SnakeY,15,15);
SnakeY=SnakeY+5;
repaint();
delay();
}
if(direction1==1) {
SnakeX1=SnakeX1+5;
repaint();
delay();
}
if(direction1==2) {
SnakeX1=SnakeX1-5;
repaint();
delay();
}
if(direction1==3) {
SnakeY1=SnakeY1-5;
repaint();
delay();
}
if(direction1==4) {
SnakeY1=SnakeY1+5;
repaint();
delay();
}
g.setColor(Color.YELLOW);
g.fillRect(BoxX,BoxY,15,15);
//Snakes
g.setColor(Color.RED);
g.fillRect(SnakeX,SnakeY,15,15);
g.setColor(Color.CYAN);
g.fillRect(SnakeX1,SnakeY1,15,15);
if(Math.abs(SnakeX1 - SnakeX)< 15 && Math.abs(SnakeY1 - SnakeY)< 7) {
g.setColor(Color.WHITE);
g.fillRect(SnakeX1,SnakeY1,15,15);
}
}
private void pic(int snakeX2, int snakeY2, int i, int j) {
// TODO Auto-generated method stub
}
}