【发布时间】:2014-11-07 13:35:04
【问题描述】:
我正在尝试从静态 main() 移动到名为paintComponent() 的非静态方法,但我遇到的问题是我无法以我所拥有的方式从静态移动到非静态。类如下,其中hunter和hunted是外部类:
import javax.swing.JFrame;
import java.awt.Graphics;
public class Main extends JFrame{ //Public class: Available for all other classes to refer to
private static final long serialVersionUID = -4511248732627763442L;
public static void main(String[] args){
frame();
repaint();
move(); //Passes to the method move() in the class Main()
}
public static JFrame frame(){
JFrame frame = new JFrame("Hunter VS Hunted"); //Sets the window title
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //Sets the size of the window
frame.setVisible(true); //Says to display the window
frame.setResizable(false); //Sets it so the screen cannot be adjusted
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the window when the x is pushed
System.out.println("Frame works.");
return frame;
}
public void paintComponent(Graphics g){
super.paintComponents(g); //Assigns a graphics value to g, so that it can be passed to other methods
Hunted.paint(g);
Hunter.paint(g);
System.out.println("Main.paintComponent works.");
}
public static void move(){
Hunter.move(); //Passes to move() in the Hunter class
Hunted.move(); //Passes to move() in the Hunter class
}
}
请记住我是初学者,所以请尽量保持简单!
【问题讨论】:
-
你到底想达到什么目的?
-
我正在尝试构建一个模拟,其中有两个对象一个猎人和一个被猎杀,它们都活着,猎人吃掉了猎物,并且都四处移动。我正在努力将它们绘制到屏幕上,作为然后移动的简单矩形。
-
我的意思是在这个过渡中..
-
看看我的代码并将其用作模型。享受
标签: java parameter-passing paint