Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.BufferedImage;
- public class CrystalView extends JPanel{
- CrystalModel cm;
- //private BufferedImage buffer;
- private boolean run = true;
- private int radius = 0;
- Timer timer;
- public CrystalView(CrystalModel cm){
- this.cm = cm;
- radius = cm.getEscapeCircleRadius();
- setPreferredSize(new Dimension(2*radius, 2*radius));
- setBackground(Color.BLACK);
- timer = new Timer(1,new ionCycle());
- timer.start();
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- for(int i = -radius; i < radius; i++){
- for(int j = radius; j > -radius; j--){
- if(cm.getModelValue(i, j)){
- g.drawLine(cm.xBathToModel(i), cm.yBathToModel(j), cm.xBathToModel(i), cm.yBathToModel(j));
- }
- }
- }
- }
- private class ionCycle implements ActionListener{
- public void actionPerformed(ActionEvent e){
- if(run){
- run = cm.runSomeSteps(50);
- repaint();
- }else if(!run){
- timer.stop();
- }
- }
- }
- /**
- public CrystalView(CrystalModel cm){
- this.cm = cm;
- size = 2*cm.getEscapeCircleRadius();
- setPreferredSize(new Dimension(size, size));
- setBackground(Color.BLACK);
- this.buffer = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
- setDoubleBuffered(false);
- Timer timer = new Timer(1,new actions());
- timer.start();
- }
- */
- /**
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(buffer, 0, 0, this);
- }
- */
- /**
- private void drawOne(Graphics g){
- if(run){
- run = cm.crystallizeOneIon();
- }
- g.setColor(Color.RED);
- g.drawLine(cm.getX()+cm.getEscapeCircleRadius()+4,cm.getEscapeCircleRadius()+4-cm.getY(),cm.getX()+cm.getEscapeCircleRadius()+4,cm.getEscapeCircleRadius()+4-cm.getY());
- // ^Converts the bath coordinates to JPanel coordinates^
- repaint();
- }
- */
- /**
- private class actions implements ActionListener{
- public void actionPerformed(ActionEvent e){
- drawOne(buffer.getGraphics());
- }
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement