Graphics2D cannot be resolved to a type

387 views Asked by At

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

0

There are 0 answers