如何在main方法中初始化变量,然后使用它在屏幕初始化后在屏幕上绘制图形

时间:2017-05-21 03:21:03

标签: java graphics

我的问题是我需要在main方法中声明所有变量,以便将来可以将它们传递给其他类。问题是我无法从main方法调用paint方法,因此我必须在绘制方法中声明所有变量。我需要的是以我们的main方法可访问的方式声明变量,然后将它们传递给我的绘图方法。我怎么能这样做似乎是不可能的,而且我花了好几个小时试图解决这个问题。代码:

package main;

import java.util.ArrayList;
import javax.swing.* ;
import java.awt.* ;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Rebellion extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;      
        doDrawing(g2d) ;
    }

 private void doDrawing(Graphics2D g2d) {
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, 800, 600);

    g2d.setPaint(Color.green);
    for (int i = 0 ; i < 601; i+= 50) {
        g2d.drawLine(0, i, 600, i) ;
        g2d.drawLine(i, 0, i, 600) ;
    }  

    //randomizing the amount of planets
    int planetCount = (int)(Math.random()*10) + 9 ;
    Planets [] worlds = new Planets[planetCount] ;
    worldGenerator(planetCount, worlds) ;

    g2d.setColor(Color.BLUE);
    for (int i = 1 ; i < planetCount ; i++) {
        int xCords = worlds[i].getX() ;
        int yCords = worlds[i].getY() ;
        g2d.fillOval(xCords,yCords,15,15) ;
    }

}

public static void main(String[] args) {

    //creating primary window
    JFrame frame = new JFrame("Rebellion Simulator 2735");
    Rebellion game = new Rebellion();
    frame.add(game) ;
    frame.setSize(800, 632);
    frame.setResizable(false); //new
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null); //new
    frame.setLayout(null) ; //new


    //Buttons
    JButton test = new JButton("Ship #");
    test.setBounds(650, 200, 100, 30);
    frame.add(test);

    test.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            StrategyScreen StrategyScreen = new StrategyScreen() ;
            StrategyScreen.setVisible(true) ;
        }
    });

    //max size of board (600,600)
    final int xMax = 600 ; 
    final int yMax = 600 ;     

 }
}

总结一下:我需要main方法来初始化变量planetCount,Planets []数组,并运行worldGenerator()方法(在代码中没有包含它并不重要)。在主要方法中初始化之后,他们必须被传递到绘画上... ...谢谢!

1 个答案:

答案 0 :(得分:0)

main方法是一个静态方法,它可以访问在类级别声明的所有静态成员,但是paint方法,即paintComponent()方法是非静态或实例方法,只能从一个方法调用叛乱类型的对象。所以我想说不要在main方法中声明你的变量。在没有静态限定符的类级别声明它们。所以这些变量都是实例变量。下一步是在构造函数Rebellion(){}中编写所有变量初始化代码,然后在main方法中创建一个类型为class的对象。虽然下面的代码不会按原样运行,但请尝试将其用作模板以理解上面提到的内容。我已经对代码进行了评论,以强调要解决问题所做的更改。

import java.text.AttributedCharacterIterator;
import java.util.ArrayList;

import javax.swing.*;

import java.awt.*;
import java.awt.image.ImageObserver;

public class Rebellion extends JPanel {
//Declare your variable as instance variables without static keyword
private JFrame frame;
private Rebellion game;
private JButton test;
private StrategyScreen StrategyScreen;

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    this.getTest(); //Just an example. Here you are accessing your JButton object which you can modify in your paint method
    Graphics2D g2d = (Graphics2D) g;
    doDrawing(g2d);
}

//All getters and setters of your Rebellion instance variables declared here

public JFrame getFrame() {
    return frame;
}

public void setFrame(JFrame frame) {
    this.frame = frame;
}

public Rebellion getGame() {
    return game;
}

public void setGame(Rebellion game) {
    this.game = game;
}

public JButton getTest() {
    return test;
}

public void setTest(JButton test) {
    this.test = test;
}

public StrategyScreen getStrategyScreen() {
    return StrategyScreen;
}

public void setStrategyScreen(StrategyScreen strategyScreen) {
    StrategyScreen = strategyScreen;
}

private void doDrawing(Graphics2D g2d) {
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, 800, 600);

    g2d.setPaint(Color.green);
    for (int i = 0; i < 601; i += 50) {
        g2d.drawLine(0, i, 600, i);
        g2d.drawLine(i, 0, i, 600);
    }

    // randomizing the amount of planets
    int planetCount = (int) (Math.random() * 10) + 9;
    Planets[] worlds = new Planets[planetCount];
    worldGenerator(planetCount, worlds);

    g2d.setColor(Color.BLUE);
    for (int i = 1; i < planetCount; i++) {
        int xCords = worlds[i].getX();
        int yCords = worlds[i].getY();
        g2d.fillOval(xCords, yCords, 15, 15);
    }

}

public Rebellion() { //Code now moved to Constructor instead of the main method
    // creating primary window
    frame = new JFrame("Rebellion Simulator 2735");
    game = new Rebellion();
    frame.add(game);
    frame.setSize(800, 632);
    frame.setResizable(false); // new
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null); // new
    frame.setLayout(null); // new

    // Buttons
    test = new JButton("Ship #");
    test.setBounds(650, 200, 100, 30);
    frame.add(test);

    test.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            StrategyScreen = new StrategyScreen();
            StrategyScreen.setVisible(true);
        }
    });

    // max size of board (600,600)
    final int xMax = 600;
    final int yMax = 600;
}

public static void main(String[] args) {
    Rebellion reb = new Rebellion(); //You just create an instance of the class which invokes the constructor and gets all your initialization done
    reb.paintComponent(g); // Pass your Graphics object here
}
}