public class TidTaker { private long startTime; private long stopTime; private long usedTime; private int accuracy; public static final int MEASURE_MILLIS=1; public static final int MEASURE_NANOS=2; public TidTaker(int measureInterval){ startTime = 0; stopTime = 0; usedTime = 0; accuracy = measureInterval; } public void start()throws RuntimeException { if(accuracy==MEASURE_MILLIS){ startTime = System.currentTimeMillis(); } else if(accuracy==MEASURE_NANOS){ startTime = System.nanoTime(); } else throw new RuntimeException("Illegal measureInterval..."); } public long stop() throws RuntimeException { if(accuracy==MEASURE_MILLIS){ stopTime = System.currentTimeMillis(); } else if(accuracy==MEASURE_NANOS){ stopTime = System.nanoTime(); } else throw new RuntimeException("Illegal measureInteval"); usedTime = stopTime - startTime; return usedTime; } } import java.awt.Graphics; import java.awt.Point; public class Firkant { private Point p1; private Point p2; private Point p3; private Point p4; private static final int ANTALL_SIDER = 4; public Firkant(Point startP1,Point startP2,Point startP3,Point startP4){ p1 = startP1; p2 = startP2; p3 = startP3; p4 = startP4; } public Point getP1(){ return p1; } public Point getP2(){ return p2; } public Point getP3(){ return p3; } public Point getP4(){ return p4; } public void setP1(Point nytt){ p1 = nytt; } public void setP2(Point nytt){ p2 = nytt; } public void setP3(Point nytt){ p3 = nytt; } public void setP4(Point nytt){ p4 = nytt; } public void draw(Graphics g){ g.drawPolygon(this.xPunkter(),this.yPunkter(),ANTALL_SIDER); } private int[] xPunkter(){ int[] x = new int[4]; x[0] = (int)p1.getX(); x[1] = (int)p2.getX(); x[2] = (int)p3.getX(); x[3] = (int)p4.getX(); return x; } private int[] yPunkter(){ int[] y = new int[4]; y[0] = (int)p1.getY(); y[1] = (int)p2.getY(); y[2] = (int)p3.getY(); y[3] = (int)p4.getY(); return y; } } import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.text.NumberFormat; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.UIManager; import static javax.swing.JOptionPane.*; import javax.swing.UnsupportedLookAndFeelException; public class Main extends JFrame implements ActionListener, KeyListener { private Container rute; private static Trekant t; private static Firkant f; private JButton knappen; private JLabel label; private JTextField txtAntNivåer; private Tegning tegning; private static Toolkit min; private int teller=0; private JRadioButton radioTrekant; private JRadioButton radioFirkant; private static Dimension dimensjon; private static int høyde; private static int bredde; NumberFormat mittFormat = NumberFormat.getIntegerInstance(); private TidTaker klokke = new TidTaker(TidTaker.MEASURE_MILLIS); public static void main(String[] args){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); min = Toolkit.getDefaultToolkit(); dimensjon = min.getScreenSize(); høyde = (int) dimensjon.getHeight(); bredde = (int) dimensjon.getWidth(); t = new Trekant(new Point(0,høyde-100),new Point(bredde-20,høyde-100),new Point((bredde-20)/2,0)); f = new Firkant(new Point(bredde/4,høyde-100),new Point((bredde/4)+(høyde-100),høyde-100),new Point((bredde/4)+(høyde-100),0),new Point(bredde/4,0)); Main ramme = new Main(); ramme.setSize(bredde, høyde); ramme.setDefaultCloseOperation(EXIT_ON_CLOSE); ramme.setTitle("Sierpinsky generator"); ramme.opprettGUI(); ramme.setVisible(true); } catch(Exception e){ showMessageDialog(null,"Det skjedde en feil ved GUI lasting"); } } public void opprettGUI(){ rute = getContentPane(); rute.setLayout(new BorderLayout()); knappen = new JButton("Trykk på meg..."); knappen.addActionListener(this); radioTrekant = new JRadioButton("Tegn trekant"); radioTrekant.addActionListener(this); radioFirkant = new JRadioButton("Tegn firkant"); radioFirkant.addActionListener(this); label = new JLabel("Antall nivåer: "); txtAntNivåer = new JTextField(2); txtAntNivåer.addKeyListener(this); JPanel panelet = new JPanel(new FlowLayout()); panelet.add(radioTrekant); panelet.add(radioFirkant); panelet.add(label); panelet.add(txtAntNivåer); panelet.add(knappen); rute.add(panelet,"North"); tegning = new Tegning(); rute.add(tegning,"Center"); } public void sierpinskyFirkant(Firkant start, int antNivå){ if(antNivå==0){ return; } tegning.g = tegning.getGraphics(); tegning.g.setColor(Color.BLUE); teller++; tegning.g.clearRect(0,0,300,20); tegning.g.drawString("Iterasjon nr: "+mittFormat.format(teller),10,10); start.draw(tegning.g); //Firkant nede til venstre int nyX11 = (int)start.getP1().getX(); int nyY11 = (int)start.getP1().getY(); int nyX21 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY21 = (int)start.getP1().getY(); int nyX31 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY31 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX41 = (int)start.getP1().getX(); int nyY41 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); //Firkant nede midten int nyX12 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY12 = (int)start.getP1().getY(); int nyX22 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY22 = (int)start.getP1().getY(); int nyX32 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY32 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX42 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY42 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); //Firkant nede til høyre int nyX13 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY13 = (int)start.getP1().getY(); int nyX23 = (int)start.getP2().getX(); int nyY23 = (int)start.getP1().getY(); int nyX33 = (int)start.getP2().getX(); int nyY33 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX43 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY43 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); //Firkant til venstre int nyX14 = (int)start.getP1().getX(); int nyY14 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX24 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY24 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX34 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY34 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX44 = (int)start.getP1().getX(); int nyY44 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); //Firkant til høyre int nyX15 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY15 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX25 = (int)start.getP2().getX(); int nyY25 = (int)start.getP1().getY()-(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX35 = (int)start.getP2().getX(); int nyY35 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX45 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY45 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); //Firkant oppe til venstre int nyX16 = (int)start.getP1().getX(); int nyY16 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX26 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY26 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX36 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY36 = (int)start.getP4().getY(); int nyX46 = (int)start.getP1().getX(); int nyY46 = (int)start.getP4().getY(); //Firkant oppe midten int nyX17 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY17 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX27 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY27 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX37 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY37 = (int)start.getP4().getY(); int nyX47 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY47 = (int)start.getP4().getY(); //Firkant oppe til høyre int nyX18 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY18 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX28 = (int)start.getP2().getX(); int nyY28 = (int)start.getP1().getY()-2*(((int)start.getP1().getY()-(int)start.getP3().getY())/3); int nyX38 = (int)start.getP2().getX(); int nyY38 = (int)start.getP4().getY(); int nyX48 = (int)start.getP1().getX()+2*(((int)start.getP2().getX()-(int)start.getP1().getX())/3); int nyY48 = (int)start.getP4().getY(); sierpinskyFirkant(new Firkant(new Point(nyX11,nyY11),new Point(nyX21,nyY21),new Point(nyX31,nyY31),new Point(nyX41,nyY41)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX12,nyY12),new Point(nyX22,nyY22),new Point(nyX32,nyY32),new Point(nyX42,nyY42)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX13,nyY13),new Point(nyX23,nyY23),new Point(nyX33,nyY33),new Point(nyX43,nyY43)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX14,nyY14),new Point(nyX24,nyY24),new Point(nyX34,nyY34),new Point(nyX44,nyY44)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX15,nyY15),new Point(nyX25,nyY25),new Point(nyX35,nyY35),new Point(nyX45,nyY45)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX16,nyY16),new Point(nyX26,nyY26),new Point(nyX36,nyY36),new Point(nyX46,nyY46)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX17,nyY17),new Point(nyX27,nyY27),new Point(nyX37,nyY37),new Point(nyX47,nyY47)), antNivå-1); sierpinskyFirkant(new Firkant(new Point(nyX18,nyY18),new Point(nyX28,nyY28),new Point(nyX38,nyY38),new Point(nyX48,nyY48)), antNivå-1); } public void sierpinskyTrekant(Trekant start, int antNivå){ if(antNivå==0){ return; } tegning.g = tegning.getGraphics(); tegning.g.setColor(Color.BLUE); teller++; tegning.g.clearRect(0,0,300,20); tegning.g.drawString("Iterasjon nr: "+mittFormat.format(teller),10,10); start.draw(tegning.g); //Trekant nede til venstre //For å regne ut med trigonometri: //X= l sin vinkel //Y= l cos vinkel int nyX11 = (int)start.getP1().getX(); int nyY11 = (int)start.getP1().getY(); int nyX21 = (int)start.getP1().getX()+(((int)start.getP2().getX()-(int)start.getP1().getX())/2); int nyY21 = (int)start.getP1().getY(); int nyX31 = (int)start.getP1().getX()+((nyX21-((int)start.getP1().getX()))/2); int nyY31 = (int)start.getP3().getY()+(((int)start.getP1().getY()-(int)start.getP3().getY())/2); //Trekant nede til høyre int nyX12 = nyX21; int nyY12 = nyY21; int nyX22 = (int)start.getP2().getX(); int nyY22 = (int)start.getP2().getY(); int nyX32 = (int)start.getP2().getX()-((nyX21-(int)start.getP1().getX())/2); int nyY32 = (int)start.getP3().getY()+(((int)start.getP1().getY()-(int)start.getP3().getY())/2); //Trekant oppe int nyX13 = nyX31; int nyY13 = nyY31; int nyX23 = nyX32; int nyY23= nyY32; int nyX33 = (int)start.getP3().getX(); int nyY33 = (int)start.getP3().getY(); sierpinskyTrekant(new Trekant(new Point(nyX11,nyY11),new Point(nyX21,nyY21),new Point(nyX31,nyY31)),antNivå-1); sierpinskyTrekant(new Trekant(new Point(nyX12,nyY12),new Point(nyX22,nyY22),new Point(nyX32,nyY32)),antNivå-1); sierpinskyTrekant(new Trekant(new Point(nyX13,nyY13),new Point(nyX23,nyY23),new Point(nyX33,nyY33)),antNivå-1); } public void actionPerformed(ActionEvent e) { if(e.getSource()==radioTrekant){ radioFirkant.setSelected(false); } if(e.getSource()==radioFirkant){ radioTrekant.setSelected(false); } if(e.getSource()==knappen){ tegning.g = tegning.getGraphics(); tegning.g.clearRect(0,0,bredde,høyde); if(radioFirkant.isSelected()){ try{ int ant = Integer.parseInt(txtAntNivåer.getText()); if(ant<11){ teller=0; klokke.start(); sierpinskyFirkant(f,ant); tegning.g.drawString(formaterTid(klokke.stop()),10,20); } else{ showMessageDialog(null,"Dette er ingen Super maskin..."); } } catch(NumberFormatException ex){ showMessageDialog(null,"Vennligst kontroller at det er et tall i antall nivåer..."); } catch(Exception ex){ showMessageDialog(null,"Det har skjedd en uventet feil...."); } } else if(radioTrekant.isSelected()){ try{ int ant = Integer.parseInt(txtAntNivåer.getText()); if(ant<21){ teller=0; klokke.start(); sierpinskyTrekant(t, ant); tegning.g.drawString(formaterTid(klokke.stop()),10,20); } else{ showMessageDialog(null,"Dette er ingen Super maskin..."); } } catch(NumberFormatException ex){ showMessageDialog(null,"Vennligst kontroller at det er et tall i antall nivåer..."); } catch(Exception ex){ showMessageDialog(null,"Det har skjedd en uventet feil...."); } } else showMessageDialog(null,"Vennligst velg en type form...trekant || firkant..."); } } public String formaterTid(long tidIMillisekunder){ long timer=0; long min=0; long sek=0; timer=tidIMillisekunder/(1000*60*60); min=(tidIMillisekunder/(1000*60))-(timer*60); sek=(tidIMillisekunder/1000)-((timer*60)+(min*60)); return "Tid brukt: "+timer+" timer, "+min+" minutter, "+sek+" sekunder."; } public void keyTyped(KeyEvent e) { action(e); } public void keyPressed(KeyEvent e) { //action(e); } public void keyReleased(KeyEvent e) { //action(e); } public void action(KeyEvent e){ char c = e.getKeyChar(); if(c=='\n'){ tegning.g = tegning.getGraphics(); tegning.g.clearRect(0,0,bredde,høyde); if(radioFirkant.isSelected()){ try{ int ant = Integer.parseInt(txtAntNivåer.getText()); if(ant<11){ teller=0; klokke.start(); sierpinskyFirkant(f,ant); tegning.g.drawString(formaterTid(klokke.stop()),10,20); } else{ showMessageDialog(null,"Dette er ingen Super maskin..."); } } catch(NumberFormatException ex){ showMessageDialog(null,"Vennligst kontroller at det er et tall i antall nivåer..."); } catch(Exception ex){ showMessageDialog(null,"Det har skjedd en uventet feil...."); } } if(radioTrekant.isSelected()){ try{ int ant = Integer.parseInt(txtAntNivåer.getText()); if(ant<21){ teller=0; klokke.start(); sierpinskyTrekant(t,ant); tegning.g.drawString(formaterTid(klokke.stop()),10,20); } else{ showMessageDialog(null,"Dette er ingen Super maskin..."); } } catch(NumberFormatException ex){ showMessageDialog(null,"Vennligst kontroller at det er et tall i antall nivåer..."); } catch(Exception ex){ showMessageDialog(null,"Det har skjedd en uventet feil...."); } } } } } import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class Tegning extends JPanel { Graphics g; public void paintComponent(Graphics inng){ g = inng; super.paintComponent(g); g.setColor(Color.BLUE); } } import java.awt.Container; import java.awt.Graphics; import java.awt.Point; public class Trekant { private Point p1; private Point p2; private Point p3; private static final int ANTALL_SIDER = 3; public Trekant(Point startP1, Point startP2,Point startP3){ p1 = startP1; p2 = startP2; p3 = startP3; } public Point getP1(){ return p1; } public Point getP2(){ return p2; } public Point getP3(){ return p3; } public void setP1(Point nytt){ p1 = nytt; } public void setP2(Point nytt){ p2 = nytt; } public void setP3(Point nytt){ p3 = nytt; } public void draw(Graphics g){ g.drawPolygon(this.xPunkter(),this.yPunkter(),ANTALL_SIDER); } private int[] xPunkter(){ int[] x = new int[3]; x[0] = (int)p1.getX(); x[1] = (int)p2.getX(); x[2] = (int)p3.getX(); return x; } private int[] yPunkter(){ int[] y = new int[3]; y[0] = (int)p1.getY(); y[1] = (int)p2.getY(); y[2] = (int)p3.getY(); return y; } }