简体   繁体   中英

Why am I getting an exception when trying to create this Screen(window)?

I'm getting an;

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method Screen(int, int, String, Game) is undefined for the type Game

    at Game.<init>(Game.java:15)
    at Game.main(Game.java:94)

I switched my program from JGrasp to Eclipse. After copying and pasting all of my codes and classes over, I ran into the error above when trying to run the program. In JGrasp this code ran fine, but for some reason Eclipse doesn't like it.

This is the full code of the Game Class, hopefully this will help show why the exception is happening. Other class I have include, ID, Handler, GameObject, KeyInput,and Player. Each are related to their name. Hopefully this is helpful

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable {

   private static final long serialVersionUID = 1L;

   private boolean isRunning = false;
   private Thread thread;
   private Handler handler;
   //Creates background window size and holds objects by handler
   public Game() {
     Screen(1280, 720, "Deed", this);
      start();

      handler = new Handler();
      addKeyListener(new KeyInput(handler));
       handler.addObject(new Player(425, 745, ID.Player, handler));

   }

//starts a new thread
   private void start() {
      isRunning = true;
      thread = new Thread(this);
      thread.start();
   }
   // Stops current thread, and catches exceptions
   private void stop() {
      isRunning = false;
      try {
         thread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }
   //Infinite game loop
   public void run() {
      this.requestFocus();
      long lastTime = System.nanoTime();
      double amountOfTicks = 60.0;
      double ns = 1000000000 / amountOfTicks;
      double delta = 0;
      long timer = System.currentTimeMillis();
      int frames = 0;
      while(isRunning) {
         long now = System.nanoTime();
         delta += (now - lastTime) / ns;
         lastTime = now;
         while(delta >= 1) {
            tick();
            //updates++;
            delta--;
         }
         render();
         frames++;

         if(System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            frames = 0;
            //updates = 0;
         }
      }
      stop();
   }

   public void tick() {
      handler.tick();
   }
   //Holds extra frames before showing (3 extra)
   public void render() {
      BufferStrategy bs = this.getBufferStrategy();
      if(bs == null) {
         this.createBufferStrategy(5);
         return;
      }

      Graphics g = bs.getDrawGraphics();
      /////////////////Renders background first, then handlers///////////////////

      g.setColor(Color.black);
      g.fillRect(0,0,1280,720);

      handler.render(g);

      /////////////////Updates graphics////////////////////
      g.dispose();
      bs.show();

   }
   public static void main(String args[]) {
      new Game();
   }

}

This is the Screen class is;

import java.awt.Dimension;
import javax.swing.JFrame;

public class Screen {
   public Screen(int width, int height, String title, Game Game) {
      JFrame frame = new JFrame(title);

      frame.setPreferredSize(new Dimension(width, height));
      frame.setMaximumSize(new Dimension(width, height));
      frame.setMinimumSize(new Dimension(width, height));

      frame.add(Game);
      frame.setResizable(false);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

   }
}

As mentioned in the comments, a couple of pointers about the code (it would be nice to have more code to work with though):

  • No constructor with the corresponding signature exists ( Window(int, int, String, Game) ), you either need to overload the constructor with a version which has this signature or you are using one of the existing ones with the wrong argument types
  • Another problem could be that you are not importing things correctly
  • You're creating a reference to this Window without storing it anywhere, which serves no purpose, try putting it in a variable ( Window myWindow = Window(int, int, String, Game) )
  • Why are you calling addKeyListener twice?

I think that you are importing different Window class than the one you have pasted eg. java.awt.Window double check imports (and to be sure paste those as well se we can check). There is no other explanation for this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM