import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import javax.imageio.ImageTypeSpecifier;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.tree.DefaultTreeCellEditor.EditorContainer;
public class Demo3 extends JFrame {
List<Line> lines = new ArrayList<>();
private MyJpanel jp = new MyJpanel();
private JMenuBar bar = new JMenuBar();
private JMenuItem filenew = new JMenuItem("New");
private JMenuItem fileopen = new JMenuItem("Open File");
private JMenuItem fileexit = new JMenuItem("Exit");
private JMenuItem filesave = new JMenuItem("Save");
private JMenuItem fileimport = new JMenuItem("Import");
private JMenuItem filedel = new JMenuItem("Delete");
private JMenuItem filesea = new JMenuItem("Search...");
private JMenuItem filepro = new JMenuItem("Properties");
private JMenuItem filecl = new JMenuItem("clean...");
private JMenuItem filehp = new JMenuItem("Abot Paint Tools");
private JMenuItem filecut = new JMenuItem("Cut");
private JMenuItem filecopy = new JMenuItem("Copy");
private JMenuItem filepaste = new JMenuItem("Paste");
public Demo3() {
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu search = new JMenu("Search");
JMenu project = new JMenu("Project");
JMenu help = new JMenu("Help");
file.add(filenew);
file.add(fileopen);
file.add(filesave);
file.add(fileimport);
file.add(fileexit);
edit.add(filecut);
edit.add(filecopy);
edit.add(filepaste);
edit.add(filedel);
search.add(filesea);
project.add(filecl);
project.add(filepro);
help.add(filehp);
MouseAdapter adapter = new MouseAdapter() {
Line l;
@Override
public void mousePressed(MouseEvent arg0) {
l = new Line();
l.x1 = arg0.getX();
l.y1 = arg0.getY();
lines.add(l);
}
@Override
public void mouseDragged(MouseEvent arg0) {
l.x2 = arg0.getX();
l.y2 = arg0.getY();
jp.repaint();
}
};
bar.add(file);
bar.add(edit);
bar.add(search);
bar.add(project);
bar.add(help);
jp.add(bar);
jp.addMouseMotionListener(adapter);
jp.addMouseListener(adapter);
add(jp);
setTitle("画板"
+ " "
+ "醉了" + "↓");
setBounds(500, 200, 900, 600);
fileexit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int n = JOptionPane.showConfirmDialog(Demo3.this, "Sure to exit?", "Test Dialog",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION)
System.exit(0);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class Line {
int x1, x2, y1, y2;
}
class MyJpanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
for (Line l : lines) {
g.drawLine(l.x1, l.y1, l.x2, l.y2);
}
}
}
public static void main(String[] args) {
new Demo3();
}
}
SWUST--Java随性画板(仅能直线)
最新推荐文章于 2021-03-08 04:18:33 发布