Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.geom.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import java.awt.image.*;
- import java.io.*;
- import javax.imageio.*;
- import java.net.URL;
- public class CircuitBuild extends JApplet implements ActionListener {
- Image image;
- public static URL url;
- /* public void Initialise()
- {
- try {
- url = new URL(getCodeBase(),"cell.png");
- } catch (IOException e){
- }
- }
- */
- public static void main(String [] args) {
- // try {
- // url = new URL(getCodeBase(), "cell.png");
- //} catch (IOException e){
- // }
- JFrame frame = new JFrame();
- frame.setTitle("Constructing a Circuit");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JApplet applet = new CircuitBuild();
- applet.init();
- frame.getContentPane().add(applet);
- frame.pack();
- frame.setVisible(true);
- }
- JavaDraw2DPanel panel = null;
- public void init() {
- JMenuBar mb = new JMenuBar();
- setJMenuBar(mb);
- JMenu menu = new JMenu("Components");
- mb.add(menu);
- JMenuItem mi = new JMenuItem("Voltmeter");
- mi.addActionListener(this);
- menu.add(mi);
- mi = new JMenuItem("Ammeter");
- mi.addActionListener(this);
- menu.add(mi);
- mi = new JMenuItem("Resistor");
- mi.addActionListener(this);
- menu.add(mi);
- mi = new JMenuItem("Switch");
- mi.addActionListener(this);
- menu.add(mi);
- mi = new JMenuItem("Wiring");
- mi.addActionListener(this);
- menu.add(mi);
- mi = new JMenuItem("Lightbulb");
- mi.addActionListener(this);
- menu.add(mi);
- mi = new JMenuItem("Cell");
- mi.addActionListener(this);
- menu.add(mi);
- panel = new JavaDraw2DPanel();
- getContentPane().add(panel);
- }
- public void actionPerformed(ActionEvent ev) {
- String command = ev.getActionCommand();
- if ("Wiring".equals(command)) {
- panel.shapeType = panel.LINE2D;
- }
- }
- class JavaDraw2DPanel extends JPanel implements MouseListener, MouseMotionListener {
- private Vector shapes = new Vector();
- static final int Voltmeter = 0;
- static final int Ammeter = 1;
- static final int Resistor = 2;
- static final int Switch = 3;
- static final int LINE2D = 4;
- static final int Lightbulb = 5;
- static final int Cel = 6;
- static final int POLYGON = 7;
- static final int GENERAL = 8;
- static final int AREA = 9;
- int shapeType = Voltmeter;
- // vector of input points
- Vector points = new Vector();
- int pointIndex = 0;
- Shape partialShape = null;
- Point p = null;
- //ImageIcon circuitIcon = new ImageIcon (url);
- //Image component = circuitIcon.getImage();
- public JavaDraw2DPanel() {
- super();
- setBackground(Color.white);
- setPreferredSize(new Dimension(640, 480));
- addMouseListener(this);
- addMouseMotionListener(this);
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D)g;
- g2.setStroke(new BasicStroke(3));
- g2.setColor(Color.blue);
- //g.drawImage(component, 10, 10, this);
- for (int i = 0; i < shapes.size(); i++) {
- Shape s = (Shape)shapes.get(i);
- g2.draw(s);
- }
- }
- public void mouseClicked(MouseEvent ev) {
- }
- public void mouseEntered(MouseEvent ev) {
- }
- public void mouseExited(MouseEvent ev) {
- }
- public void mousePressed(MouseEvent ev) {
- points.add(ev.getPoint());
- pointIndex++;
- p = null;
- }
- public void mouseReleased(MouseEvent ev) {
- Graphics g = getGraphics();
- Point p1 = (Point)(points.get(pointIndex-1));
- p = ev.getPoint();
- Shape s = null;
- switch (shapeType) {
- case LINE2D:
- s = new Line2D.Float(p1.x, p1.y, p.x, p.y);
- break;
- }
- if (s != null) {
- shapes.add(s);
- points.clear();
- pointIndex = 0;
- p = null;
- repaint();
- }
- }
- public void mouseMoved(MouseEvent ev) {
- }
- public void mouseDragged(MouseEvent ev) {
- Graphics2D g = (Graphics2D)getGraphics();
- g.setXORMode(Color.white);
- Point p1 = (Point)points.get(pointIndex-1);
- switch (shapeType) {
- case LINE2D:
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment