Tentando animar um JPainel

1 resposta
P

Boa tarde pessoal,

eu tenho que fazer um simulador de pipeline como trabalho de uma disciplina da universidade.
Como sempre o professor não deu quase prazo nenhum pra gente entregar e como eu estou
com muitos outros trabalhos para fazer, não terei tempo de aprender a mexer com o Graphics 2D
de Java. Sendo assim estou tentando somente fazer um painel passar pela tela. Vou postar meu
código todo aqui atual:

package com.retotorto.pipesl;

import java.awt.Color;

public class PipeSL {

	private JFrame frmPipesl;
	private JTextField tfCaminho;
	private ArrayList<String> loading;
	private JPanel pDI, pER, pMEM, pBI, pEX, pSimulator;
	private Runnable ani;
	private Thread anim;
	private DefaultListModel<String> modelo;
	private JLabel lblInst1, lblInst2, lblInst3, lblInst4, lblInst5;
	private JList<String> listInstructions;
	private JLabel lblClockPipe;
	private JPanel pnInstructions, pn;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PipeSL window = new PipeSL();
					window.frmPipesl.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public PipeSL() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmPipesl = new JFrame();
		frmPipesl.getContentPane().setBackground(Color.WHITE);
		frmPipesl.setBackground(Color.WHITE);
		frmPipesl.setResizable(false);
		frmPipesl.setTitle("PipeSL :: Pipeline Simulator to Learning");
		frmPipesl.setBounds(100, 100, 858, 628);
		frmPipesl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmPipesl.getContentPane().setLayout(null);
		
		modelo = new DefaultListModel<String>();
		
