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

📄 queenframe.java

📁 用java实现几个算法和应用程序,有一定的参考价值
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;

public class QueenFrame extends Frame implements ActionListener {
    private static final int FRAME_START_X = 200;
    private static final int FRAME_START_Y = 200;
    private static final int FRAME_SIZE = 400;
    private static final int PANEL_SIZE = 50;
    private static final int QUEEN_START_X = 50;
    private static final int QUEEN_START_Y = 50;
    private static final int QUEEN_SIZE = 200;
    private static int n;
    public PaintThread paintThread;
    private static QueenFrame frame = null;
    private TextField text;
    public TextField textUp;
    private Button buttonStart;
    private Button buttonStop;
    private Button buttonEnd;
    private Button buttonContinue;
    private Queen queen = null;
    boolean stop ;

    public static void main(String[] args) {
	frame = new QueenFrame();
	frame.init();

    }

    public void init() {
	// paintThread = new PaintThread(frame,queen);
	this.setLocation(FRAME_START_X, FRAME_START_Y);
	this.setSize(FRAME_SIZE, FRAME_SIZE);
	this.setTitle("N皇后问题");
	this.setBackground(Color.GREEN);
	this.setLayout(new BorderLayout());

	Panel panelUp = new Panel();
	panelUp.setSize(PANEL_SIZE, PANEL_SIZE);
	panelUp.setBackground(Color.BLUE);
	panelUp.setLayout(new FlowLayout());
	this.add(panelUp, BorderLayout.NORTH);
	
	
	textUp = new TextField(18);
	panelUp.add(textUp);
	
	Panel panelDown = new Panel();
	panelDown.setSize(PANEL_SIZE, PANEL_SIZE);
	panelDown.setBackground(Color.BLUE);
	panelDown.setLayout(new FlowLayout());
	this.add(panelDown, BorderLayout.SOUTH);

	buttonStart = new Button("Start");
	panelDown.add(buttonStart);
	buttonStart.addActionListener(this);

	Label label = new Label("Queen number:");
	label.setBackground(Color.WHITE);
	panelDown.add(label);

	text = new TextField();
	panelDown.add(text);
	text.addTextListener(new TextListener() {
	    public void textValueChanged(TextEvent e) {
		int s=0;
		if(text.getText().equals(""))
		    s = 0;
		else
		    s=Integer.parseInt(text.getText());
		QueenFrame.n = s;
	    }
	});

	buttonStop = new Button("Stop");
	panelDown.add(buttonStop);
	buttonStop.addActionListener(this);
	
	buttonContinue = new Button("Continue");
	panelDown.add(buttonContinue);
	buttonContinue.addActionListener(this);
	
	buttonEnd = new Button("End");
	panelDown.add(buttonEnd);
	buttonEnd.addActionListener(this);

	this.setResizable(true);
	this.setVisible(true);
	this.addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
		System.exit(0);
	    }
	});
    }

    public void actionPerformed(ActionEvent e) {
	if (e.getSource()==buttonStart) {
	    repaint();
	    textUp.setText("");
	    paintThread = frame.new PaintThread();
	    run();
	    draw(getGraphics());
	    paintThread.start();	    
	}
	if (e.getSource()==buttonStop) 
	   paintThread.suspend();
	
	   // paintThread.stop();
	if (e.getSource()==buttonContinue)
	    paintThread.resume();
	
	    
	if (e.getSource()==buttonEnd) 
	    paintThread.stop();;
	
	
    }

    public void paint(Graphics g) {
	if (paintThread!=null&& n!=0) {
	    draw(g);
	   
	}
    }

    public void draw(Graphics g) {
	Color c = g.getColor();
	g.setColor(Color.BLUE);
	System.out.println(QueenFrame.n);
	for (int i = 1; i<=n; i++)
	    for (int j = 1; j<=n; j++)
		g.drawRect(QUEEN_START_X+QUEEN_SIZE/n*i,QUEEN_START_Y+QUEEN_SIZE/n*j, QUEEN_SIZE/n, QUEEN_SIZE/n);
	g.setColor(c);
    }

    public void run() {
	int[] p = new int[n+1];
	for (int i = 0; i<=n; i++)
	    p[i] = 0;
	queen = new Queen(n, p, 0, this);
    }

    public class PaintThread extends Thread {
	private long TIME = 300;
	public void run() {
	    queen.drawBackTrace(getGraphics());
	}
	
	public void pause(){
	    try {
		Thread.sleep(TIME);
	    } catch (InterruptedException e) {
		e.printStackTrace();
	    }
	}
    }
}

⌨️ 快捷键说明

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