Inner class problems
hey, i want to ask about a weird problem i had lately. i am currently creating a startegy game and i got lots of problems of accessing memebers and connecting the implemention of all of the class.
now i am having a problem because i took one class and insert inside another class, so the implementation will be possible.
what happend next that i can't create instances of this inner class. and it piss me off because i triple checked the syntix and everything is OK.
when i create the instance without parameters:
Unit newUnit=new Unit() , for some reason it works
but when i changed it to call another constructor with Point variable, it says it doesn't know this constructor.
Help plz :(
[734 byte] By [
Aviva] at [2007-10-2 9:06:50]

are you SURE your syntax is correct? this works for me:
import java.awt.*;
public class Game {
public Game() {
Unit u = new Unit(new Point(100,50));
System.out.println(u);
}
public static void main(String args[]) {
new Game();
}
class Unit {
Point p;
public Unit(Point p) {
this.p = new Point(p);
}
public String toString() {
return p.x+","+p.y;
}
}
}
show us some of your code so we can see what's wrong ..
here you go
the code is abit long and i don't really know what i was doing, but i'll give you the relevent information:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Map extends JPanel implements MouseListener,MouseMotionListener {
public final static int WIDTH=70; public final static int LENGTH=30;
MapUnit[][] mp=new MapUnit[WIDTH][LENGTH];
static MiniMap minimap;
Point xlocation;
Object formerUnit;
Point currentlocation = new Point(-1,-1);
public Map() {
setPreferredSize(new Dimension(500,500));
addMouseListener(this);
addMouseMotionListener(this);
int width=0; int length=0;
for (int y=0; y<WIDTH; y++) {
for (int i=0;i<LENGTH;i++) {
mp[y]=new MapUnit(new Point(width,length));
width=width+50;
}
length=length+50;
}
minimap=new MiniMap(mp);
Unit newUnit=new Unit(new Point(0,0); // the problem is here
add(newUnit);//
newUnit.setLocation(0,0);
System.out.println(newUnit);
}
}
public MapUnit[][] getMap() {}
public MiniMap getMinatureMap() {
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
public void paint(Graphics g) {}
}
And here is the Player Class, which cotains the unit class. If you got any suggestments how to build the game itself withno relation to this topic issue, i'll be glad to hear.
ok, here is the player class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//contains the toolbar, the amount of resources, and the alerted units
public class Player extends JPanel {
int goldStock=300; int lumberStock=300; // resources of the player
Unit[] focusedUnits=new Unit[3];// will contain all of the currently focused player which wait for orders
Unit[] exsitingUnit=new Unit[39]; // will contain all of the player controllable units
int indexFocused=0;// index of free location for unit, full =3
int indexExsiting=0; // the location which is aviable to add new Unit, full =40
public void setAmountGold(int gold) {
}
public void setAmountLumber(int lumber) {
}
public void insertFocused(Unit u) throws UnitFullException {
}
public class Unit extends JPanel implements MouseMotionListener, MouseListener {
int healthPointsMax=0;
int healthPoints=0; boolean attackable=false;
int attackPower=0; boolean moveAble=false;
Color fraction=Color.blue;
boolean focused=false; // is this unit focused?
Point location; //location of upperleft x,y in the map
ImageIcon soliderIncarniation;//will be automaticly set by the type of soldier we will create
String currentOperation="Stop";// various commands that will change the behavior of that soldier and the way we control it. the default is "stop" which means, move to his current location.
String unitName=new String();
int milisecOfCreation=100;//Creation time of this unit, untill appearnce
JLabel detail=new JLabel(unitName);
public Unit (Point location) { //PROBLEM IS HERE :(
setSize(50,50);
System.out.println(" something");
this.location=new Point(location);
this.healthPoints=300;
this.healthPointsMax=300;
soliderIncarniation=new ImageIcon("soldier.JPG");
unitName=new String("footman");
setVisible(true);
addMouseListener(this);
addMouseMotionListener(this);
}
public void drawDetail() {
detail=new JLabel();
detail.setForeground(fraction);
detail=new JLabel(unitName+" "+healthPoints);
this.add(detail);
}
public void removeDetail() {
}
public Color getColorFraction() {
}
public void getDamage(int damageTaken) {
}
public void makingDamage(Unit u) {
}
public String toString() {
return unitName;
}
public void paint(Graphics g) {
soliderIncarniation.paintIcon(this,g,(int)location.getX(),(int)location.getY());
}
//interface methods
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}//end of unit class
}
>
Aviva at 2007-7-16 23:13:53 >

here you go
the code is abit long and i don't really know what i was doing, but i'll give you the relevent information:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Map extends JPanel implements MouseListener,MouseMotionListener {
public final static int WIDTH=70; public final static int LENGTH=30;
MapUnit[][] mp=new MapUnit[WIDTH][LENGTH];
static MiniMap minimap;
Point xlocation;
Object formerUnit;
Point currentlocation = new Point(-1,-1);
public Map() {
setPreferredSize(new Dimension(500,500));
addMouseListener(this);
addMouseMotionListener(this);
int width=0; int length=0;
for (int y=0; y<WIDTH; y++) {
for (int i=0;i<LENGTH;i++) {
mp[y][i]=new MapUnit(new Point(width,length));
width=width+50;
}
length=length+50;
}
minimap=new MiniMap(mp);
[b]Unit newUnit=new Unit(new Point(0,0); // the problem is here
add(newUnit);//
newUnit.setLocation(0,0);
System.out.println(newUnit);
[/b]
}
}
public MapUnit[][] getMap() {}
public MiniMap getMinatureMap() {
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
public void paint(Graphics g) {}
}
And here is the Player Class, which cotains the unit class. If you got any suggestments how to build the game itself withno relation to this topic issue, i'll be glad to hear.
ok, here is the player class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//contains the toolbar, the amount of resources, and the alerted units
public class Player extends JPanel {
int goldStock=300; int lumberStock=300; // resources of the player
Unit[] focusedUnits=new Unit[3];// will contain all of the currently focused player which wait for orders
Unit[] exsitingUnit=new Unit[39]; // will contain all of the player controllable units
int indexFocused=0;// index of free location for unit, full =3
int indexExsiting=0; // the location which is aviable to add new Unit, full =40
public void setAmountGold(int gold) {
}
public void setAmountLumber(int lumber) {
}
public void insertFocused(Unit u) throws UnitFullException {
}
public class Unit extends JPanel implements MouseMotionListener, MouseListener {
int healthPointsMax=0;
int healthPoints=0; boolean attackable=false;
int attackPower=0; boolean moveAble=false;
Color fraction=Color.blue;
boolean focused=false; // is this unit focused?
Point location; //location of upperleft x,y in the map
ImageIcon soliderIncarniation;//will be automaticly set by the type of soldier we will create
String currentOperation="Stop";// various commands that will change the behavior of that soldier and the way we control it. the default is "stop" which means, move to his current location.
String unitName=new String();
int milisecOfCreation=100;//Creation time of this unit, untill appearnce
JLabel detail=new JLabel(unitName);
[b]public Unit (Point location) { //PROBLEM IS HERE :(
setSize(50,50);
System.out.println(" something");
this.location=new Point(location);
this.healthPoints=300;
this.healthPointsMax=300;
soliderIncarniation=new ImageIcon("soldier.JPG");
unitName=new String("footman");
setVisible(true);
addMouseListener(this);
addMouseMotionListener(this);
}[/b]
public void drawDetail() {
detail=new JLabel();
detail.setForeground(fraction);
detail=new JLabel(unitName+" "+healthPoints);
this.add(detail);
}
public void removeDetail() {
}
public Color getColorFraction() {
}
public void getDamage(int damageTaken) {
}
public void makingDamage(Unit u) {
}
public String toString() {
return unitName;
}
public void paint(Graphics g) {
soliderIncarniation.paintIcon(this,g,(int)location.getX(),(int)location.getY());
}
//interface methods
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}//end of unit class
}
>
Aviva at 2007-7-16 23:13:53 >

Thanks for posting the code.
You are missing a right parenthesis on this line:
Unit newUnit=new Unit(new Point(0,0); // the problem is here
It may not be the only problem.
Also, you might want to check out the spelling of the following words:
soldier (solider)
existing (exsiting)
available (aviable)
incarnation (incarniation)
automatically (automaticly)
until (untill)
appearance (appearnce)
this parenthesis probably was deleted while editing, i assure you that this isn't the problem.
it gives me the following error:
D:\JCreatorV3LE\MyProjects\Startegy\Map.java:34: cannot find symbol
symbol : constructor Unit(java.awt.Point)
location: class Unit
Unit newUnit=new Unit(new Point(0,0));
^
1 error
Any other suggestments :\?
i'll be glad to recieve solution or attempts for solution atleast.
P.s: yes my spelling sucks :X
Aviva at 2007-7-16 23:13:53 >

Unit newUnit=new Unit(new Point(0,0);
You need another bracket for starters but I guess you know that.
I think it should be
Unit newUnit=new Player.Unit(new Point(0,0));
But in any case, why would you be instantiating an inner class of another object? I don't personally agree with public inner classes anyway, they're a sign of design flaws.
Oh and a random comment... For your Map class, rather than implement various mouse interfaces I'd use a (private) inner class which extends MouseAdapter. That's the sort of thing you do want to be using inner classes for :o)
Thanks for the error message. Now we are getting somewhere.
An instance of an inner class exists within an instance of its enclosing class. That is, any instance of Unit has to exist within an instance of Player. To create a Unit, you must first create its associated Player instance:
// Within the constructor for Map
//
Player player1 = new Player();
Player.Unit newUnit = player1.new Unit(new Point(0,0));
See "instantiating" at http://mindprod.com/jgloss/nestedclasses.html
See http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
See http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
newUnit has access to all of the instance fields of player1, which may be a great convenience.
However, you may want to rethink your architecture. Which objects should create other objects? Which objects need access to fields in other objects? Which objects should hold instances of other objects?
Drawing diagrams may be helpful.
Good luck!
lol yes,i know what you are talking about, but overall it will give me teh same results :D.
and you are right, now i am thinking how to rebuild my game. and i draw also diagram of the all the methods, classes so i'll be able to orgenize the game without any problem (but of course they will appear somehow :( ).
I solved the unit stuff, but i see it wasn't what i wanted. :\
however, thanks for you help :P
Aviva at 2007-7-16 23:13:53 >
