我正在开发一个程序,如果单击菜单项“显示网格线”,菜单栏下方的绘图区域将在网格上绘制网格线。
编辑:以下是程序的组件。我把对这个问题不重要的任何部分都变成了评论。
import java.awt.Color;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
//main initializer
public class lineFollower {
frameDesign frame;
//private static Image icon = new ImageIcon("images/icon copy.jpg").getImage();
public static void main(String[] args) {
//make new frame
JFrame frame = new frameDesign();
frame.setSize(1000,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocale(null);
frame.setTitle("Line Follower Program 1.0");
//frame.setIconImage(icon);
}
}
这是框架设计(也省略了部分):
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.*;
public class frameDesign extends JFrame {
//data fields
//JPanel DataPanel;
//ListPanel ListPanel;
DrawingPanel DrawingPanel;
grid gridbox;
menuPanel menu;
//ArrayList<Rectangle> Rectangles;
//ArrayList<Circle> Circles;
//ArrayList<Triangle> Triangles;
public int newvalue = 0;
public int[] griddim;
public int gridwidth, gridheight;
BorderLayout layout;
//combine data into frame
public frameDesign(){
//Rectangles = new ArrayList<Rectangle>();
//Circles = new ArrayList<Circle>();
//Triangles = new ArrayList<Triangle>();
//Layout: Border
BorderLayout layout = new BorderLayout();
setLayout(layout);
//Assemble the Menu
menu = new menuPanel();
//Assemble the List Panel which also includes the animation buttons
//ListPanel = new ListPanel();
//Create the gridbox
gridbox = new grid();
gridbox.setPreferredSize(new Dimension(200,200));
gridbox.setSize(500,200);
griddim = gridbox.getgriddim();
gridwidth = griddim[0];
gridheight = griddim[1];
//Data Panel consists of ListPanel, AnimatePanel, and DrawingPanel
//DataPanel = new JPanel(new FlowLayout());
//DataPanel.add(DrawingPanel= new DrawingPanel());
//DataPanel.add(ListPanel = new ListPanel());
//DataPanel.setSize(250,700);
//DataPanel.setBackground(Color.getHSBColor(150f, 100f, 100f));
//DataPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
add(gridbox, BorderLayout.CENTER);
add(menu, BorderLayout.NORTH);
//add(DataPanel, BorderLayout.EAST);
//getPoints pointfinder = new getPoints();
//pointgrabber pointfinder2 = new pointgrabber();
//gridbox.addMouseListener(pointfinder2);
//gridbox.addMouseMotionListener(pointfinder);
//ActionListeners for Menus
menu.ShowLine.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//show the grid
if (gridbox.isgridline()==true){
gridbox.gridline(false);
}
else if (gridbox.isgridline()==false){
gridbox.gridline(true);
}
}
});
}
下一堆代码用于创建网格:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
* This is the design for the grid
*/
public class grid extends JPanel {
//data fields
boolean isgridline;
private static int startX = 50;
private static int startY = 50;
private int intervalline;
private int[] griddim;
//gridPanel
grid(){
setBorder(BorderFactory.createLineBorder(Color.BLACK));
griddim = new int[2];
griddim[0] = 500;
griddim[1] = 500;
isgridline = false;
intervalline = 20;
}
//Paints grid
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
//GridBox
g.setColor(Color.BLACK);
g.drawRect(50, 50, griddim[0], griddim[1]);
g.setColor(Color.WHITE);
g.fillRect(50, 50, griddim[0], griddim[1]);
//draw grid lines
if (isgridline){
g.setColor(Color.getHSBColor(150, 150, 200));
//vertical lines
for (int v = 0; v <griddim[0]; v = v+intervalline){
g.drawLine(startX, 50,
startX, 50+griddim[1]);
startX = startX+intervalline;
}
//horizontal lines
for (int v=0; v<griddim[1]; v=v+intervalline){
g.drawLine(50, startY, 50+griddim[0], startY);
startY = startY+intervalline;
}
}
}
//set grid dimensions
public void setgridbox(int[] dim){
griddim=dim;
}
//get grid dimensions
public int[] getgriddim(){
return griddim;
}
//set the grid line true or false
public void gridline(boolean islines){
isgridline=islines;
repaint();
}
//get gridline true or false
public boolean isgridline(){
return isgridline;
}
//set grid line intervals
//public void setinterval(int interval){
//intervalline=interval;
//repaint();
//}
}
最后是菜单:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.event.MenuEvent;
/*
* Class just for the menu panel
*/
public class menuPanel extends JMenuBar {
JMenuItem New, Save, Open, Exit;
JMenu fileMenu, gridMenu;
JMenuItem GridDim, GridLine, ShowLine;
menuPanel(){
//Assemble file menu
fileMenu = new JMenu("File");
New = new JMenuItem("New");
Save = new JMenuItem("Save");
Open = new JMenuItem("Open");
Exit = new JMenuItem("Exit");
fileMenu.add(New);
fileMenu.add(Save);
fileMenu.add(Open);
fileMenu.addSeparator();
fileMenu.add(Exit);
add(fileMenu);
//Assemble grid menu
gridMenu = new JMenu("Grid");
GridDim = new JMenuItem("Dimension");
GridLine = new JMenuItem("Change Grid Line Interval");
ShowLine = new JMenuItem("Show Grid Lines");
gridMenu.add(GridDim);
gridMenu.add(GridLine);
gridMenu.add(ShowLine);
add(gridMenu);
}
}
单击grid下菜单栏中的“显示网格线”按钮时,打开网格线,关闭网格线一次。之后它停止工作......我该怎么做才能使网格线不止一次地出现和关闭?