Browse Source

GUI und Canvas Entwurf, Anfang

krabs 8 năm trước cách đây
mục cha
commit
6e97e9c2df
2 tập tin đã thay đổi với 237 bổ sung0 xóa
  1. 161 0
      src/ui/view/GUI.java
  2. 76 0
      src/ui/view/MyCanvas.java

+ 161 - 0
src/ui/view/GUI.java

@@ -0,0 +1,161 @@
+package ui.view;
+
+import java.awt.EventQueue;
+
+import javax.swing.JFrame;
+import javax.swing.JMenuBar;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import javax.swing.JTree;
+import javax.swing.BoxLayout;
+import javax.swing.JSplitPane;
+import javax.swing.JScrollPane;
+import javax.swing.JTabbedPane;
+import javax.swing.JTable;
+import javax.swing.tree.DefaultTreeModel;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.JEditorPane;
+
+public class GUI {
+
+	private JFrame frame;
+	private final JMenuBar menuBar = new JMenuBar();
+	private final JMenu mnNewMenu = new JMenu("File");
+	private final JMenu mnNewMenu_1 = new JMenu("Edit");
+	private final JMenu mnNewMenu_2 = new JMenu("Options");
+	private final JMenu mnNewMenu_3 = new JMenu("View");
+	private final JMenu mnHelp = new JMenu("Help");
+	private final JMenuItem mntmOpen = new JMenuItem("Open");
+	private final JMenuItem mntmNew = new JMenuItem("New");
+	private final JMenuItem mntmSave = new JMenuItem("Save");
+	private final JSplitPane splitPane = new JSplitPane();
+	private final JSplitPane splitPane_1 = new JSplitPane();
+	private final JScrollPane scrollPane_1 = new JScrollPane();
+	private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
+	private final JScrollPane scrollPane_2 = new JScrollPane();
+	private final MyCanvas canvas = new MyCanvas();
+	private final JTree tree = new JTree();
+	private final JEditorPane dtrpnHereWillBe = new JEditorPane();
+
+	/**
+	 * Launch the application.
+	 */
+	public static void main(String[] args) {
+		
+		try {
+			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
+				| UnsupportedLookAndFeelException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
+		
+		EventQueue.invokeLater(new Runnable() {
+			public void run() {
+				try {
+					GUI window = new GUI();
+					window.frame.setVisible(true);
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
+		});
+	}
+
+	/**
+	 * Create the application.
+	 */
+	public GUI() {
+		initialize();
+	}
+
+	/**
+	 * Initialize the contents of the frame.
+	 */
+	private void initialize() {
+		frame = new JFrame();
+		frame.setBounds(100, 100, 805, 486);
+		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+		
+		frame.setJMenuBar(menuBar);
+		
+		menuBar.add(mnNewMenu);
+		
+		mnNewMenu.add(mntmNew);
+		
+		mnNewMenu.add(mntmOpen);
+		
+		mnNewMenu.add(mntmSave);
+		
+		menuBar.add(mnNewMenu_1);
+		
+		menuBar.add(mnNewMenu_2);
+		
+		menuBar.add(mnNewMenu_3);
+		
+		menuBar.add(mnHelp);
+		frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
+		
+		frame.getContentPane().add(splitPane);
+		
+		//Table
+		String[] columnNames = {"Producer",
+                "Units",
+                "Number"};
+
+		Object[][] data = {
+				{"TV", "20",
+						"1"},
+				{"PC", "30",
+						"2"},
+				{"Fridge", "40",
+						"1"}
+		};
+		JTable table = new JTable(data, columnNames);
+		table.setCellSelectionEnabled(true);
+		table.setColumnSelectionAllowed(true);
+		
+		splitPane.setRightComponent(splitPane_1);
+		splitPane.setDividerLocation(200);
+		
+		splitPane_1.setRightComponent(new JScrollPane(table));
+		splitPane_1.setDividerLocation(400);
+		
+		tabbedPane.addTab("Modeling", null, canvas, null);
+		tabbedPane.addTab("Simulation", null, scrollPane_2, null);
+		dtrpnHereWillBe.setText("Here will be the Simulation");
+		
+		scrollPane_2.setViewportView(dtrpnHereWillBe);
+		
+		splitPane_1.setLeftComponent(tabbedPane);
+		
+		splitPane.setLeftComponent(scrollPane_1);
+		tree.setModel(new DefaultTreeModel(
+			new DefaultMutableTreeNode("Components") {
+				{
+					DefaultMutableTreeNode node_1;
+					node_1 = new DefaultMutableTreeNode("PowerPlant");
+						node_1.add(new DefaultMutableTreeNode("Standart P.P"));
+						node_1.add(new DefaultMutableTreeNode("Power PowerPlant"));
+					add(node_1);
+					node_1 = new DefaultMutableTreeNode("Houses");
+						node_1.add(new DefaultMutableTreeNode("Hospital"));
+						node_1.add(new DefaultMutableTreeNode("Standart House"));
+						node_1.add(new DefaultMutableTreeNode("Castle"));
+						node_1.add(new DefaultMutableTreeNode("Arena"));
+					add(node_1);
+					node_1 = new DefaultMutableTreeNode("Cars");
+						node_1.add(new DefaultMutableTreeNode("Small Car"));
+						node_1.add(new DefaultMutableTreeNode("Medium  Car"));
+						node_1.add(new DefaultMutableTreeNode("Big Car"));
+						node_1.add(new DefaultMutableTreeNode("Invisible Car"));
+					add(node_1);
+				}
+			}
+		));
+		
+		scrollPane_1.setViewportView(tree);
+	}
+}

+ 76 - 0
src/ui/view/MyCanvas.java

@@ -0,0 +1,76 @@
+package ui.view;
+
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import javax.imageio.ImageIO;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.event.MenuDragMouseEvent;
+
+class MyCanvas extends JPanel implements MouseListener
+{
+	private Image img;      // Contains the image to draw on MyCanvas
+	private int x = 0;
+	private int y = 0;
+	
+    public MyCanvas()
+    {
+    	img = null;
+    	try {
+    		URL url = new URL("https://cdn4.iconfinder.com/data/icons/buildings-filled-1/60/house-home-building-construction-32.png");
+          
+    		img = ImageIO.read(url);
+    	} catch (IOException e) {
+    		System.out.println("Failed to load the Image!");
+    	}
+    	
+        this.addMouseListener(this);
+    }
+
+    public void paintComponent(Graphics g)
+    {
+        // Draws the image to the canvas
+        g.drawImage(img, x, y, null);
+    }
+
+	@Override
+	public void mouseClicked(MouseEvent e) {
+		// TODO Auto-generated method stub
+	}
+
+	@Override
+	public void mouseEntered(MouseEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void mouseExited(MouseEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void mousePressed(MouseEvent e) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void mouseReleased(MouseEvent e) {
+		x = e.getX();
+        y = e.getY();
+
+        Graphics g = img.getGraphics();
+        g.fillOval(x, y, 3, 3);
+        System.out.println("Draw!");
+		repaint();
+	}
+}