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

📄 emulatorframe.java

📁 J2me唆哈的代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int x=(screenSize.width-screenIns.left-screenIns.right-deviceWidth)/2;
        int y=(screenSize.height-screenIns.top-screenIns.bottom-inset.top-inset.bottom-deviceHeight-MENU_BAR_HEIGHT)/2;
        this.setLocation(x,y);
    }
    
    private void initFileChoosers(){
    	openFileChooser=new JFileChooser();
    	openFileChooser.setFileFilter(new JarAndDirFileFilter(true));
    	openFileChooser.setAcceptAllFileFilterUsed(false);
    	setSnapDirChooser=new JFileChooser();
    	setSnapDirChooser.setFileFilter(new JarAndDirFileFilter(false));
    	setSnapDirChooser.setAcceptAllFileFilterUsed(false);
    	setSnapDirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    private void updateUI(){
        try {
            /*
			 * use system UI style
			 */
            String lnfName = UIManager.getSystemLookAndFeelClassName();
            UIManager.setLookAndFeel(lnfName);
            SwingUtilities.updateComponentTreeUI(this);
            openFileChooser.updateUI();
            setSnapDirChooser.updateUI();
        } catch (Exception e) {
            
        }
        
    }
    
    private void exitEmulator(){
    	if(gameThread!=null){
        	gameThread.stopGame();
        	gameThread=null;
        }
    	console.stopConsole();
    	console.dispose();
        saveConfig();
        this.dispose();
        System.gc();
        System.exit(0);
    }
    
    private void moveWindow(){
    	Rectangle rc=this.getBounds();
    	console.setLocation(rc.x-console.getWidth(),rc.y);
    	if(zoomWindow!=null){
    		zoomWindow.setLocation(rc.x+rc.width,rc.y);
    	}
    }
    
    private void reDirectErr(){
    	if(console!=null){
    		try{
            	System.setErr(new PrintStream(console.getErrStream()));
            }catch(Exception exp){
            	exp.printStackTrace();
            }
    	}
    }
    
    public static void showMessage(String msg){
    	JOptionPane.showMessageDialog(EmulatorFrame.instance,msg,"Message",JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void showZoomWindow(int zoomSize){
    	//System.out.println("zoom="+zoomSize);
    	zoom=zoomSize;
    	if(zoom>0){
    		if(zoomWindow==null){
    			zoomWindow=new ZoomWindow(this);
    		}
	    	zoomWindow.setSize(emuParam.getScreenWidth()*zoom,emuParam.getScreenHeight()*zoom);
	    	Rectangle rc=this.getBounds();
	    	zoomWindow.setLocation(rc.x+rc.width,rc.y);
	    	zoomWindow.setVisible(true);
	    	//zoomWindow.setZoom(zoom);
	    	updateZoomWindow();
    	}else{
    		zoomWindow.setVisible(false);
    	}
    }
    
    private void loadConfig(){

        openDir="./";
        snapDir="./";
        showConsole=false;
        zoom=0;
        e.restoreDefaultKey();
        try{
            BufferedReader bis=new BufferedReader(new InputStreamReader(new FileInputStream(new File("./config.ini"))));
            String s=bis.readLine();
            while(s!=null){
                int index=s.indexOf('=');
                if(index>0){
                    String key=s.substring(0,index);
                    String value=s.substring(index+1,s.length());
                    if(key.equalsIgnoreCase("opendir")){
                    	openDir=value;
                    }else if(key.equalsIgnoreCase("snapdir")){
                    	snapDir=value;
                    }else if(key.equalsIgnoreCase("Device")){
                    	try{
                    		deviceType=Integer.parseInt(value);
                    	}catch(NumberFormatException excp){
                    	}
                    }else if(key.equalsIgnoreCase("Console")){
                    	showConsole=(value.equalsIgnoreCase("true"));
                    }else if(key.equalsIgnoreCase("Zoom")){
                    	zoom=Integer.parseInt(value);
                    }else{
                    	for(int i=0;i<e.keys.length;++i){
                    		if(key.equalsIgnoreCase(e.keys[i])){
                    			e.currentKey[i]=Integer.parseInt(value);
                    		}
                    	}
                    }
                }
                s=bis.readLine();
            }
            bis.close();
        }catch(Exception e){
        }
    }
    
    private void saveConfig(){
    	try{
    		BufferedWriter bws=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("./config.ini"))));
    		bws.write("Device="+emuParam.getCurrentDevice());
    		bws.newLine();
    		bws.write("opendir="+openDir);
    		bws.newLine();
    		bws.write("snapdir="+snapDir);
    		bws.newLine();
    		bws.write("Console="+showConsole);
    		bws.newLine();
    		bws.write("Zoom="+zoom);
    		bws.newLine();
    		for(int i=0;i<e.keys.length;++i){
    			bws.write(e.keys[i]+"="+e.currentKey[i]);
    			bws.newLine();
    		}
    		bws.close();
    	}catch(Exception e){
    		
    	}
    }

    private void openFile(){
    	openFileChooser.setCurrentDirectory(new File(openDir));
    	if(openFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
    		File f=openFileChooser.getSelectedFile();
    		openDir=f.isDirectory()?f.getAbsolutePath():f.getParent();
    		startNewGame(f.getAbsolutePath());
    	}
    }
    
    private void setSnapDir(){
    	setSnapDirChooser.setCurrentDirectory(new File(snapDir));
    	if(setSnapDirChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
    		snapDir=setSnapDirChooser.getSelectedFile().getAbsolutePath();
    		if(!snapDir.endsWith("\\")&&!snapDir.endsWith("/")){
    			snapDir+="\\";
    		}
    	}
    }
    
    private void showSetKeyDialog(){
    	SetKeyDialog dlg=new SetKeyDialog(this,title);
    	Rectangle rc=this.getBounds();
    	Dimension dm=dlg.getSize();
    	dlg.setLocation(rc.x+(rc.width-dm.width)/2,rc.y+(rc.height-dm.height)/3);
    	dlg.setVisible(true);
    }
    
    private void showHelpDialog(){
    	AboutDialog aboutDlg=new AboutDialog(this,title);
    	aboutDlg.setLogo(emuParam.getLogoImage().getTrueImage());
    	Rectangle rc=this.getBounds();
    	Dimension dm=aboutDlg.getPreferredSize();
    	aboutDlg.setLocation(rc.x+(rc.width-dm.width)/2,rc.y+(rc.height-dm.height)/3);
    	aboutDlg.setVisible(true);
    }
    
    public static EmulatorFrame getInstance(int _deviceType){
    	if(instance==null){
    		instance=new EmulatorFrame(_deviceType);
    	}
    	return instance;
    }
    
    public void setMIDlet(MIDlet m){
    	if(gameThread!=null){
        	gameThread.stopGame();
        	gameThread=null;
        }
    	//gameThread=new GameThread();
    	gameThread=new GameThread(gameThreadGroup,"GameThread");
    	gameThread.setMIDlet(m);
    }
    
    public String getGameName(){
    	if(gameThread!=null){
    		return gameThread.getGameName();
    	}
    	return null;
    }
    
    public void startNewGame(String jarFileName){
    	reDirectErr();
    	if(gameThread!=null){
        	gameThread.stopGame();
        	gameThread=null;
        }
    	//gameThread=new GameThread();
    	gameThread=new GameThread(gameThreadGroup,"GameThread");
    	gameThread.setJarFileName(jarFileName);
    	startGame();
    }
    
    public void startGame(){
    	this.getJMenuBar().getMenu(0).getItem(0).setEnabled(false);
    	if(gameThread==null){
    		showMessage("MIDlet=null! Call setMIDlet(MIDlet m) before start!");
    	}else{
    		gameThread.start();
    	}
    }
    
    public void endGame(){
        if(gameThread!=null){
        	gameThread.stopGame();
        }
        resetScreen();
        this.getJMenuBar().getMenu(0).getItem(0).setEnabled(true);
        exitEmulator();
    }
    
    public void resetScreen(){

        setCurrent(null);
        System.gc();
        initTitleBar();
    }
    
    public void setCurrent(BQDisplayable d){
        if(screenPanel!=null){
            devicePanel.remove(screenPanel);
            screenPanel=null;
        }
        if(d!=null){
            screenPanel=d;
            emuParam.addScreenPanel(devicePanel,d);
            emuParam.setScreenBounds(d,screenOffsetX,screenOffsetY,screenWidth,screenHeight);
            d.requestFocus();
            d.repaint();
        }else{
        	devicePanel.repaint();
        }
        if(d instanceof BQCanvas){
        	this.requestFocus();
        }
    }
    
    public void endMIDlet(){
    	endGame();
    }
    
    public BQDisplayable getCurrent(){
        return screenPanel;
    }
    
    public void updateZoomWindow(){
    	if(zoom>0&&zoomWindow!=null&&zoomWindow.isVisible()&&screenPanel instanceof BQCanvas){
            //int w=e.getScreenWidth()*zoom;
            //int h=e.getScreenHeight()*zoom;
    		//zoomWindow.update(((BQCanvas)screenPanel).getScreenImage().getScaledInstance(w,h,BufferedImage.SCALE_FAST));
    		zoomWindow.setZoom(zoom);
    		zoomWindow.update(((BQCanvas)screenPanel).getScreenImage());
        }
    }
    
    public void saveScreen(){
        try{
	        Date current=new Date();
	        
	        String filename=snapDir+current.getTime()+".png";
	        File f=new File(filename);
	        if((!f.getParentFile().exists())){
	            f.getParentFile().mkdir();
	        }
	        if(screenPanel instanceof BQCanvas){
	            ImageIO.write(((BQCanvas)screenPanel).getScreenImage(),"png",f);
	        }else{
	        	Rectangle rc=this.getBounds();
	        	Insets ins=this.getInsets();
	        	Rectangle rc2=this.getContentPane().getBounds();
	        	int x=rc.x+ins.left+rc2.x+screenOffsetX;
	        	int y=rc.y+ins.top+rc2.y+screenOffsetY;
	        	java.awt.image.BufferedImage snap = (new Robot()).createScreenCapture(new
	        	        Rectangle(x, y, screenWidth,screenHeight));
	        	ImageIO.write(snap,"png",f);
	        }
	        
        }catch(Exception e){
        	
        }
    }

    public void showAlert(String cap, String str){
    	
    	JOptionPane.showMessageDialog(this,str,cap,JOptionPane.INFORMATION_MESSAGE);
    }
    
    public BQImage createImage(String resName) throws IOException{
    	if(gameThread==null){
    		return null;
    	}
    	return gameThread.createImage(resName);
    }

    class ChangeDeviceListener implements ActionListener {

    	private int targetDevice;
    	public ChangeDeviceListener(int _targetDevice) {
    		super();
    		targetDevice=_targetDevice;
    	}

    	public void actionPerformed(ActionEvent e) {
    		changeDevice(targetDevice);
    	}

    }
    
    class ChangeZoomSizeActListener implements ActionListener {
    	private int zoom=0;
    	ChangeZoomSizeActListener(int zoomSize){
    		zoom=zoomSize;
    	}
    	public void actionPerformed(ActionEvent evt){
    		showZoomWindow(zoom);
    	}
    }
}

⌨️ 快捷键说明

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