Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Overriding a method question.  (Read 888 times)
0 Members and 1 Guest are viewing this topic.
Offline Lucasmontec

Senior Newbie


Medals: 1



« Posted 2010-04-18 19:48:20 »

Hello, and thanks for reading this.
I'm pretty new to Java development and i caught myself here in this AWESOME  Grin site were things seems to work better than trying every thing all alone.
I am trying to use a game base engine that i found here in the tutorial section but for some reason i can't override any class. I create the base class and a Game main class to place the game code separately, than i extended the Game class to the base class this way i could override the update method and the render method. For some weird reason none of those are accepting my overrides Cry .

The code:

---------------------The (modified) Base class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Base implements Runnable{
   
   int WIDTH = 1024;
   int HEIGHT = 768;
   
   JFrame frame;
   Canvas canvas;
   BufferStrategy bufferStrategy;
   
   private GraphicsDevice  gd;
   private DisplayMode defaultDM;
   
   public static final DisplayMode DefaultDisplaySet[]={
      //640x480
     new DisplayMode(640,480,32,0),
      new DisplayMode(640,480,16,0),
      //800x600
     new DisplayMode(800,600,32,0),
      new DisplayMode(800,600,16,0),
      //1024x768
     new DisplayMode(1024,768,32,0),
      new DisplayMode(1024,768,16,0),
      //1440x900
     new DisplayMode(1440,900,32,0),
      new DisplayMode(1440,900,16,0)
   };
   
   public Base(){
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
      gd = env.getDefaultScreenDevice();
      defaultDM = gd.getDisplayMode();
     
      frame = new JFrame("Basic Game");
      frame.setUndecorated(true);
        gd.setFullScreenWindow(frame);
        if (gd.isDisplayChangeSupported())
        {
            gd.setDisplayMode(checkCompatibleMode(DefaultDisplaySet[7]));
            this.WIDTH = gd.getDisplayMode().getWidth();
            this.HEIGHT = gd.getDisplayMode().getHeight();
        }
         
      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);
     
      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);
     
      panel.add(canvas);
     
      canvas.addMouseListener(new MouseControl());
     
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);
     
      initialize();
     
      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();
     
      canvas.requestFocus();
   }
   
   //diplay mode handlers
  public DisplayMode[] getCompatibleDisplayModes(){
      return gd.getDisplayModes();
   }
   
   public DisplayMode checkCompatibleMode(DisplayMode mode){
      DisplayMode goodModes[] = getCompatibleDisplayModes();
         for(int j=0;j<goodModes.length;j++){
            if (DisplayModesMatch(mode,goodModes[j])){
               return mode;
            }
      }
      return null;
   }
   
   //compara displaymodes
  public boolean DisplayModesMatch(DisplayMode d1, DisplayMode d2){
      if(d1.getWidth() != d2.getWidth() || d1.getHeight() != d2.getHeight()){
         return false;
      }
      if(d1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && d2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && d1.getBitDepth() != d2.getBitDepth()){
         return false;
      }
      if(d1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && d2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && d1.getRefreshRate() != d2.getRefreshRate()){
         return false;
      }
     
      return true;
   }
   
   
   //initialize - override
 
   protected void initialize(){}
   
   //the keyInput for shutting down
 
   private class KeyInputHandler extends KeyAdapter
   {
                   public void keyPressed(KeyEvent e)
                   {
                           if(e.getKeyCode()==27)
                   {
                                   gd.setDisplayMode(defaultDM);
                                   gd.setFullScreenWindow(null);
                           System.exit(0);
                   }
                   }
                 
                   public void keyReleased(KeyEvent e)
                   {
                         
                   }
   }
   
   //the mouse listeners
 
   private class MouseControl extends MouseAdapter{
     
   }
   
   long desiredFPS = 100;
    long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
   
   boolean running = true;
   
   //the separate thread func
 
   public void run(){
     
      long beginLoopTime;
      long endLoopTime;
      long currentUpdateTime = System.nanoTime();
      long lastUpdateTime;
      long deltaLoop;
     
      while(running){
         beginLoopTime = System.nanoTime();
         
         render();
         
         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();
         update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
         
         endLoopTime = System.nanoTime();
         deltaLoop = endLoopTime - beginLoopTime;
           
           if(deltaLoop > desiredDeltaLoop){
               //Do nothing. We are already late.
          }else{
               try{
                   Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
               }catch(InterruptedException e){
                   //Do nothing
              }
           }
      }
   }
   
   //rendering
 
   private void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }

   int counter = 0;
   
   //updating - overriding
 
   public void update(int deltaTime){
   }
   
   //rendering - overriding
 
   public void render(Graphics2D g){
      g.setColor(Color.black);
      g.fillRect(0, 0, this.WIDTH, this.HEIGHT);
   }
   
   //main thread func
 
   public static void main(String [] args){
      Base ex = new Base();
      new Thread(ex).start();
   }
   
}


---------------------The Main Game Class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class Game extends Base{
   
   BufferedImage bg = null;
   int counter = 0;
   
   protected void initialize(){
   try {
       bg = ImageIO.read(new File("bg.png"));
   } catch (IOException e) {}
   }
   
   public void update(int deltaTime){
      counter++;
      if (counter > 100){
         counter = 0;
         JOptionPane.showMessageDialog(null, "UPDATE FUNCIONANDO."); //NOT WORKING
     }
   }
   public void render(Graphics2D g){
      g.setColor(Color.blue);
      g.fillRect(0, 0, 1024, 768); //NOT WORKING EITHER
     //g.drawImage(bg, 0, 0, canvas);
  }

}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-04-18 20:54:35 »

That should be working, we'd have to see more code. Are you instantiating a Base or a Game class?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
public class SuperClass
{
    public void doSomething()
    {
        System.out.println("SuperClass is printing.");
    }
}

public class SubClass extends SuperClass
{
    public void doSomething()
    {
        System.out.println("SubClass is printing.");
    }
}

public class DoStuffClass
{
    public static void main(String[] args)
    {
        SuperClass myClass = new SuperClass();
        myClass.doSomething(); //Prints "SuperClass is printing."

        myClass = new SubClass();
        myClass.doSomething(); //Prints "SubClass is printing."

        SubClass myOtherClass = new SubClass();
        myOtherClass.doSomething(); //Prints "SubClass is printing."

        //This is illegal:
       SubClass myThirdClass = new SuperClass();
        myThirdClass.doSomething(); //Illegal
   }
}


So have a look at the DoStuffClass, and make sure you're instantiating your class in the correct way. As long as you're calling new Game(), the correct methods should be getting called. But if you call new Base(), then you'll still be getting stuff from the Base class.

Forgive me if this was obvious to you, but it's the only way that I can see you getting no overriding. Or if your method signatures were not the same, but I can see above that they are.

See my work:
OTC Software
Offline Lucasmontec

Senior Newbie


Medals: 1



« Reply #2 - Posted 2010-04-18 22:04:17 »

Well, thats all my code so far... I'm running(calling) the Game class where I extend the base class.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2010-04-19 14:08:08 »

Well, thats all my code so far... I'm running(calling) the Game class where I extend the base class.
Sorry, I should have looked further down. You did exactly what I said would be a problem:

1  
2  
3  
4  
public static void main(String [] args){
   Base ex = new Base();
   new Thread(ex).start();
}


If you call a new Base(), you are making a new Base class, not a new Game class. You want to do:

1  
2  
3  
4  
public static void main(String [] args){
   Base ex = new Game();
   new Thread(ex).start();
}


or

1  
2  
3  
4  
public static void main(String [] args){
   Game ex = new Game();
   new Thread(ex).start();
}


It's all about which object you are actually instantiating.

See my work:
OTC Software
Offline Lucasmontec

Senior Newbie


Medals: 1



« Reply #4 - Posted 2010-04-19 17:18:13 »

MAN! I LUV YOU! Thank you so much!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2010-04-19 22:39:45 »

MAN! I LUV YOU! Thank you so much!
No problem. Smiley

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (11 views)
2014-09-21 23:33:17

BurntPizza (29 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (30 views)
2014-09-19 03:14:18

Dwinin (47 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 16:40:00

List of Learning Resources
by SilverTiger
2014-08-06 01:33:27

Resources for WIP games
by CogWheelz
2014-08-01 22:20:17

Resources for WIP games
by CogWheelz
2014-08-01 22:19:50

List of Learning Resources
by SilverTiger
2014-07-31 22:29:50

List of Learning Resources
by SilverTiger
2014-07-31 22:26:06

List of Learning Resources
by SilverTiger
2014-07-31 17:54:12

HotSpot Options
by dleskov
2014-07-08 07:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!