0

I'm creating a game while following a tutorial and I came across an error. It shows Graphics2D cannot be resolved to a type. I'm new to java and I'm using Eclipse IDE (JavaSE-12). I imported the import java.awt.Graphics2D and it seems to remove the error, but as soon as I run the program the error shows up again. Everything else works fine and let's me import other things (Graphics, Dimension and JPanel for example). I would appreciate your answers!

package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{
    
    //screen settings
    final int originalTileSize = 16;
    final int scale = 3; // 48x48

    final int tileSize = originalTileSize * scale;
    final int maxScreenCol = 16;
    final int maxScreenRow = 12;
    final int screenWidth = tileSize * maxScreenCol;
    final int screenHeight = tileSize * maxScreenRow;
    
    Thread gameThread;
    
    public GamePanel() {
        
        this.setPreferredSize(new Dimension(screenWidth, screenHeight));
        this.setBackground(Color.pink);
        this.setDoubleBuffered(true);
    }

    public void startGameThread() {
        
        gameThread = new Thread(this);
        gameThread.start();
    }
    
    @Override
    public void run() {

        while(gameThread != null) {
            update();
            
            repaint();
        }
        
    }
    
    public void update() {
        
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g; <--- this line shows an error
        
        // (I'm trying to create a rectangle after I resolve the issue with Graphics)
        //g2.setColor(Color.white);
        //g2.fillRect(100, 100, tileSize, tileSize);
        //g2.dispose();
    }
}

UPDATE: My CHECKED Type filters are:

  • com.sun*
  • io.micrometer.shaded*
  • jdk.*
  • sun.*
  • java.awt.List

The exact error messages:

  • Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: Graphics2D cannot be resolved to a type
  • The import java.awt.Graphics2D cannot be resolved

The youtube tutorial I'm following is: https://www.youtube.com/watch?v=VpH33Uw-_0E&list=PL_QPQmz5C6WUF-pOQDsbsKbaBZqXj4qSq&index=2 This is the second part of the tutorial and I'm at 7:00 minutes

UPDATE: removing module-info.java didn't work, but I'll show you how it looks like right now:

module Maistas {
   exports main;

   requires transitive java.desktop;
}

Structure: enter image description here

Paul
  • 53
  • 4
  • 1
    What is the exact error message? What are the exact steps to reproduce your problem? Please also tell what you have in the preferences _Java > Appearance > Type Filter_ since this might be related (even though it is unclear to me what you are talking about). – howlger Feb 19 '23 at 16:55
  • @howlger I added more information, hope it helps! – Paul Feb 19 '23 at 17:19
  • `The import java.awt.Graphics2D cannot be resolved` is a compile error, preventing parts to be compiled. If you ignore this compile error, and run it and run into the not compiled code, you get `Unresolved compilation problems` at run time. If you have a `module-info.java` file, delete it (if needed you can recreate it later via right-click on project and _Configure_) or [in `module-info.java` add the required `requires ...;` statement](https://en.wikipedia.org/wiki/Java_Platform_Module_System#Properties_of_modules). If this does not help, please show a screenshot of structure, editor, error. – howlger Feb 19 '23 at 17:51
  • @howlger deleting it didn't help and I have requires java.desktop in the module, but it doesn't seem to help – Paul Feb 19 '23 at 18:12
  • Maybe https://stackoverflow.com/questions/13705087/the-import-java-awt-cannot-be-resolved-and-cannot-be-resolved-to-a-typ – MadProgrammer Feb 19 '23 at 20:28
  • And while we’re at it - this looks like a really bad starting point as Swing is not Thread safe and single threaded - this means you shouldn’t be updating the ui or any state it relies on from outside the context of event dispatching thread. A Swing Timer would be a better place to start – MadProgrammer Feb 19 '23 at 20:31
  • If they recommend using `KeyListener` for input monitoring, defiantly stop watching. If you're just learning, I would suggest starting with JavaFX instead – MadProgrammer Feb 19 '23 at 20:34
  • 1
    Which Eclipse version? And why Java 12? It’s outdated and not an LTE version. – Holger Feb 20 '23 at 08:46

0 Answers0