1 / 21

circuitworks

circuitworks. First prototype: mouselistener draws triangles wherever clicked. Coordinates are recorded. 2 nd prototype: GUI with inverters & and-gates using mouselistener. Saving coordinates. 3 rd prototype: saving graph Read/writeObjects. import java.io.*; import java.awt.*;

meir
Download Presentation

circuitworks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. circuitworks

  2. First prototype: mouselistener draws triangles wherever clicked

  3. Coordinates are recorded

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

  5. Saving coordinates

  6. 3rd prototype: saving graph Read/writeObjects import 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 }

  7. CircuitThingy object import 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;} }

  8. 4th prototype: mouseclick puts rectangles

  9. 4th prototype: Entering connections

  10. Blackscreen log of rectangles and connections

  11. 4th prototype display of circuit with crude wiring

  12. 4th prototype: Processing graph using topological sort

  13. 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();

  14. output

  15. 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.

  16. 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,…)

  17. JFrame import 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); }}

  18. JPanel import 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){

  19. 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 param paintBuffer(bufferGraphics); //we finaly paint the offscreen image onto the onscreen g.drawImage(bufferImage,0,0,this); } }

  20. More of the panel stuff public 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

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

More Related