Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Broseph Radson on September 16, 2011, 06:27:05 pm

Title: What am i doing wrong here?
Post by: Broseph Radson on September 16, 2011, 06:27:05 pm
Ive tried compiling a program on 2 different computers using this code:
Code: (java) [Select]
button1.setBounds(5, 5, 100, 60);
button2.setBounds(5, 60, 100, 60);

on one computer it says "symbol not found" on setBounds (ofc i have awt.* and swing.* included)

on the other it says "illegal start of type" on the the first arg, and "<identifier> expected" after setBounds

???

Also, when i try to define an array, it keeps telling me that i need 2 closing brackets and that i need a semicolon inside the brackets...
???
Title: Re: What am i doing wrong here?
Post by: Builderboy on September 16, 2011, 06:33:31 pm
It would help to see more of the code, like where you declared the buttons, and where you are trying to create the arrays.  What type of object are the buttons?
Title: Re: What am i doing wrong here?
Post by: Broseph Radson on September 16, 2011, 07:12:09 pm
Code: (moar java) [Select]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class herp extends JFrame {

JButton button1 = new JButton("Derp");
JButton button2 = new JButton("Lolwut");
button1.setBounds(5, 5, 100, 60);
button2.setBounds(5, 60, 100, 60);

public herp() {
super("Window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(button1);
add(button2);
pack();
}
}

class window {
public static void main(String[] args) {
herp hai = new herp();
hai.setVisible(true);
}
}

And the arrays are in here:
Code: (arrayzz) [Select]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class map extends JPanel {
int mapMatrix[][] = new int[15][10];


String tiles[] = new String[2];


map() {
this.setBounds(0, 25, 960, 640);
for(int x = 0; x < 15; x++) {
for(int y = 0; y < 10; y++) {
ImageIcon tile = new ImageIcon(tiles[mapMatrix[x][y]]);
tile.setBounds(((x + 1) * 64), ((y + 1) * 64), 64, 64);
this.add(tile);
tile.setVisible(true);
this.setVisible(true);
}
}
}
}

There may be other errors but im concerned with why javac is telling me to put extra closing brackets and stuff.

EDIT: i just compiled that second one on this computer and its giving me different errors. using the same version of jdk...

Maybe im just dreaming...nothing is making any kind of sense.
Title: Re: What am i doing wrong here?
Post by: Builderboy on September 16, 2011, 07:16:29 pm
The syntax for an array is int[] array = new int[5], not int array[] = new int[5]. 

The second issue is that you cannot do commands outside of a method, so you will need to move the two button.setBounds into the constructor or wherever you want them
Title: Re: What am i doing wrong here?
Post by: Broseph Radson on September 16, 2011, 07:21:39 pm
Ah, ive seen arrays defined both ways before. I guess i was reading in the wrong places  :D

I think my computer at school is just messed up as this computer doesnt give me any of those errors...
I had like 57 errors on a tiny program, on this computer it only gave me 4...

Thanks, though!

Title: Re: What am i doing wrong here?
Post by: Builderboy on September 16, 2011, 07:22:14 pm
No problem!  You might have been seeing some C++ code, as I think thats how they define arrays o.O
Title: Re: What am i doing wrong here?
Post by: Broseph Radson on September 16, 2011, 07:24:44 pm
Another issue: The second one says "cannot find symbol" on setBounds, add, and setVisible. All are within a method.
Title: Re: What am i doing wrong here?
Post by: JL235 on September 16, 2011, 08:04:48 pm
Another issue: The second one says "cannot find symbol" on setBounds, add, and setVisible. All are within a method.
Your calling those methods on the 'tile' object, which is an ImageIcon, and doesn't have those methods. In the compiler error...
Code: [Select]
map.java:19: cannot find symbol
symbol  : method setVisible(boolean)
location: class javax.swing.ImageIcon
                                tile.setVisible(true);
... where it says 'location', that is what tells you that it is failing to find 'setVisible' in the 'javax.swing.ImageIcon' class. You also cannot add an ImageIcon directly to a JPanel.

A third thing, it's convention to start class names with a capital letter. Notice this is the case with 'JPanel' and 'ImageIcon', so I heavily advise you call your class 'Map' rather then 'map'.
Title: Re: What am i doing wrong here?
Post by: Broseph Radson on September 19, 2011, 03:38:50 pm
Got it working!

I was using icons when i shouldve been using awt.Graphics and awt.Graphics2D

 :w00t: