⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 利用JAVA实现的利用进化算法对人口增长进行仿真的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * mainFrame.java
 *
 * Created on 12 October 2006, 07:37
 */

package wpc;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowListener;
import javax.swing.JTextArea;
import java.util.Random;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 *
 * @author  User
 */
public class MainFrame extends javax.swing.JFrame implements WindowListener, ActionListener, KeyListener, wpc.data.settingsInterface {
    public String settingsPath = "settings.dat";
    wpc.data.Settings settings;    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date;    
    /** Creates new form mainFrame */
    public MainFrame() {
        loadSettings();
        initComponents();
        nextInit();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //Initialization
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel9 = new javax.swing.JLabel();
        jProgressBar2 = new javax.swing.JProgressBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        File = new javax.swing.JMenu();
        exit = new javax.swing.JMenuItem();
        View = new javax.swing.JMenu();
        GraphicView = new javax.swing.JMenuItem();
        DebugTextView = new javax.swing.JMenuItem();
        Generator = new javax.swing.JMenu();
        CreateWorld = new javax.swing.JMenuItem();
        Settings = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel1.setMaximumSize(new java.awt.Dimension(100, 32767));
        jPanel1.setPreferredSize(new java.awt.Dimension(120, 100));
        jLabel1.setText("Program is:");

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        jLabel4.setText("Current Year:");

        jLabel5.setText("jLabel5");

        jLabel7.setText("jLabel7");

        jSlider1.setPreferredSize(new java.awt.Dimension(100, 25));

        jButton1.setText("Start");

        jButton2.setText("Pause");

        jButton3.setText("Exit");

        jLabel8.setText("&");

        jLabel6.setText("Infomation");

        jLabel10.setText("Total Progress");

        jProgressBar1.setPreferredSize(new java.awt.Dimension(90, 18));

        jLabel9.setText("Part Progress");

        jProgressBar2.setPreferredSize(new java.awt.Dimension(90, 18));

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel7)
                    .add(jLabel1)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel8))
                    .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .add(jSlider1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5)
                    .add(jLabel4)
                    .add(jLabel3)
                    .add(jLabel6)
                    .add(jLabel10)
                    .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel9)
                    .add(jProgressBar2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel8))
                .add(4, 4, 4)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel5)
                .add(48, 48, 48)
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel10)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel9)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jProgressBar2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 55, Short.MAX_VALUE)
                .add(jSlider1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 401, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 422, Short.MAX_VALUE)
        );
        jScrollPane1.setViewportView(jPanel2);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        File.setText("File");
        exit.setText("Exit");
        File.add(exit);

        jMenuBar1.add(File);

        View.setText("View");
        GraphicView.setText("Graphics");
        GraphicView.setActionCommand("graphicsShow");
        View.add(GraphicView);

        DebugTextView.setText("DebugText");
        DebugTextView.setActionCommand("debugShow");
        View.add(DebugTextView);

        jMenuBar1.add(View);

        Generator.setText("Generator");
        CreateWorld.setText("Create World");
        Generator.add(CreateWorld);

        Settings.setText("Options");
        Generator.add(Settings);

        jMenuBar1.add(Generator);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //My init Routines
    public void loadSettings(){
        /*test if file exist
         *
         *
         *
         */
        File settingsFile = new File(settingsPath);
        if(settingsFile.exists()){
            try{
                FileInputStream fi = new FileInputStream(settingsFile);
                ObjectInputStream oi = new ObjectInputStream(fi);
                try{
                    settings = (wpc.data.Settings)oi.readObject();
                    oi.close();
                    fi.close();
                    settingsFile = null;
                }catch(java.lang.ClassNotFoundException e){
                    //File is corrupt
                    System.out.println("Corrupt File, re-doing");
                    settingsFile.delete();
                    settingsFile.createNewFile();
                    FileOutputStream fo = new FileOutputStream(settingsFile);
                    ObjectOutputStream oo = new ObjectOutputStream(fo);
                    settings = new wpc.data.Settings();
                    oo.writeObject(settings);
                    oo.close();
                    fo.close();
                    settingsFile = null;
                }
            }catch(java.io.IOException e){
                
            }
            
        }else{
           //Create new settings file, using default values.
           try{
              settingsFile.createNewFile();
              FileOutputStream fo = new FileOutputStream(settingsFile);
              ObjectOutputStream oo = new ObjectOutputStream(fo);
              settings = new wpc.data.Settings();
              oo.writeObject(settings);
              oo.close();
              fo.close();
              settingsFile = null;
           }catch(java.io.IOException e){
               
           }
              
           
           
        }
        
    }
    private void nextInit(){
        options = new Options(this);
        options.setAlwaysOnTop(false);
        options.setVisible(false);
        //options.set
        generator = new Random();
        world = new wpc.data.World(this);
        jSlider1.setMinimum(this.getTimerMin());
        jSlider1.setMaximum(this.getTimerMax());
        jSlider1.setValue(this.getTimerStart());        
        year=0;
        running=false;
        started=false;
        created=false;
        DebugTextView.addActionListener(this);
        GraphicView.addActionListener(this);
        exit.addActionListener(this);
        jButton1.setActionCommand("Start");
        jButton2.setActionCommand("Pause");
        Settings.setActionCommand("Options");
        CreateWorld.setActionCommand("Create");
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);
        Settings.addActionListener(this);
        CreateWorld.addActionListener(this);
        debugText = new JTextArea();
        debugText.setTabSize(4);        
        gPanel = new GraphicsPanel(this);
        gPanel.setSize(this.getWorldX(),this.getWorldY());
        gPanel.setPreferredSize(new java.awt.Dimension(this.getWorldX(), this.getWorldY()));
        gPanel.setMaximumSize(new java.awt.Dimension(this.getWorldX(), this.getWorldY()));
        calcThread=new wpc.threads.CalcThread(this);
        
        jScrollPane1.setViewportView(gPanel);
        jScrollPane1.validate();
        updateSidebar();
        this.repaint();        
    }
    
    //Updaters
    public void updateGraphics(){
        gPanel.update();
    }
    public void updateSidebar(){
        if(started){
            jLabel2.setText("Started");
        }else{
            jLabel2.setText("Stopped");
        }
        
        if(running){
            jLabel3.setText("Running");
        }else{
            jLabel3.setText("Paused");
        }
        
        jLabel5.setText(Integer.toString(year));
        
        jLabel7.setText(Integer.toString(jSlider1.getValue())) ;        
    }    
    private void showGraphics(){
        jScrollPane1.setViewportView(gPanel);
        jScrollPane1.validate();
        this.repaint();
    }    
    private void showDebugText(){
        jScrollPane1.setViewportView(debugText);
        jScrollPane1.validate();
        this.repaint();
        
    }
    public void paint(Graphics g) {
        updateSidebar();
        super.paint(g);
    } 
    
    private void start(){
        if(created){
            //do nothing
        }else{
            //start creation thread here.
            
        }
        if(started){            
            jButton1.setText("Start");
            jButton2.setText("Pause");            
        }else{
            if(calcThread.isAlive()){
                
            }else{
                //thread hasnt been started
                calcThread.start();
            }
            jButton1.setText("Stop");
            jButton2.setText("Pause");            
        }
        started=!started;
        running=!running;
        updateSidebar();
    }
    
    private void pause(){
        if(started){
            if(running){
                running=false;
                jButton2.setText("Run");
            }else{
                running=true;
                jButton2.setText("Pause");
            }
        }
        updateSidebar();
    }
    private void create(){
        if(createThread==null){
            //Thread does not exist;
            //world = new wpc.data.World(this);
            createThread=new wpc.threads.CreationThread(this);
            createThread.start();
        }else{
            if(createThread.isAlive()){
                //Thread is alive and running, kill it?
                
            }else{
                //Thread exists, but has finished
                //world = new wpc.data.World(this);
                year=0;
                createThread=new wpc.threads.CreationThread(this);
                createThread.start();
            }
        }
        
    }
    
    public void exitPressed(){
        System.exit(0);
    }
    
    public void resetCalc(){        
        calcThread = new wpc.threads.CalcThread(this);
        updateSidebar();
    }
    
    public void saveSettings(){
        File settingsFile = new File(settingsPath);
        if(settingsFile.exists()){
            settingsFile.delete();
        }
        try{
            settingsFile.createNewFile();
            FileOutputStream fo = new FileOutputStream(settingsFile);
            ObjectOutputStream oo  = new ObjectOutputStream(fo);
            oo.writeObject(settings);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -