我试图让我的放大/缩小按钮在我的 GUI 上工作。我有 2 个(我有其他但不需要发布)类,动作监听器/按钮合二为一,还有缩放方法。
按下 zoomIn/zoomOut 按钮时,我无法调用缩放方法。
第 1 类:(我已经导入了 GUI,不想在这里占用空间)
@SuppressWarnings("serial")
public class PartyParkingGUIPanel extends JPanel implements ActionListener
{
private final int DEFAULT_WIDTH = 800;
private final int DEFAULT_HEIGHT = 600;
private final int DEFAULT_GRID_WIDTH = DEFAULT_HEIGHT - 50;
//panels
private JPanel mapPanel = new JPanel();
private JPanel inputPanel = new JPanel();
private JPanel setupPanel = new JPanel();
private JPanel statusPanel = new JPanel();
private JPanel setupButtonsPanel = new JPanel();
private JPanel setupTextFieldPanel = new JPanel();
private JPanel controlPanel = new JPanel();
//labels
private JLabel gridSizeLabel = new JLabel("grid size:");
private JLabel numCarsLabel = new JLabel("#of cars:");
private JLabel numParkingSpotsLabel = new JLabel("#parking spots:");
//buttons
private JButton randomGrid = new JButton("Random Grid");
private JButton readFile = new JButton("Read File");
private JButton resetGrid = new JButton("Reset Grid");
private JButton zoomIn = new JButton("Zoom In");
private JButton zoomOut = new JButton("Zoom Out");
private JButton start = new JButton("Start");
private JButton step = new JButton("Step");
private JButton pause = new JButton("Pause");
//text area and cityMap with scrollpane
private JTextArea status = new JTextArea("Enter grid size, #cars, #parking spots" +
" \nClick on random button to generate random cars and spots" +
" \nClick on reset grid to clear the grid",20, 20);
private CityMap cityMap;
private JScrollPane mapScroller = new JScrollPane(cityMap);
private JScrollPane statusScroller = new JScrollPane(status);
//text fields
private JTextField gridSizeField = new JTextField("10",12);
private JTextField numParkingSpotsField = new JTextField("0",12);
private JTextField numCarsField = new JTextField("0",12);
private int gridSize = ParkingSimulation.DEFAULT_GRID_SIZE;
private int numCars;
private int numSpots;
private ArrayList<Car> cars;
private ParkingSimulation simulator;
private boolean showPaths = true;
private int speedFactor = 1;
private boolean animate = false;
private int delay = 500; // milliseconds
/**
* Set up the panel.
*/
public PartyParkingGUIPanel() {
setPreferredSize(new Dimension(1200, 700));
simulator = new ParkingSimulation();
simulator.setGridSize(gridSize);
cars = simulator.getCars();
cityMap = new CityMap(ParkingSimulation.DEFAULT_GRID_SIZE, DEFAULT_GRID_WIDTH, simulator);
cityMap.setShowPaths(showPaths);
zoomIn.addActionListener(this);
zoomOut.addActionListener(this);
startAnimation();
}
....
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == zoomIn){
??
}
}
....
具有缩放方法的 2 类:
public class CityMap extends JPanel
{
private int gridSize;
private final int DEFAULT_GRID_WIDTH = 1024;
private int displaySize = DEFAULT_GRID_WIDTH;
private int blockSize;
private int carSize;
private int offset;
private int numBlocks;
private ParkingSimulation simulator;
private ArrayList<Car> cars;
private ArrayList<ParkingSpot> spots;
private boolean showPaths = false;
private boolean flag = true;
private final int PATH_THICKNESS = 2;
/**
* Draw a city grid.
* @param gridSize
* @param displaySize
*/
public CityMap(int gridSize, int displaySize, ParkingSimulation simulator)
{
super();
this.simulator = simulator;
this.cars = simulator.getCars();
this.spots = simulator.getSpots();
this.displaySize = displaySize;
this.gridSize = gridSize;
setParameters(gridSize);
setBackground(Color.white);
setPreferredSize(new Dimension(displaySize, displaySize));
setDoubleBuffered(true); //makes drawing smoother
}
/**
* Sets parameters for drawing the grid
* @param size
*/
private void setParameters(int size)
{
numBlocks = size;
blockSize = displaySize/(numBlocks + 1);
offset = blockSize;
carSize = blockSize/4;
}
/**
* Zoom by a factor (1 and higher)
*/
public void zoom(int factor) {
setParameters(gridSize/factor);
}