		pSimulator = new JPanel();
		pSimulator.setBackground(Color.WHITE);
		pSimulator.setBorder(new TitledBorder(new LineBorder(new Color(128, 128, 128)), "Simulation", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		pSimulator.setBounds(14, 284, 821, 222);
		frmPipesl.getContentPane().add(pSimulator);
		pSimulator.setLayout(null);
		
		pBI = new JPanel();
		pBI.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
		pBI.setBounds(29, 52, 128, 145);
		pSimulator.add(pBI);
		pBI.setBackground(Color.BLACK);
		pBI.setLayout(null);
		
		JLabel lblBi = new JLabel("BI");
		lblBi.setForeground(Color.WHITE);
		lblBi.setFont(new Font("Kristen ITC", Font.BOLD, 16));
		lblBi.setBounds(51, 65, 26, 14);
		pBI.add(lblBi);
		
		pDI = new JPanel();
		pDI.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
		pDI.setBounds(186, 52, 128, 145);
		pSimulator.add(pDI);
		pDI.setBackground(Color.BLACK);
		pDI.setLayout(null);
		
		JLabel lblDi = new JLabel("DI");
		lblDi.setForeground(Color.WHITE);
		lblDi.setFont(new Font("Kristen ITC", Font.BOLD, 16));
		lblDi.setBounds(51, 65, 26, 14);
		pDI.add(lblDi);
		
		pEX = new JPanel();
		pEX.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
		pEX.setBounds(343, 52, 128, 145);
		pSimulator.add(pEX);
		pEX.setBackground(Color.BLACK);
		pEX.setLayout(null);
		
		JLabel lblEx = new JLabel("EX");
		lblEx.setForeground(Color.WHITE);
		lblEx.setFont(new Font("Kristen ITC", Font.BOLD, 16));
		lblEx.setBounds(51, 65, 26, 14);
		pEX.add(lblEx);
		
		pMEM = new JPanel();
		pMEM.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
		pMEM.setBounds(500, 52, 128, 145);
		pSimulator.add(pMEM);
		pMEM.setBackground(Color.BLACK);
		pMEM.setLayout(null);
		
		JLabel lblMem = new JLabel("MEM");
		lblMem.setForeground(Color.WHITE);
		lblMem.setFont(new Font("Kristen ITC", Font.BOLD, 16));
		lblMem.setBounds(36, 61, 56, 23);
		pMEM.add(lblMem);
		
		pER = new JPanel();
		pER.setBorder(new EtchedBorder(EtchedBorder.LOWERED, Color.BLACK, null));
		pER.setBounds(657, 52, 128, 145);
		pSimulator.add(pER);
		pER.setBackground(Color.BLACK);
		pER.setLayout(null);
		
		JLabel lblEr = new JLabel("ER");
		lblEr.setForeground(Color.WHITE);
		lblEr.setFont(new Font("Kristen ITC", Font.BOLD, 16));
		lblEr.setBounds(51, 65, 26, 14);
		pER.add(lblEr);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(14, 11, 821, 262);
		frmPipesl.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblOpenFile = new JLabel("Open File");
		lblOpenFile.setBounds(11, 37, 57, 14);
		panel.add(lblOpenFile);
		
		tfCaminho = new JTextField();
		tfCaminho.setEditable(false);
		tfCaminho.setBorder(new LineBorder(Color.GRAY));
		tfCaminho.setBounds(66, 34, 192, 20);
		panel.add(tfCaminho);
		tfCaminho.setColumns(10);
		
		final JButton btnNewButton = new JButton("...");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				setLoading(new ArrayList<String>());
				
				fc.updateUI();
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int returnVal = fc.showOpenDialog(btnNewButton);
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
		            File file = fc.getSelectedFile();
		            //This is where a real application would open the file.
		            tfCaminho.setText(file.getAbsolutePath());
		        }
				
				if(!tfCaminho.getText().equals("")){
					
					Reader reader = new Reader();
					reader.file(tfCaminho.getText(),modelo);
					
					int loc = 0;
					int i;
					pn = new JPanel();
					pn.setLayout(null);
					for(i = modelo.size()+8; i >= 0; i--){
						loc += 157;
					}
					pn.setBounds(-750, pnInstructions.getY(), loc, pnInstructions.getHeight());
					
					
					pn.setLocation(-pn.getWidth()+785, pnInstructions.getY());
					pSimulator.add(pn);
					
					//pn.setBackground(Color.WHITE);
					JLabel aux = new JLabel();
					loc = pn.getX()+2;
					
					for(i = 0; i < 4; i++){
						aux = new JLabel();
						aux.setText("NOP");
						aux.repaint();
						pn.add(aux);
						aux.setBounds(loc, lblInst1.getY(), lblInst1.getWidth(), lblInst1.getHeight());
						pn.repaint();
						loc+= 157;
					}
					
					for(i = modelo.size()-1; i >= 0; i--){
						aux = new JLabel();
						aux.setText(modelo.getElementAt(i));
						aux.repaint();
						pn.add(aux);
						aux.setBounds(loc, lblInst1.getY(), lblInst1.getWidth(), lblInst1.getHeight());
						pn.repaint();
						loc+= 157;
					}
					
					for(i = 0; i < 4; i++){
						aux = new JLabel();
						aux.setText("NOP");
						aux.repaint();
						pn.add(aux);
						aux.setBounds(loc, lblInst1.getY(), lblInst1.getWidth(), lblInst1.getHeight());
						pn.repaint();
						loc+= 157;
					}
					
				}
				
			}
		});
		btnNewButton.setBackground(Color.WHITE);
		btnNewButton.setBounds(268, 33, 43, 23);
		panel.add(btnNewButton);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(9, 63, 304, 175);
		panel.add(scrollPane);
		
		listInstructions = new JList<String>(modelo);
		scrollPane.setViewportView(listInstructions);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(128, 128, 128)), "Simulation Status", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLACK));
		panel_2.setBackground(Color.WHITE);
		panel_2.setBounds(329, 15, 484, 237);
		panel.add(panel_2);
		panel_2.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 21, 463, 107);
		panel_2.add(scrollPane_1);
		
		JList<String> listStatus = new JList<String>();
		scrollPane_1.setViewportView(listStatus);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(Color.GRAY));
		panel_1.setBounds(10, 133, 463, 91);
		panel_2.add(panel_1);
		panel_1.setBackground(Color.WHITE);
		panel_1.setLayout(null);
		
		final JLabel lblClockWPipe = new JLabel("Clock Cicles Without Pipeline:");
		lblClockWPipe.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblClockWPipe.setBounds(13, 11, 259, 14);
		panel_1.add(lblClockWPipe);
		
		lblClockPipe = new JLabel("Clock Cicles With Pipeline:");
		lblClockPipe.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblClockPipe.setBounds(13, 51, 259, 14);
		panel_1.add(lblClockPipe);
		
		final JLabel lblStalls = new JLabel("Stalls:");
		lblStalls.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblStalls.setBounds(282, 51, 98, 14);
		panel_1.add(lblStalls);
		
		final JLabel lblAdiatamento = new JLabel("Advances:");
		lblAdiatamento.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblAdiatamento.setBounds(282, 11, 117, 14);
		panel_1.add(lblAdiatamento);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(new LineBorder(new Color(128, 128, 128)), "Instructions", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_4.setBackground(Color.WHITE);
		panel_4.setBounds(-1, 15, 325, 237);
		panel.add(panel_4);
		panel_4.setLayout(null);
		
		JProgressBar progressBar = new JProgressBar();
		progressBar.setBounds(0, 566, 852, 14);
		progressBar.setVisible(false);
		frmPipesl.getContentPane().add(progressBar);
		
		final JSlider slVelocity = new JSlider();
		slVelocity.setBackground(Color.WHITE);
		slVelocity.setBounds(16, 518, 200, 23);
		frmPipesl.getContentPane().add(slVelocity);
		
		JLabel lblVelocityControl = new JLabel("Velocity Control");
		lblVelocityControl.setBounds(20, 542, 94, 14);
		frmPipesl.getContentPane().add(lblVelocityControl);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.WHITE);
		panel_3.setBounds(526, 511, 316, 38);
		frmPipesl.getContentPane().add(panel_3);
		panel_3.setLayout(null);
		
		JButton btnPlay = new JButton("Play");
		btnPlay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				if(anim.getState().equals("TIMED_WAITING")){
					anim.resume();
				}
				else if(modelo.size() > 0){
				slVelocity.setEnabled(false);
				lblClockWPipe.setText("Clock Cicles Without Pipeline: " + modelo.size()*5);
				
				ani = new Animation(pDI, pER, pMEM, pBI, pEX, 2000, pn);
				anim = new Thread(ani);
				anim.start();
				}
				else{
					JOptionPane.showMessageDialog(null, "To start a simulation you must upload a file of instructions", "Attention", JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		btnPlay.setBounds(12, 8, 89, 23);
		panel_3.add(btnPlay);
		btnPlay.setBackground(Color.WHITE);
		
		JButton btnPause = new JButton("Pause");
		btnPause.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					anim.suspend();
				
			}
		});
		btnPause.setBounds(113, 8, 89, 23);
		panel_3.add(btnPause);
		btnPause.setBackground(Color.WHITE);
		
		JButton btnStop = new JButton("Stop");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
					anim.stop();
				
					pBI.setBackground(Color.RED);
					pDI.setBackground(Color.RED);
					pEX.setBackground(Color.RED);
					pMEM.setBackground(Color.RED);
					pER.setBackground(Color.RED);
					
					
			}
		});
		btnStop.setBounds(214, 8, 89, 23);
		panel_3.add(btnStop);
		btnStop.setBackground(Color.WHITE);
		
		JLabel lblFast = new JLabel("Slow");
		lblFast.setBounds(186, 542, 46, 14);
		frmPipesl.getContentPane().add(lblFast);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBackground(Color.WHITE);
		frmPipesl.setJMenuBar(menuBar);
		
		JMenu mnEdit = new JMenu("File");
		mnEdit.setBackground(Color.WHITE);
		menuBar.add(mnEdit);
		
		JMenu mnNew = new JMenu("New");
		mnNew.setBackground(Color.WHITE);
		mnEdit.add(mnNew);
		
		JMenuItem mntmSimulation = new JMenuItem("Simulation");
		mntmSimulation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				modelo.removeAllElements();
				pBI.setBackground(Color.BLACK);
				pDI.setBackground(Color.BLACK);
				pEX.setBackground(Color.BLACK);
				pMEM.setBackground(Color.BLACK);
				pER.setBackground(Color.BLACK);
				lblClockWPipe.setText("Clock Cicles Without Pipeline:");
				lblClockPipe.setText("Clock Cicles With Pipeline:");
				lblStalls.setText("Stalls:");
				lblAdiatamento.setText("Advances:");	
				tfCaminho.setText("");
				slVelocity.setEnabled(true);
				lblInst1.setText("NOP");
				lblInst2.setText("NOP");
				lblInst3.setText("NOP");
				lblInst4.setText("NOP");
				lblInst5.setText("NOP");
				anim.stop();
			}
		});
		mntmSimulation.setBackground(Color.WHITE);
		mnNew.add(mntmSimulation);
		
		JMenuItem mntmOpenFile = new JMenuItem("Open File ...");
		mntmOpenFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				setLoading(new ArrayList<String>());
				
				fc.updateUI();
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int returnVal = fc.showOpenDialog(btnNewButton);
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
		            File file = fc.getSelectedFile();
		            //This is where a real application would open the file.
		            tfCaminho.setText(file.getAbsolutePath());
		        }
				
				if(!tfCaminho.getText().equals("")){
					
					Reader reader = new Reader();
					reader.file(tfCaminho.getText(),modelo);
				}
			}
		});
		mntmOpenFile.setBackground(Color.WHITE);
		mnEdit.add(mntmOpenFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mntmExit.setBackground(Color.WHITE);
		mnEdit.add(mntmExit);
		
		JMenu mnRun = new JMenu("Run");
		mnRun.setBackground(Color.WHITE);
		menuBar.add(mnRun);
		
		JMenuItem mntmRun = new JMenuItem("Play");
		mntmRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(anim.getState().equals("TIMED_WAITING")){
					anim.resume();
				}
				else if(modelo.size() > 0){
				slVelocity.setEnabled(false);
				lblClockWPipe.setText("Clock Cicles Without Pipeline: " + modelo.size()*5);
				

				ani = new Animation(pDI, pER, pMEM, pBI, pEX, slVelocity.getValue()*10, pn);
				anim = new Thread(ani);
				anim.start();
				}
				else{
					JOptionPane.showMessageDialog(null, "To start a simulation you must upload a file of instructions", "Attention", JOptionPane.ERROR_MESSAGE);
				};
			}
		});
		mntmRun.setBackground(Color.WHITE);
		mnRun.add(mntmRun);
		
		JMenuItem mntmPause = new JMenuItem("Pause");
		mntmPause.setBackground(Color.WHITE);
		mnRun.add(mntmPause);
		
		JMenuItem mntmStop = new JMenuItem("Stop");
		mntmStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				anim.stop();
				
				pBI.setBackground(Color.RED);
				pDI.setBackground(Color.RED);
				pEX.setBackground(Color.RED);
				pMEM.setBackground(Color.RED);
				pER.setBackground(Color.RED);
			}
		});
		mntmStop.setIcon(new ImageIcon(PipeSL.class.getResource("/org/eclipse/jface/action/images/stop.gif")));
		mntmStop.setBackground(Color.WHITE);
		mnRun.add(mntmStop);
		
		JMenu mnHelp = new JMenu("Help");
		mnHelp.setBackground(Color.WHITE);
		menuBar.add(mnHelp);
		
		JMenuItem mntmHelpContents = new JMenuItem("Help Contents");
		mntmHelpContents.setBackground(Color.WHITE);
		mnHelp.add(mntmHelpContents);
		
		JMenuItem mntmAboutPipesl = new JMenuItem("About PipeSL");
		mntmAboutPipesl.setBackground(Color.WHITE);
		mnHelp.add(mntmAboutPipesl);
		
		ani = new Animation(null, null, null, null, null, 0, null);
		
		pnInstructions = new JPanel();
		pnInstructions.setBounds(29, 16, 757, 29);
		pSimulator.add(pnInstructions);
		pnInstructions.setLayout(null);
		pnInstructions.setVisible(false);
		
		lblInst1 = new JLabel("NOP");
		lblInst1.setBounds(3, 8, 124, 14);
		pnInstructions.add(lblInst1);
		
		lblInst2 = new JLabel("NOP");
		lblInst2.setBounds(160, 8, 124, 14);
		pnInstructions.add(lblInst2);
		
		lblInst3 = new JLabel("NOP");
		lblInst3.setBounds(317, 8, 124, 14);
		pnInstructions.add(lblInst3);
		
		lblInst4 = new JLabel("NOP");
		lblInst4.setBounds(474, 8, 124, 14);
		pnInstructions.add(lblInst4);
		
		lblInst5 = new JLabel("NOP");
		lblInst5.setBounds(631, 8, 126, 14);
		pnInstructions.add(lblInst5);
		
		anim = new Thread(ani);
		
		
		
	}

	public ArrayList<String> getLoading() {
		return loading;
	}

	public void setLoading(ArrayList<String> loading) {
		this.loading = loading;
	}
	
	public DefaultListModel<String> getModelo() {
		return modelo;
	}

	public void setModelo(DefaultListModel<String> modelo) {
		this.modelo = modelo;
	}

	
}
package com.retotorto.pipesl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

import javax.swing.DefaultListModel;
  
public class Reader {  
  
    public void file(String path, DefaultListModel<String> modelo) {  
        String linha = null;  
        try {  
            // instancia do arquivo que vou ler  
            FileReader reader = new FileReader(path);  
            BufferedReader leitor = new BufferedReader(reader);  
  
            // loop que percorrerá todas as  linhas do arquivo.txt que eu quero ler  
            while ((linha = leitor.readLine()) != null) {  
                      //No metodo StringTokenizer passo os parametros que quero ler, em seguida o que eu quero descartar no meu caso ( - ) e ( ; ).   
                StringTokenizer st = new StringTokenizer(linha, ";" );  
                    // Aqui determino que enquanto existir tokens que ele faça a leitura  
                modelo.addElement(st.nextToken());  
            }  
  
            leitor.close();  
            reader.close();  
  
        } catch (Exception e) {  
            e.printStackTrace();  
  
        }  
    }
}
package com.retotorto.pipesl;

import javax.swing.JPanel;

public class Animation implements Runnable{
	
	private JPanel pDI, pER, pMEM, pBI, pEX, pn;
	private int time;
	private int pausedTime;
	
	
	public Animation(JPanel pDI, JPanel pER, JPanel pMEM, JPanel pBI, JPanel pEX, int time, JPanel pn){
		
		this.setpBI(pBI);
		this.setpDI(pDI);
		this.setpMEM(pMEM);
		this.setpER(pER);
		this.setpEX(pEX);
		this.time = time;
		this.pn = pn;
	}


	@Override
	public void run() {		
		
		for(int i = 0; i < 10; i++){
		
			pn.setLocation(pn.getX()+152, pn.getY());
			pn.repaint();
			try {
				Thread.sleep(getTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}			
		/*
		while(i <= modelo.size()){
			
			
			if(i >= modelo.size()){
				lblInst1.setText("NOP");
			}
			else	
			lblInst1.setText(modelo.getElementAt(i));
			
			
			lblClockPipe.setText("Clock Cicles With Pipeline: " + clock);
			clock++;
			
			pBI.setBackground(Color.GREEN);
			
			try {
				Thread.sleep(getTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(i+1 < modelo.size()){
				lblInst1.setText(modelo.getElementAt(i+1));
				ad++;
			}
			else
				lblInst1.setText("NOP");
			
			if(i >= modelo.size()){
				lblInst2.setText("NOP");
			}
			else
			lblInst2.setText(modelo.getElementAt(i));
			
			lblClockPipe.setText("Clock Cicles With Pipeline: " + clock);
			clock++;
			pDI.setBackground(Color.GREEN);
			try {
				Thread.sleep(getTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(i+2 < modelo.size()){
				lblInst1.setText(modelo.getElementAt(i+2));
				ad++;
			}
			else
				lblInst1.setText("NOP");
			
			if(i+1 < modelo.size())
				lblInst2.setText(modelo.getElementAt(i+1));
			else
				lblInst2.setText("NOP");
			
			if(i >= modelo.size()){
				lblInst3.setText("NOP");
			}
			else
			lblInst3.setText(modelo.getElementAt(i));
			
			lblClockPipe.setText("Clock Cicles With Pipeline: " + clock);
			clock++;
			pEX.setBackground(Color.GREEN);
			try {
				Thread.sleep(getTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			if(i+3 < modelo.size()){
				lblInst1.setText(modelo.getElementAt(i+3));
				ad++;
			}
			else
				lblInst1.setText("NOP");
			
			if(i+2 < modelo.size())
				lblInst2.setText(modelo.getElementAt(i+2));
			else
				lblInst2.setText("NOP");
			
			if(i+1 < modelo.size())
				lblInst3.setText(modelo.getElementAt(i+1));
			else
				lblInst3.setText("NOP");
			
			if(i >= modelo.size()){
				lblInst4.setText("NOP");
			}
			else	
			lblInst4.setText(modelo.getElementAt(i));
			
			lblClockPipe.setText("Clock Cicles With Pipeline: " + clock);
			clock++;
			pMEM.setBackground(Color.GREEN);
			try {
				Thread.sleep(getTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(i+4 < modelo.size()){
				lblInst5.setText(modelo.getElementAt(i+4));
				ad++;
			}
			else
				lblInst5.setText("NOP");
			
			if(i+3 < modelo.size())
				lblInst4.setText(modelo.getElementAt(i+3));
			else
				lblInst4.setText("NOP");
			
			if(i+2 < modelo.size())
				lblInst3.setText(modelo.getElementAt(i+2));
			else
				lblInst3.setText("NOP");
			
			if(i+1 < modelo.size())
				lblInst4.setText(modelo.getElementAt(i+1));
			else
				lblInst4.setText("NOP");
			
			if(i >= modelo.size()){
				lblInst5.setText("NOP");
			}
			else
			lblInst5.setText(modelo.getElementAt(i));
			
			lblClockPipe.setText("Clock Cicles With Pipeline: " + clock);
			clock++;
			pER.setBackground(Color.GREEN);
			try {
				Thread.sleep(getTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			listInstructions.setSelectedIndex(i);
			i+= ad;
			i++;
		}*/
		
	}
	
	public int getTime(){
		return time;
	}


	public int getPausedTime() {
		return pausedTime;
	}


	public void setPausedTime(int pausedTime) {
		this.pausedTime = pausedTime;
	}


	public JPanel getpMEM() {
		return pMEM;
	}


	public void setpMEM(JPanel pMEM) {
		this.pMEM = pMEM;
	}


	public JPanel getpER() {
		return pER;
	}


	public void setpER(JPanel pER) {
		this.pER = pER;
	}


	public JPanel getpDI() {
		return pDI;
	}


	public void setpDI(JPanel pDI) {
		this.pDI = pDI;
	}


	public JPanel getpEX() {
		return pEX;
	}


	public void setpEX(JPanel pEX) {
		this.pEX = pEX;
	}


	public JPanel getpBI() {
		return pBI;
	}


	public void setpBI(JPanel pBI) {
		this.pBI = pBI;
	}		
	
	
}

como vocês podem ver está bastante amador eu reconheço, mas preciso dos pontos e não tenho
tempo, como todo estudante universitário em fim de período.

Por isso gostaria da ajuda de vocês para somente fazer o loop que vai fazer com que as minhas instruções passem
sobre os blocos do Pipeline, veja na imagem abaixo:

[IMG]http://www.crazzy.com.br/upload/upload-img/pictures/f8213b8adc8e763f259c20aebacd9020.jpg[/IMG]

Eu já tentei de diversas maneiras mas não consigo enxergar.
O que está comentado foi a penúltima tentativa, mas não deu certo tbm.
o que estou tentando agora está nesta parte do código:

int loc = 0;
					int i;
					pn = new JPanel();
					pn.setLayout(null);
					for(i = modelo.size()+8; i >= 0; i--){
						loc += 157;
					}
					pn.setBounds(-750, pnInstructions.getY(), loc, pnInstructions.getHeight());
					
					
					pn.setLocation(-pn.getWidth()+785, pnInstructions.getY());
					pSimulator.add(pn);
					
					//pn.setBackground(Color.WHITE);
					JLabel aux = new JLabel();
					loc = pn.getX()+2;
					
					for(i = 0; i < 4; i++){
						aux = new JLabel();
						aux.setText("NOP");
						aux.repaint();
						pn.add(aux);
						aux.setBounds(loc, lblInst1.getY(), lblInst1.getWidth(), lblInst1.getHeight());
						pn.repaint();
						loc+= 157;
					}
					
					for(i = modelo.size()-1; i >= 0; i--){
						aux = new JLabel();
						aux.setText(modelo.getElementAt(i));
						aux.repaint();
						pn.add(aux);
						aux.setBounds(loc, lblInst1.getY(), lblInst1.getWidth(), lblInst1.getHeight());
						pn.repaint();
						loc+= 157;
					}
					
					for(i = 0; i < 4; i++){
						aux = new JLabel();
						aux.setText("NOP");
						aux.repaint();
						pn.add(aux);
						aux.setBounds(loc, lblInst1.getY(), lblInst1.getWidth(), lblInst1.getHeight());
						pn.repaint();
						loc+= 157;
					}

eu estou tentando carregar todas as intruções num painel e antes colocar NOP e depois NOP.
Posicionar esse painel para ficar com os NOP no começo e ir arrastando ele na tela até
os outros NOP do final fiquei parados em cima dos blocos. O problema é que eu estou inserindo
as instruções no painel e quando vou arrastá-lo pela tela, as instruções não estão lá.
Sei que é amadorismo para se eu conseguir fazer isso já é um caminho para conseguir
ganhar os pontos.

Se puderem dar um help até mesmo de como melhorar isso, eu agradeço.

Grato.
Pablo.

1 Resposta

vtr002

Se tivesse feito com Graphics2D ia poupar um moooonte de código, e sua vida ia ser bem mais simples. Além de ficar fazendo movimentos com Paineis vai ter que ficar atualizando a tela.

Criado 2 de abril de 2013
Ultima resposta 2 de abr. de 2013
Respostas 1
Participantes 2