-3

So heres my code:

package project;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

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

import project.Project.MyGraphics;

public class Project extends JFrame implements Runnable{
String thing = "none";
public static int x;
public static int y;
int w = 1600;
int h = 1000;
public int ww = 5;
public int hh = 5;
public class AL extends KeyAdapter{
    public void keyPressed(KeyEvent e) {
        int KeyCode = e.getKeyCode();
        if (KeyCode == e.VK_NUMPAD1){
            downleft();
            if (thing == "none" || thing == "d" || thing == "dr" || thing == "l" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "dl";
                System.out.println("Drawing down to the left!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }

        }
        if (KeyCode == e.VK_NUMPAD2){
            down();
            if (thing == "none" || thing == "dl" || thing == "dr" || thing == "l" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "d";
            System.out.println("Drawing down!");
        }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD3){
            downright();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "l" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "dr";
            System.out.println("Drawing down to the right!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD4){
            left();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "r" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "l";
            System.out.println("Drawing to the left");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD6){
            right();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "ul" || thing == "u" || thing == "ur") {
                thing = "r";
            System.out.println("Drawing to the right!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD7){
            upleft();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "r" || thing == "u" || thing == "ur") {
                thing = "ul";
            System.out.println("Drawing up to the left!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD8){
            up();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "r" || thing == "ul" || thing == "ur") {
                thing = "u";
            System.out.println("Drawing up!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_NUMPAD9){
            upright();
            if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
                    thing == "r" || thing == "ul" || thing == "u") {
                thing = "ur";
            System.out.println("Drawing up to the right!");
            }
            if (x > 1595) {
                x = 1595;
            }
            if (y > 974) {
                y = 974;
            }
            if (x < 0) {
                x = 0;
            }
            if (y < 0) {
                y = 0;
            }
        }
        if (KeyCode == e.VK_B){
            ww += 5;
            hh += 5;
            System.out.println("Resized! It is now " + ww + " by " + hh + "!");
            if (ww > 150) {
                ww = 150;
                hh = 150;
                System.out.println("The size went too high! Resized back to 150 by 150!");
            }
        }
        if (KeyCode == e.VK_S){
            ww -= 5;
            hh -= 5;
            System.out.println("Resized! It is now " + ww + " by " + hh + "!");
            if (ww < 5) {
                ww = 5;
                hh = 5;
                System.out.println("The size went too low! Resized back to 5 by 5!");
            }
        }
    }
}
public void downleft(){
    x-=5;
    y+=5;
}
public void down(){
    y+=5;
}
public void downright(){
    x+=5;
    y+=5;
}
public void left(){
    x-=5;
}
public void right(){
    x+=5;
}
public void upleft(){
    x-=5;
    y-=5;
}
public void up(){
    y-=5;
}
public void upright(){
    x+=5;
    y-=5;
}
public Project(){
    addKeyListener(new AL());
    setTitle("Etch a Sketch");
    setResizable(false);
    setVisible(true);
    setSize(w, h);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new MyGraphics());

    JLabel label1 = new JLabel("Size: " + x + " by " + y, JLabel.CENTER);
    label1.setAlignmentX(0);
    label1.setAlignmentY(0);
    Project.add(label1);

    x = 100;
    y = 100;
}
public static void main(String[] args) {
    new Project();
}
class MyGraphics extends JPanel {
    public void paintComponent(Graphics g) {
        g.fillRect(x, y, ww, hh);
        repaint();
    }
}
@Override
public void run() {

}
}

I'm getting an error on line 247 "Project.add(label1);". It says it can't make a static reference to the add method because the add method isn't static. Which other method am I calling besides Project. I tried making Project static but it's not a valid modifier for it. Can anyone help?

1 Answers1

3

In public Project(){ change Project.add(label1); to this.add(label1);

It is because add(..) in class Project is not a static method and if you need to call it outside this class you will have you use an instance variable or if you want with in same class as case seems to be at the moment then use keyword this.

Shahzeb
  • 4,745
  • 4
  • 27
  • 40