Una forma sencilla y rápida de aprender JAVA, observando y deduciendo cómo se comporta el lenguaje a través de ejemplos prácticos.

Archivo del blog

domingo, 25 de octubre de 2020

Gráficos en 2D. Diseño 2

Código Java (Design2.java):

package design;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import javax.swing.Timer;

public class Design2 extends javax.swing.JFrame {

    Trazador t;

    public Design2() {
        initComponents();
        this.setLocationRelativeTo(null);

        t = new Trazador(
                jPanel1.getGraphics(),
                jPanel1.getWidth(),
                jPanel1.getHeight(),
                0);

        Timer timer = new Timer(100, (ActionEvent e) -> {
            int a = 0;
            int b = 0;
            t.salta(245, 300);
            while (true) {
                for (int i = 0; i < 4; i++) {
                    t.avanza(80);
                    t.gira(90);
                }
                t.gira(15);
                a++;
                if (a >= 390 / 15) {
                    t.avanza(50);
                    a = 0;
                    b++;
                    if (b >= 12) {
                        break;
                    }
                }
            }
        });
        timer.start();
        timer.setRepeats(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Design 2 - 2020");
        setMinimumSize(new java.awt.Dimension(450, 450));
        setPreferredSize(new java.awt.Dimension(300, 300));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 643, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 488, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1);

        getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>                        

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {                                     

    }                                    

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Design2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   

    private static class Trazador {

        double x, y, angulo;
        Graphics g;

        private Trazador(Graphics g, float x, float y, float angulo) {

            this.x = x;
            this.y = y;
            this.angulo = angulo * Math.PI / 180;
            this.g = g;

            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(0.3f));
            g2.setColor(Color.YELLOW);
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

        }

        public void gira(double angulo) {
            this.angulo += angulo * Math.PI / 180;
        }

        public void avanza(double distancia) {
            double x2 = x + distancia * Math.cos(angulo);
            double y2 = y - distancia * Math.sin(angulo);
            g.drawLine((int) x2, (int) y2, (int) x, (int) y);
            salta(x2, y2);
        }

        public void salta(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }
}

Resultado:



Con la tecnología de Blogger.