0

This is intended to be a To-Do List and when the user selects 5, an image should show on a Canvas, but I cannot access the paint(Graphics g) method from the Canvas class, in my Tester.

I'm getting an error in the Tester that says that a non-static method cannot be referenced from a static context. Also variable g, in case 5, cannot be resolved.

Here's the Canvas class

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


class Motivation extends JFrame {


{
    Motivation a = new Motivation();
    a.setVisible(true);
}


Image img = Toolkit.getDefaultToolkit().getImage("motivation.jpg");


public Motivation() {

    super("motivation");
    getContentPane().add(new Canvas(), BorderLayout.CENTER);
    setSize(500, 500);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}



class MyCanvas extends Canvas  {

        public void paint(Graphics g) {
            g.drawImage(img, 5, 5, this);
            paint(g);
        }
    }
}

This is the Tester

import java.awt.*;
import java.io.*;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;

public class ToDoListTester {


public static void main(String[] args) throws IOException {
    AtomicReference<String> userName = new AtomicReference<String>();
    Scanner name = new Scanner(System.in);
    System.out.println("\n FINALS TO-DO LIST \n");
    System.out.println("Please Enter Your name");
    userName.set(name.nextLine());
    System.out.println("Hello " + userName + "!");

    int select = -1;
    while (select != 0) {
        select = ToDoList.menu();
        switch (select) {
            case 1:                             // case: different form of for/while loop. If the user selects 1, show the To Do list
                ToDoList.showList();
                break;
            case 2:                             //if the user selects 2, show the To Do list
                ToDoList.addTask();
                break;
            case 3:                             //if the user selects 3, show the To Do list
                ToDoList.removeTask();
                break;
            case 4:
                ToDoList.setTimer();                     //if the user selects 4, the countdown timer is started.
                break;
            case 5:
                Motivation motivateMe = new Motivation();
                motivateMe.MyCanvas.paint(Graphics g);
            case 0:                             //if the user selects 0, the program is stopped, takes us out of the loop.
                break;
            default:                            // different form of else. If the user does something we don't recognize/
                System.out.println("Well you're just a little rebel aren't you?");
                System.out.println("Try again.");
                System.out.println("Select 1 to display your To Do List.");
                System.out.println("Select 2 to add a task to your To Do list.");
                System.out.println("Select 3 to remove a task from your To Do list.");
                System.out.println("Select 4 to set a countdown timer for your task.");
                System.out.println("Select 5 for some motivation. ");
                System.out.println("Select 0 to exit the program.");
        }
    }
}

}

Consuela
  • 1
  • 1
  • Can you elaborate a bit on your error message please? – Joe C Dec 30 '16 at 19:28
  • @JoeC case 5: Motivation.MyCanvas.paint(g); When I try to access the paint method from the MyCanvas class, I get: "non-static method 'paint(java.awt.Graphics)' cannot be referenced from a static context." At this point I am not even sure that I have the correct code to access the method from the Main class. – Consuela Dec 30 '16 at 19:34
  • Possible duplicate of [Non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Joe C Dec 30 '16 at 19:36
  • @JoeC Thank you, that was a good suggestion. What I got from that is that I need to create an instance before calling the method. However, I'm still getting an error that the variable 'g' cannot be resolved – Consuela Dec 30 '16 at 19:55
  • And where are you defining `g` in your `main` method? – Joe C Dec 30 '16 at 20:35
  • @JoeC I have now defined g in the Main method as : Motivation g = new Motivation(); Motivation.MyCanvas.paint(g); – Consuela Dec 30 '16 at 21:25
  • @JoeC but I am still getting the non-static method error – Consuela Dec 30 '16 at 21:25
  • Right, may I suggest that you purchase a good book on Java fundamentals, and another one on Swing. – Joe C Dec 30 '16 at 21:26

1 Answers1

0

Never invoke a paint(...) method directly, Swing will determine when a component needs to be repainted. Swing will then create the Graphics object to be used by the painting method.

If you want to manually repaint the component then you use:

someComponent.repaint();

Although in your case then is no need to do this. After you create the component you actually have to add the Canvas to the frame in order for it to be painted.

Also, why are you using a Canvas? That is an AWT component. You should be using JPanel when using Swing.

Actually if you are just trying to display an image then use a JLabel. Read the section from the Swing tutorial on How to Use Icons for more information and working examples.

camickr
  • 321,443
  • 19
  • 166
  • 288