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

📄 aquamatic.java

📁 用java写的apple机上的小游戏aquamatic
💻 JAVA
字号:
package org.orchesta.mathena.aquamatic;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.util.Locale;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Aquamatic extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private ResourceManager rm = new ResourceManager();
	private GameMap gm;
	private String level="1";
	private JComboBox levellist;
	private JLabel playland;
	private AquaMouseListener gamectrl;
	private JLabel goalLab; 
	private BufferedImage goal;
	private int step =0;
	private JLabel steps;
	//为了方便做出答案,我在右上作了,最终答案的布局,
	//但现在没时间想算法,所以先做成手工调整了
	private int xJust;
	private int yJust;
	private JButton justRightSubmit;
	private JButton justLeftSubmit;
	private JButton justUpSubmit;
	private JButton justDownSubmit;
	private JButton buildMolecularButton;
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JComboBox) 
			level = (String)((JComboBox)e.getSource()).getSelectedItem();
			//gm = rm.getGameMap(level);
			this.reload();
	}
	
	
	public void reload() {
		gamectrl.active=true;
		gm = rm.getGameMap(level);
		this.setTitle("Aquatomic -- "+ gm.getName());
		BufferedImage background = getEmptyMap();

		playland.setIcon(new ImageIcon(gm.getBasicMap(background)));
		playland.setText("");
		goal = gm.getMolecular();
		goalLab.setIcon(new ImageIcon(goal.getScaledInstance(goal.getWidth()* 3 / 5, goal.getHeight()* 3 /5, Image.SCALE_SMOOTH)));
		goalLab.setVerticalTextPosition(JLabel.TOP);
		goalLab.setHorizontalAlignment(JLabel.CENTER);
		//goalLab.setText("<HTML><H2>GOAL</H2></HTML>");
		step =0;
		steps.setText(rm.getLocalizedMsg(Locale.CHINESE) + 0);
		setTitle("Aquatomic -- "+ gm.getName());
	}
	
	public Aquamatic() {
		
		this.addWindowListener( new WindowListener() {
			public void windowClosing(WindowEvent e) {System.exit(0);}
			public void windowClosed (WindowEvent e) { }
			public void windowOpened(WindowEvent e) { }
			public void windowIconified(WindowEvent e) { }
			public void windowDeiconified(WindowEvent e) { } 
			public void windowActivated(WindowEvent e) { }
			public void windowDeactivated(WindowEvent e) {}
		});

		playland = new JLabel();
		gamectrl = new AquaMouseListener();
		gamectrl.setAquamatic(this);
		gamectrl.setBoard(playland);
		gamectrl.active=true;
		playland.addMouseListener(gamectrl);
		playland.setBounds(20, 20, 450, 450);
		playland.setOpaque(true);
		playland.setBackground(new Color(243,243,243));
		playland.setForeground(new Color(180,180,180));
		this.getContentPane().add(playland);
	
		goalLab= new JLabel();
		goalLab.setOpaque(true);
		goalLab.setBackground(new Color(243,243,243));
		goalLab.setBounds(480, 20 , 200+100, 200+100);
		//goalLab.setHorizontalAlignment(JLabel.TOP);
		this.getContentPane().add(goalLab);
		
		String[] level = new String[82];
		for (int i=0; i<82; i++) level[i]= ""+(i+1);
		levellist = new JComboBox(level);
		levellist.addActionListener(this);
		//levellist.setBounds(485, 235, 195, 30);
		//this.getContentPane().add(levellist);
		
		steps = new JLabel();
		//steps.setBounds(485, 275, 100, 20);
		//this.getContentPane().add(steps);

		justLeftSubmit = new JButton("L");
		justRightSubmit = new JButton("R");
		justUpSubmit = new JButton("U");
		justDownSubmit = new JButton("D");
		buildMolecularButton = new JButton("C");
		
		JPanel buttomRightPane = new JPanel();
		buttomRightPane.setLayout(new BoxLayout(buttomRightPane, BoxLayout.Y_AXIS));
		buttomRightPane.add(levellist);
		buttomRightPane.add(steps);
		
		JPanel justPane = new JPanel();
		justPane.setSize(30, 20);
		justPane.setLayout(new BoxLayout(justPane, BoxLayout.X_AXIS));
		

		justPane.add(justRightSubmit);
		justPane.add(justLeftSubmit);
		justPane.add(justUpSubmit);
		justPane.add(justDownSubmit);
		justPane.add(buildMolecularButton);
		
		buttomRightPane.add(justPane);
		//buttomRightPane.setVisible(true);
		buttomRightPane.setBounds(485, 255+100, 200, 80);
		this.getContentPane().add(buttomRightPane);
		
		ActionListener buttonJust = (new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				Object o = e.getSource();
				if(o instanceof JButton){
					JButton jb = (JButton)o;
					String text = jb.getText();
					if("R".equals(text)){
						xJust ++;
					}else if("L".equals(text)){
						xJust --;
					}else if("U".equals(text)){
						yJust --;
					}else if("D".equals(text)){
						yJust ++;
					}else if("C".equals(text)){
						xJust=0;
						yJust=0;
					}
					if(xJust < 0)xJust=0;
					if(yJust < 0)yJust=0;
					if("C".equals(text)){
						gm.buildMolecularOnly();
					}else{
						gm.buidMolecularOnFelder(getXJust(), getYJust());
					}
					goal = gm.getMolecular();
					goalLab.setIcon(new ImageIcon(goal.getScaledInstance(goal.getWidth()* 3 / 5, goal.getHeight()* 3 /5, Image.SCALE_SMOOTH)));
					//goalLab.setIcon(new ImageIcon(goal));
					goalLab.setVerticalTextPosition(JLabel.TOP);
					goalLab.setHorizontalAlignment(JLabel.CENTER);
				}
				
			}

		});
		justRightSubmit.addActionListener(buttonJust);
		justLeftSubmit.addActionListener(buttonJust);
		justUpSubmit.addActionListener(buttonJust);
		justDownSubmit.addActionListener(buttonJust);
		buildMolecularButton.addActionListener(buttonJust);
	}
	public int getXJust(){
		return xJust;
	}
	public int getYJust(){
		return yJust;
	}

	public BufferedImage getEmptyMap() {
		BufferedImage background = new BufferedImage(15*30, 15*30, BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D gb = background.createGraphics();
		gb.setColor(new Color(180,180,180));
		gb.drawRoundRect(5,5,440,440 ,10,10);
		gb.drawRoundRect(5,6,440,439, 10,11);
		gb.drawRoundRect(5,7,440,438, 11,11);
		return background;
	}
	
	
	public void increaseStep(){
		step++;
		steps.setText(rm.getLocalizedMsg(Locale.CHINESE) + step);
	}
	
	/**
	 * @param args
	 */
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Aquamatic jf = new Aquamatic();
			jf.reload();
			jf.getContentPane().setLayout(null);
			jf.setSize(720+100,540+100);
			jf.setResizable(false);
			jf.setVisible(true);
			
			} catch (Exception e) {e.printStackTrace();}
	}

}

⌨️ 快捷键说明

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