21
circuitworks

Circuitworks. First prototype: mouselistener draws triangles wherever clicked

  • View
    226

  • Download
    2

Embed Size (px)

Citation preview

circuitworks

First prototype: mouselistener draws triangles wherever clicked

Coordinates are recorded

2nd prototype: GUI with inverters & and-gates using mouselistener

Saving coordinates

3rd prototype: saving graph Read/writeObjectsimport java.io.*;

import java.awt.*;public class Circuit3{public static void main(String args[]){CircuitThingy z=new CircuitThingy();CircuitThingy m=new CircuitThingy();System.out.println("Color is"+z.getColor());z.setColor(Color.orange);try{FileOutputStream fos = new FileOutputStream("tmp.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(z);oos.close();FileInputStream fis=new FileInputStream("tmp.txt");ObjectInputStream objin=new ObjectInputStream(fis);m=(CircuitThingy)objin.readObject();objin.close();}catch(ClassNotFoundException cnfe) {}catch (IOException e){}int []r=m.getX();for(int j=0;j<r.length;j++)System.out.println("coordinate "+j+"="+r[j]);System.out.println("Color is"+m.getColor());}//main}

CircuitThingy objectimport java.io.*;import java.awt.*;

public class CircuitThingy implements Serializable{int []x;int[]y;Color p;public CircuitThingy(){x=new int[5];x[0]=0;x[1]=33;x[2]=44;x[3]=777;x[4]=901;y=new int[5];y[0]=10;y[1]=332;y[2]=404;y[3]=717;y[4]=9101;

p=Color.red;}

public void setColor(Color c){p=c;}public int[]getX(){return x;}public Color getColor(){return p;}}

4th prototype: mouseclick puts rectangles

4th prototype: Entering connections

Blackscreen log of rectangles and connections

4th prototype display of circuit with crude wiring

4th prototype: Processing graph using topological sort

Fields in Applet

Point rectangles[];Point input[],output[];int boxcount=-1;String connect="";Point start[],end[];int connections=-1;JButton calculate;

PicturePanel view;JButton b;Graph g=new Graph();

output

Prototypes have limited functionality

• I only processed a few kinds of gates (NAND and NOR – I think) and gate type was determined by odd-even count parity, not user input.

• No proper gate representation was provided• Wiring wasn’t easy or pretty• No undo/redo was provided• No proper debugging occurred• No proper display of circuit execution occurred.

Double buffering…an image on a JPanel

• I used two classes, a BufPanel (extends JPanel) and a JFrame.

• Basically, you get an image, X.

• Then get the graphics associated with that image, GX

• Whatever you write using GX goes on X.

• Then in your (usual) paint method you just call g.drawImage(X,…)

JFrameimport javax.swing.*;import java.awt.*;public class DoubleBuf extends JFrame{BufPanel p;

public static void main(String args[]){ DoubleBuf cd = new DoubleBuf (); }public DoubleBuf (){setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

System.out.println("init()");this.setBounds(0,0,800,600);Container c = getContentPane();

p=new BufPanel(); c.setLayout(new BorderLayout()); c.add(p, BorderLayout.CENTER);

this.setVisible(true);}}

JPanelimport javax.swing.JPanel;import java.awt.*;import java.awt.event.*;public class BufPanel extends JPanel{

int x,y;private int bufferWidth; private int bufferHeight; private Image bufferImage; private Graphics bufferGraphics;public BufPanel(){ super();}//cal super for panel public void paint(Graphics g){ // checks the buffersize with the current panelsize // or initialises the image with the first paint if(bufferWidth!=getSize().width || bufferHeight!=getSize().height || bufferImage==null || bufferGraphics==null) resetBuffer(); if(bufferGraphics!=null){

Here the buffered image is drawn

//this clears the offscreen image, not the onscreen one

bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight); //calls the paintbuffer method with//the offscreen graphics as a parampaintBuffer(bufferGraphics);//we finaly paint the offscreen image onto the onscreen

g.drawImage(bufferImage,0,0,this); } }

More of the panel stuffpublic void paintBuffer(Graphics g){ /// g is the offscreen graphics g.setColor(Color.red); g.fillOval(30,30,100,100); g.setColor(Color.blue); g.fillOval(50,50,50,50); g.setColor(Color.green); g.fillOval(60,60,25,25); } private void resetBuffer(){////////////////you can cut most of this // always keep track of the image size bufferWidth=getSize().width; bufferHeight=getSize().height; // clean up the previous image if(bufferGraphics!=null){ bufferGraphics.dispose(); bufferGraphics=null; } if(bufferImage!=null){ bufferImage.flush(); bufferImage=null; } System.gc(); // create the new image with the size of the panel….need this part!!!!!! bufferImage=createImage(bufferWidth,bufferHeight); bufferGraphics=bufferImage.getGraphics(); }}//class

Double buffering…an image on a JPanel…code in slide notes