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 0

Código Java (Principal.java):

package octagono;

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 Principal extends javax.swing.JFrame {

   
Trazador t;

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

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

        Timer timer = new Timer(100, (ActionEvent e) -> {
            for (int i = 0; i < 8; i++) {
                t.gira(-45);
                for (int j = 0; j < 8; j++) {
                    t.avanza(45);
                    t.gira(45);
                }
            }
        });
        timer.start();
        timer.setRepeats(false);
    }

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Octogonal Shape");
        setMinimumSize(new java.awt.Dimension(300, 300));
        setPreferredSize(new java.awt.Dimension(300, 300));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridLayout());

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        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);

        pack();

    }                       


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
               
    private javax.swing.JPanel jPanel1;         

    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(2)); //Grosor pincel
            g2.setColor(Color.YELLOW); //Color
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);//Filtro de suavizado

        }

        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.