Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import java.awt.image.*;
  7. import java.io.*;
  8. import javax.imageio.*;
  9. import java.net.URL;
  10.  
  11.  
  12. public class CircuitBuild extends JApplet implements ActionListener {
  13.  
  14. Image image;
  15. public static URL url;
  16.  
  17.  
  18.  
  19. /* public void Initialise()
  20. {
  21.  
  22.  
  23. try {
  24. url = new URL(getCodeBase(),"cell.png");
  25.  
  26. } catch (IOException e){
  27. }
  28. }
  29. */
  30.  
  31. public static void main(String [] args) {
  32.  
  33.  
  34. // try {
  35. // url = new URL(getCodeBase(), "cell.png");
  36. //} catch (IOException e){
  37. // }
  38.  
  39. JFrame frame = new JFrame();
  40. frame.setTitle("Constructing a Circuit");
  41. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. JApplet applet = new CircuitBuild();
  43. applet.init();
  44. frame.getContentPane().add(applet);
  45. frame.pack();
  46. frame.setVisible(true);
  47.  
  48. }
  49.  
  50.  
  51.  
  52. JavaDraw2DPanel panel = null;
  53.  
  54. public void init() {
  55. JMenuBar mb = new JMenuBar();
  56. setJMenuBar(mb);
  57. JMenu menu = new JMenu("Components");
  58. mb.add(menu);
  59. JMenuItem mi = new JMenuItem("Voltmeter");
  60. mi.addActionListener(this);
  61. menu.add(mi);
  62. mi = new JMenuItem("Ammeter");
  63. mi.addActionListener(this);
  64. menu.add(mi);
  65. mi = new JMenuItem("Resistor");
  66. mi.addActionListener(this);
  67. menu.add(mi);
  68. mi = new JMenuItem("Switch");
  69. mi.addActionListener(this);
  70. menu.add(mi);
  71. mi = new JMenuItem("Wiring");
  72. mi.addActionListener(this);
  73. menu.add(mi);
  74. mi = new JMenuItem("Lightbulb");
  75. mi.addActionListener(this);
  76. menu.add(mi);
  77. mi = new JMenuItem("Cell");
  78. mi.addActionListener(this);
  79. menu.add(mi);
  80. panel = new JavaDraw2DPanel();
  81. getContentPane().add(panel);
  82. }
  83.  
  84. public void actionPerformed(ActionEvent ev) {
  85. String command = ev.getActionCommand();
  86. if ("Wiring".equals(command)) {
  87. panel.shapeType = panel.LINE2D;
  88. }
  89.  
  90.  
  91. }
  92.  
  93. class JavaDraw2DPanel extends JPanel implements MouseListener, MouseMotionListener {
  94. private Vector shapes = new Vector();
  95. static final int Voltmeter = 0;
  96. static final int Ammeter = 1;
  97. static final int Resistor = 2;
  98. static final int Switch = 3;
  99. static final int LINE2D = 4;
  100. static final int Lightbulb = 5;
  101. static final int Cel = 6;
  102. static final int POLYGON = 7;
  103. static final int GENERAL = 8;
  104. static final int AREA = 9;
  105.  
  106. int shapeType = Voltmeter;
  107. // vector of input points
  108. Vector points = new Vector();
  109. int pointIndex = 0;
  110. Shape partialShape = null;
  111. Point p = null;
  112.  
  113.  
  114. //ImageIcon circuitIcon = new ImageIcon (url);
  115. //Image component = circuitIcon.getImage();
  116.  
  117.  
  118. public JavaDraw2DPanel() {
  119. super();
  120. setBackground(Color.white);
  121. setPreferredSize(new Dimension(640, 480));
  122. addMouseListener(this);
  123. addMouseMotionListener(this);
  124.  
  125.  
  126.  
  127. }
  128.  
  129. public void paintComponent(Graphics g) {
  130. super.paintComponent(g);
  131. Graphics2D g2 = (Graphics2D)g;
  132. g2.setStroke(new BasicStroke(3));
  133. g2.setColor(Color.blue);
  134.  
  135.  
  136.  
  137. //g.drawImage(component, 10, 10, this);
  138.  
  139.  
  140. for (int i = 0; i < shapes.size(); i++) {
  141.  
  142. Shape s = (Shape)shapes.get(i);
  143.  
  144. g2.draw(s);
  145.  
  146.  
  147.  
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. }
  155.  
  156.  
  157. public void mouseClicked(MouseEvent ev) {
  158. }
  159.  
  160. public void mouseEntered(MouseEvent ev) {
  161. }
  162.  
  163. public void mouseExited(MouseEvent ev) {
  164. }
  165.  
  166. public void mousePressed(MouseEvent ev) {
  167. points.add(ev.getPoint());
  168. pointIndex++;
  169. p = null;
  170. }
  171.  
  172. public void mouseReleased(MouseEvent ev) {
  173. Graphics g = getGraphics();
  174. Point p1 = (Point)(points.get(pointIndex-1));
  175. p = ev.getPoint();
  176. Shape s = null;
  177. switch (shapeType) {
  178. case LINE2D:
  179. s = new Line2D.Float(p1.x, p1.y, p.x, p.y);
  180. break;
  181.  
  182. }
  183. if (s != null) {
  184. shapes.add(s);
  185. points.clear();
  186. pointIndex = 0;
  187. p = null;
  188. repaint();
  189. }
  190. }
  191.  
  192. public void mouseMoved(MouseEvent ev) {
  193. }
  194.  
  195. public void mouseDragged(MouseEvent ev) {
  196. Graphics2D g = (Graphics2D)getGraphics();
  197. g.setXORMode(Color.white);
  198. Point p1 = (Point)points.get(pointIndex-1);
  199. switch (shapeType) {
  200. case LINE2D:
  201. break;
  202. }
  203. }
  204. }
  205. }
Add Comment
Please, Sign In to add comment