📄 applicationwindow.java
字号:
package gizmoball_demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.Timer;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;
public class ApplicationWindow extends JFrame {
private static final long serialVersionUID = 3257563992905298229L;
protected AnimationWindow animationWindow;
private JFileChooser fc = new JFileChooser(".");
private JPanel leftJPanel = null;
private JPanel rightJPanel = null;
//private ApplicationEventListener appEveLis;
/**
* @effects Initializes the application window so that it contains
* a toolbar and an animation window.
*/
public ApplicationWindow() {
// Title bar
super("Gizmoball");
// respond to the window system asking us to quit
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Create the toolbar.
JToolBar toolBar = new JToolBar();
addButtons(toolBar);
leftJPanel = new JPanel();
addButtons2(leftJPanel);
rightJPanel = new JPanel();
addButtons3(rightJPanel);
//Create the animation area used for output.
animationWindow = new AnimationWindow();
// Put it in a scrollPane, (this makes a border)
JScrollPane scrollPane = new JScrollPane(animationWindow);
//appEveLis = new ApplicationEventListener();
//addMouseListener(appEveLis);
//addMouseMotionListener(appEveLis);
//addKeyListener(appEveLis);
//Lay out the content pane.
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(720, 530));
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(leftJPanel, BorderLayout.WEST);
contentPane.add(rightJPanel, BorderLayout.EAST);
contentPane.add(scrollPane, BorderLayout.CENTER);
setContentPane(contentPane);
}
/**
* @modifies toolBar
* @effects adds Run, Stop and Quit buttons to toolBar
* @param toolBar toolbar to add buttons to.
*/
protected void addButtons(JToolBar toolBar) {
final JButton savebutton = new JButton("Save");;
final JButton loadbutton = new JButton("Load");
final JButton runbutton = new JButton("Run");
final JButton stopbutton = new JButton("Stop");
final JButton quitbutton = new JButton("Quit");
savebutton.setToolTipText("Save map to a file");
savebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Save pressed");
int returnVal = fc.showSaveDialog(ApplicationWindow.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
System.out.println("保存文件: " + file.getName());
} else {
System.out.println("保存文件被用户取消!" );
}
}
});
toolBar.add(savebutton);
//Load the map file
loadbutton.setToolTipText("Load map from a file");
loadbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Load pressed");
int returnVal = fc.showOpenDialog(ApplicationWindow.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
System.out.println("打开文件: " + file.getName());
} else {
System.out.println("打开文件被用户取消!");
}
}
});
toolBar.add(loadbutton);
//Run the game
runbutton.setToolTipText("Start the animation");
// when this button is pushed it calls animationWindow.setMode(true)
runbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
animationWindow.setMode(true);
leftJPanel.setVisible(false);
rightJPanel.setVisible(false);
savebutton.setEnabled(false);
loadbutton.setEnabled(false);
}
});
toolBar.add(runbutton);
//Stop the game
stopbutton.setToolTipText("Stop the animation");
// when this button is pushed it calls animationWindow.setMode(false)
stopbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
animationWindow.setMode(false);
leftJPanel.setVisible(true);
rightJPanel.setVisible(true);
savebutton.setEnabled(true);
loadbutton.setEnabled(true);
}
});
toolBar.add(stopbutton);
//Quit the game, that is close the game window
quitbutton.setToolTipText("Quit the game");
quitbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
toolBar.add(quitbutton);
}
protected void addButtons2(JPanel jPanel) {
JButton button1 = new JButton("Circle");
button1.setToolTipText("Add a circular bumper");
// when this button is pushed it create a circular bumper in the playing area
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
animationWindow.addC();//添加一个圆形障碍物
animationWindow.repaint();//更新绘图区域
}
});
JButton button2 = new JButton("Square");
button2.setToolTipText("Add a square bumper");
// when this button is pushed it create a square bumper in the playing area
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("square added");//test the listener
animationWindow.addS();//增加一个方形障碍物
animationWindow.repaint();//更新绘图区域
}
});
JButton button3 = new JButton("Triangle");
button3.setToolTipText("Add a triangular bumper");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Triangle added");
}
});
JButton button4 = new JButton("Left Flipper");
button4.setToolTipText("Add left flipper");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Left Flipper added");
}
});
JButton button5 = new JButton("Right Flipper");
button5.setToolTipText("Add right flipper");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Right Flipper added");
}
});
org.jdesktop.layout.GroupLayout jPanelLayout = new org.jdesktop.layout.GroupLayout(jPanel);
jPanel.setLayout(jPanelLayout);
jPanelLayout.setHorizontalGroup(
jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(button1)
.add(button2)
.add(button3)
.add(button4)
.add(button5)
);
jPanelLayout.setVerticalGroup(
jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanelLayout.createSequentialGroup()
.addContainerGap()
.add(button1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.addContainerGap(149, Short.MAX_VALUE))
);
}
protected void addButtons3(JPanel jPanel) {
JButton button1 = new JButton("Move");
button1.setToolTipText("Move the item you choose");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Move pressed");
}
});
JButton button2 = new JButton("Rotate");
button2.setToolTipText("Rotate the item you choose");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Rotate pressed");
}
});
JButton button3 = new JButton("Delete");
button3.setToolTipText("Delete the item you choose");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Triangle added");
}
});
JButton button4 = new JButton("Connect");
button4.setToolTipText("Conncet the item to a flipper");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Connect pressed");
}
});
org.jdesktop.layout.GroupLayout jPanelLayout = new org.jdesktop.layout.GroupLayout(jPanel);
jPanel.setLayout(jPanelLayout);
jPanelLayout.setHorizontalGroup(
jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(button1)
.add(button2)
.add(button3)
.add(button4)
);
jPanelLayout.setVerticalGroup(
jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanelLayout.createSequentialGroup()
.addContainerGap()
.add(button1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(button4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.addContainerGap(149, Short.MAX_VALUE))
);
}
//class ApplicationEventListener extends MouseAdapter implements MouseMotionListener, KeyListener, ActionListener {
//@Override public void mouseClicked(MouseEvent e) {
// System.out.println("hello");
// }
/**
* MouseMotionListener interface
* Override this method to act on mouse drag events.
* @param e Detected MouseEvent
*/
//public void mouseDragged(MouseEvent e) {
//}
/**
* MouseMotionListener interface
* Override this method to act on mouse move events.
* @param e Detected MouseEvent
*/
//public void mouseMoved(MouseEvent e) {
//}
/**
* We implement the KeyListener interface so that we can bump the ball in a
* random direction if keys A-J is presse.
* @modifies the ball that this listener owns
* @effects causes the ball to be bumped in a random direction but
* only if one of the keys A-J is pressed.
* @param e Detected Key Press Event
*/
// public void keyPressed(KeyEvent e) {
//
//int keynum = e.getKeyCode();
//if ((keynum >= 65) && (keynum <= 74)) {
//System.out.println("keypress " + e.getKeyCode());
//ball.randomBump();
//}
//}
/**
* Do nothing.
* @param e Detected Key Released Event
*/
//public void keyReleased(KeyEvent e) {
//}
/**
* Do nothing.
* @param e Detected Key Typed Event
*/
//public void keyTyped(KeyEvent e) {
//}
/**
* This is the callback for the timer
* @param e ActionEvent generated by timer
*/
//public void actionPerformed(ActionEvent e) {
//update();
//}
//}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -