Results 1 to 6 of 6

Thread: Java problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    english tyrant's Avatar Domesticus
    Join Date
    Dec 2006
    Location
    Colchester
    Posts
    2,112

    Default Java problem

    I have 3 classes, lets just call them Class1 Class2 and Class3.
    Class1 contains my Public Static Main etc method, in which it declares an instance of Class2.
    Class2 InstanceClass2 = new Class2();
    Class1 also calls the Repaint method of InstanceClass2

    Class2 extends JFrame.
    Class2 declares an instance of Class3.
    Class3 InstanceClass3 = new Class3();
    Class2 then inside it's repaint method calls some methods from Class3.

    Class3 in one of it's methods called by InstanceClass2 tries to access the getHeight() method from InstanceClass2.
    InstanceClass2.getHeight();
    The Error I recieve is that the symbol "InstanceClass2" is not valid. Or something along those lines.

    All Methods and classes are public.
    I have tried saving the getHeight method to a public variable within Class2, but Class3 still couldn't access it.


    Help!



    Visual aid =




    I hope you could understand that. I thought it would be easier to use generic names rather than the actual names I have used, but in retrospect, I've made it look more complicated than it actually is.

  2. #2
    Juvenal's Avatar love your noggin
    Patrician Content Emeritus

    Join Date
    Apr 2006
    Location
    The Home Counties
    Posts
    3,465

    Default Re: Java problem

    I think we need to see your actual code... it isn't clear where the Class3 method is getting its reference to instanceClass2, or exactly how it is trying to use it.
    imb39 ...is my daddy!
    See AARtistry in action: Spite of Severus and Severus the God

    Support the MAARC!
    Tale of the Week Needs You!


  3. #3
    Dewy's Avatar Something Witty
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    4,697

    Default Re: Java problem

    The problem is the way you're referencing the instance of class 2 in class 3. Class 3 doesn't know what the instance is. That's what I gathered from what you said but I can't be sure as I haven't seen the code.
    Oh no the picture of my dog disappeared!

  4. #4
    english tyrant's Avatar Domesticus
    Join Date
    Dec 2006
    Location
    Colchester
    Posts
    2,112

    Default Re: Java problem

    Quote Originally Posted by Dewy View Post
    The problem is the way you're referencing the instance of class 2 in class 3. Class 3 doesn't know what the instance is. That's what I gathered from what you said but I can't be sure as I haven't seen the code.
    Sounds about right.

    Class 1
    Spoiler Alert, click show to read: 
    Code:
    public class Launcher {
        
        public static void main( String args[]) {
            simplegraphics SG = new simplegraphics();
            
            while(true) {
                SG.repaint();
            }
            
        }
    }


    Class 2
    Spoiler Alert, click show to read: 
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class simplegraphics extends JFrame {
    
        Square MySquare = new Square();
        public int Height = getHeight(); //just testing if I could access it after it has been stored as a public variable, apparently not.
        public int Width = getWidth();
        
        public simplegraphics(){
            setVisible(true);
            setSize(1200,1000);
        }
    
         public void clear(Graphics Window) {
            Window.setColor(Color.white);                   // set to white
            Window.fillRect(0, 0, getWidth(), getHeight()); // clear background
        }   
         
         
         
        public void paint(Graphics Window){
            Window.translate(0, 25);         //Accounts for titlebar
    
            clear(Window);
            MySquare.move();
            MySquare.draw(Window);       
        }    
    }


    Class 3
    Spoiler Alert, click show to read: 
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Square extends JFrame{
        
        int x = 50, y = 50, xsize = 100, ysize = 100, xvel = 1, yvel = 1, speed = 1;
        
        
        void move() {
            if(x > SG.getWidth()) // this
                xvel = -xvel;
            if(x < 0)
                xvel = -xvel;
            if(y > SG.getHeight()) // and this. Are my problems.
                yvel = -yvel;
            if(y < 0)
                yvel = -yvel;
            
            x = x + xvel;
            y = y + yvel;    
            
            
        }
        
        
        void draw(Graphics Window) {
            Window.setColor(Color.black);                   // set to black
            Window.fillRect(x, y, xsize, ysize);            // draw square
        }
    }

  5. #5
    Juvenal's Avatar love your noggin
    Patrician Content Emeritus

    Join Date
    Apr 2006
    Location
    The Home Counties
    Posts
    3,465

    Default Re: Java problem

    When you create an instance of Square, you need to pass a reference to the instance of simplegraphics which created it. The easiest way to do this is in the constructor:

    Code:
    public class Square extends JFrame{ 
    
        int x = 50, y = 50, xsize = 100, ysize = 100, xvel = 1, yvel = 1, speed = 1; 
        simplegraphics myParent;
    
        Square (simplegraphics daddy) {
            myParent = daddy;
        }
    
        void move() {
            if(x > myParent.getWidth()) // this
                xvel = -xvel;
            if(x < 0)
                xvel = -xvel;
            if(y > myParent.getHeight()) // and this. Are my problems.
                yvel = -yvel;
            if(y < 0)
                yvel = -yvel;
            
            x = x + xvel;
            y = y + yvel;    
        }
    }
    Don't forget in simplegraphics to pass the instance when creating Square:

    Code:
    Square MySquare = new Square(this);
    I ran this in NetBeans and got a nice bouncing box.
    imb39 ...is my daddy!
    See AARtistry in action: Spite of Severus and Severus the God

    Support the MAARC!
    Tale of the Week Needs You!


  6. #6
    english tyrant's Avatar Domesticus
    Join Date
    Dec 2006
    Location
    Colchester
    Posts
    2,112

    Default Re: Java problem

    Ahh, of course, that makes sense now.
    Thankyou very much. This had been irritating me for a while.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •