简体   繁体   中英

Java: having trouble calling a method from one class into another

Still trying to grasp how classes and methods work in Java. To experiment, I tried to create a graphics class, with a void draw box method inside. Then, I try to call that method in the main method to try to draw those boxes. I'm getting "cannot be resolved to variable" errors which I believe means the main class can't see my other class for some reason?

Boxymain.java:

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

public class Boxymain extends Canvas {
    public static void main(String[] args){
        BoxyMethod c = new BoxyMethod();            
        c.drawBox(window, Color.RED, 200, 300);

        JFrame win = new JFrame("Boxy Main");
        win.setSize(800,600);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Boxymain canvas = new Boxymain();
        win.add(canvas);
        win.setVisible(true);  
    }  
}

BoxyMethod.java:

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

public class BoxyMethod {   
    public void drawBox(Graphics window, Color c, int x, int y){
        window.setColor(c);
        window.fillRect(x, y, 100, 100);
        window.setColor(Color.WHITE);
        window.fillRect(x+10,y+10,80,80);
        }   
}

Error text: "window cannot be resolves to a variable."

The error message is telling you exactly what is wrong. You're passing in a window variable into the drawBox method, but you don't declare or initialize such a variable in the main method before doing so, and so this cannot be done in Java.

BoxyMethod c = new BoxyMethod();        
// *** window variable below is used but never declared prior to use
c.drawBox(window, Color.RED, 200, 300); 

More importantly though, you're not doing Swing drawing correctly.

Instead, you should create a class that extends JPanel, give it a paintComponent(Graphics g) method override, and draw in that method. Then place that JPanel in a JFrame and display the JFrame. Please check out the Performing Custom Painting Swing graphics tutorial for more detail on how to do Swing graphics.

As an aside, do not follow that tutorial that you've linked to as it is 30 years out of date.


For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;

public class BoxyTest {

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Boxy Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new BoxyPanel(200, 300));
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class BoxyPanel extends JPanel {
   private static final int PREF_W = 800;
   private static final int PREF_H = 650;
   private int myX;
   private int myY;

   public BoxyPanel(int myX, int myY) {
      this.myX = myX;
      this.myY = myY;
   }

   @Override // so my JPanel will be big enough to see
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      // call super method so that the JPanel can do housekeeping painting
      super.paintComponent(g);
      g.fillRect(myX, myY, 100, 100);
      g.setColor(Color.WHITE);
      g.fillRect(myX + 10, myY + 10, 80, 80);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM