0

I am doing some test code for an inventory system (I plan to work on a game later on) using Javax components. I was making a class called 'Cursor' which extends 'ItemStack' so that you can "pick up" items you click on in an inventory screen. The problem I'm having is that I use a static variable 'cur' as to hold the instance of this class, but every time I try to compile, I get the following error:

.\john\window\component\ItemSlotPanel.java:13: error: cannot find symbol
    itemStack.swapWith(Cursor.cur);
                             ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\ItemSlotPanel.java:25: error: package itemstack.item doe
s not exist
      g.drawImage(itemstack.item.icon.getScaledInstance(this.getWidth(),this.get
Height(),Image.SCALE_SMOOTH),0,0,null);
                                ^
.\john\window\component\InfiniteItemSlotPanel.java:15: error: cannot find symbol

    if (Cursor.cur.isEmpty()) {
              ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\InfiniteItemSlotPanel.java:16: error: cannot find symbol

      this.itemStack.copyTo(Cursor.cur);
                                  ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\InfiniteItemSlotPanel.java:17: error: cannot find symbol

    } else if (Cursor.cur.item.id == this.itemStack.item.id) {
                     ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\InfiniteItemSlotPanel.java:18: error: cannot find symbol

      Cursor.cur.count += this.itemStack.count;
            ^
  symbol:   variable cur
  location: class Cursor
6 errors

which implies that the variable doesn't exist

I have attempted to find similar problems on this site (as well as through Google), but most of what I have found is irrelevant. On StackOverflow, I found questions like What is reflection and why is it useful?, Bytecode features not avaliable in the Java language, and Benefits of prototypal inheritance over classical.

The class in question:

package john;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import john.item.*;
import john.window.*;
import john.window.component.*;

public class Cursor extends ItemStack implements MouseListener,MouseMotionListener {
  public static Cursor cur = new Cursor();

  public Cursor() {
    super(null);
  }

  public static Cursor getCursor() {
    return cur;
  }

  public void mouseClicked(MouseEvent e) {
    Object source = e.getSource();
    if (source instanceof InfiniteItemSlotPanel) {
      ((InfiniteItemSlotPanel) source).mouseClicked(e);
    } else if (source instanceof ItemSlotPanel) {
      ((ItemSlotPanel) source).mouseClicked(e);
    }
  }
  public void mouseEntered(MouseEvent e) {

  }
  public void mouseExited(MouseEvent e) {

  }
  public void mousePressed(MouseEvent e) {

  }
  public void mouseReleased(MouseEvent e) {

  }
  public void mouseDragged(MouseEvent e) {

  }
  public void mouseMoved(MouseEvent e) {

  }

  public Cursor addListenerTo(Component x) {
    x.addMouseListener(this);
    x.addMouseMotionListener(this);
    return this;
  }
}

ItemStack.java:

package john.item;

public class ItemStack {
  public Item item;
  public int count;

  public ItemStack(Item item) {
    this(item,1);
  }
  public ItemStack(Item item,int count) {
    this.item = item;
    this.count = count;
  }

  public boolean isEmpty() {
    return this.item == null && this.count == 0;
  }
  public void check() {
    if (this.count < 0) {this.count = 0;this.item = null;}
    if (this.item == null && this.count > 0) this.count = 0;
    if (this.count == 0 && this.item != null) this.item = null;
  }
  public void dispose() {
    this.item = null;
    this.count = 0;
  }
  public boolean canInsert(ItemStack stack) {
    return this.item.id == stack.item.id;
  }
  public void insert(ItemStack stack) {
    if (!canInsert(stack)) return;
    this.count += stack.count;
  }
  public void insertInto(ItemStack dest) {
    dest.insert(this);
  }
  public void copyTo(ItemStack dest) {
    dest.item = this.item;
    dest.count = this.count;
  }
}

If you need more code than this, don't hesitate to ask, I'll do my best to condense it, but it currently consists of 14 interlocking classes, so I don't know how well that will go.

I am not sure how well this question will be received, but I don't know where else to go. I apologise if I am wasting time, but I tried to make the question fit StackOverflow's guidelines. Any reply is appreciated (even negative ones). I just really need to figure out why this is happening. The last time I had a problem similar to this one, I had to delete and rewrite everything, so any information would be great! Also, sorry for my poor english (I am american born-and-raised, but I don't write this kind of thing very often anymore).

JHNUXER
  • 32
  • 6
  • 1
    How are you importing `Cursor` into the `ItemStack` class? – Kon Aug 17 '17 at 22:59
  • 2
    Are you sure you have imported the correct Cursor class? – takendarkk Aug 17 '17 at 22:59
  • Ah, no I don't believe I have, now that you mention it. I'll try that. (Figured it would be a different kind of error, but maybe not) – JHNUXER Aug 17 '17 at 23:03
  • @JHNUXER Make sure to import the one from the correct package, *not* from the `java.awt.*` package which also has a `Cursor` class. – Kon Aug 17 '17 at 23:04
  • no use, same error. Thank you for your comment though. – JHNUXER Aug 17 '17 at 23:04
  • @Kon Thank you! I had no idea there was a conflicting class. I did use `import java.awt.*` in a lot of my classes, so that very well could be the issue. Thanks again! :) – JHNUXER Aug 17 '17 at 23:05
  • @Kon Thank you, this fixed the issue! I just need to apply to the other classes now. THanks again to everyone for your help! – JHNUXER Aug 17 '17 at 23:08

0 Answers0