Advertisement
PZenith

CrystalView

Jan 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.BufferedImage;
  5.  
  6. public class CrystalView extends JPanel{
  7.     CrystalModel cm;
  8.     //private BufferedImage buffer;
  9.     private boolean run = true;
  10.     private int radius = 0;
  11.     Timer timer;
  12.    
  13.     public CrystalView(CrystalModel cm){
  14.         this.cm = cm;
  15.         radius = cm.getEscapeCircleRadius();
  16.         setPreferredSize(new Dimension(2*radius, 2*radius));
  17.         setBackground(Color.BLACK);
  18.         timer = new Timer(1,new ionCycle());
  19.         timer.start();
  20.     }
  21.    
  22.     public void paintComponent(Graphics g) {
  23.         super.paintComponent(g);
  24.         g.setColor(Color.RED);
  25.         for(int i = -radius; i < radius; i++){
  26.             for(int j = radius; j > -radius; j--){
  27.                 if(cm.getModelValue(i, j)){
  28.                     g.drawLine(cm.xBathToModel(i), cm.yBathToModel(j), cm.xBathToModel(i), cm.yBathToModel(j));
  29.                 }
  30.             }
  31.         }
  32.     }
  33.    
  34.     private class ionCycle implements ActionListener{
  35.         public void actionPerformed(ActionEvent e){
  36.             if(run){
  37.                 run = cm.runSomeSteps(50);
  38.                 repaint();
  39.             }else if(!run){
  40.                 timer.stop();
  41.             }
  42.         }
  43.     }
  44.     /**
  45.     public CrystalView(CrystalModel cm){
  46.         this.cm = cm;
  47.         size = 2*cm.getEscapeCircleRadius();
  48.         setPreferredSize(new Dimension(size, size));
  49.         setBackground(Color.BLACK);
  50.         this.buffer = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
  51.         setDoubleBuffered(false);
  52.         Timer timer = new Timer(1,new actions());
  53.         timer.start();
  54.     }
  55.     */
  56.     /**
  57.     public void paintComponent(Graphics g) {
  58.         super.paintComponent(g);
  59.         g.drawImage(buffer, 0, 0, this);
  60.     }
  61.     */
  62.     /**
  63.     private void drawOne(Graphics g){
  64.         if(run){
  65.             run = cm.crystallizeOneIon();
  66.         }
  67.         g.setColor(Color.RED);
  68.         g.drawLine(cm.getX()+cm.getEscapeCircleRadius()+4,cm.getEscapeCircleRadius()+4-cm.getY(),cm.getX()+cm.getEscapeCircleRadius()+4,cm.getEscapeCircleRadius()+4-cm.getY());
  69.         //              ^Converts the bath coordinates to JPanel coordinates^
  70.         repaint();
  71.     }
  72.     */
  73.     /**
  74.     private class actions implements ActionListener{
  75.         public void actionPerformed(ActionEvent e){
  76.             drawOne(buffer.getGraphics());
  77.         }
  78.     }
  79.     */
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement