Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.awt.Color.white;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author janek
- */
- public class animacja extends javax.swing.JFrame implements ActionListener {
- PanelGraficzny panel;
- Prostokąt[] tp = new Prostokąt[10];
- Timer t;
- static boolean ruch = false;
- public animacja() {
- initComponents();
- setSize(500,500);
- setBackground( white);
- panel = new PanelGraficzny();
- add(panel);
- panel.setSize(600,400);
- panel.setBounds(20, 20, 450, 200);
- t = new Timer(10, this);
- t.start();
- for (int i = 0; i < 10; i++) {
- tp[i]=new Prostokąt(0, 0, 0);
- }}
- @Override
- public void actionPerformed(ActionEvent e) {
- panel.repaint();
- }
- class PanelGraficzny extends JPanel {
- protected void paintComponent(Graphics g) {
- g.clearRect(0, 0, getWidth(),getHeight());
- for (int i = 0; i < 10; i++) { tp[i].rysuj(g); }
- }
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- buttonGroup1 = new javax.swing.ButtonGroup();
- jRadioButton1 = new javax.swing.JRadioButton();
- jRadioButton2 = new javax.swing.JRadioButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- buttonGroup1.add(jRadioButton1);
- jRadioButton1.setText("start");
- jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jRadioButton1ActionPerformed(evt);
- }
- });
- buttonGroup1.add(jRadioButton2);
- jRadioButton2.setText("stop");
- jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jRadioButton2ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jRadioButton1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jRadioButton2)
- .addContainerGap(296, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(0, 277, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jRadioButton1)
- .addComponent(jRadioButton2)))
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
- ruch = false;
- // TODO add your handling code here:
- }//GEN-LAST:event_jRadioButton2ActionPerformed
- private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
- for (int i = 0; i < 10; i++) {
- int d = panel.getWidth() / 10;
- tp[i] = new Prostokąt(i * d, d, panel.getHeight()); tp[i].start();// nowe wątki
- }
- panel.repaint();
- ruch = true;// TODO add your handling code here:
- }//GEN-LAST:event_jRadioButton1ActionPerformed
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(animacja.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new animacja().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.ButtonGroup buttonGroup1;
- private javax.swing.JRadioButton jRadioButton1;
- private javax.swing.JRadioButton jRadioButton2;
- // End of variables declaration//GEN-END:variables
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement