Orizzonte artificiale

OrizzonteArtificiale

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class JHorizon extends JPanel {

    private int h = 0, a = 0;

    public JHorizon() {
        setMinimumSize(new Dimension(100, 100));
        setPreferredSize(new Dimension(100, 100));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        double m = Math.tan(Math.toRadians(a));
        int y1 = (int) (h - m * 50), y2 = (int) (h + m * 50);
        int[] x = new int[4], y = new int[4];

        // parte inferiore verde
        x[0] = 0;   y[0] = 50 - y1;
        x[1] = 100; y[1] = 50 - y2;
        x[2] = 100; y[2] = 100;
        x[3] = 0;   y[3] = 100;
        g.setColor(Color.GREEN);
        g.fillPolygon(x, y, 4);

        // parte superiore azzurra
        x[0] = 0;   y[0] = 50 - y1;
        x[1] = 100; y[1] = 50 - y2;
        x[2] = 100; y[2] = 0;
        x[3] = 0;   y[3] = 0;
        g.setColor(Color.CYAN);
        g.fillPolygon(x, y, 4);

        // assi, gradazioni e cerchio
        g.setColor(Color.BLACK);
        g.drawLine(0, 50, 100, 50);
        g.drawLine(50, 0, 50, 100);
        for (int i = 10; i < 100; i += 10)
            g.drawLine(45, i, 55, i);
        g.drawOval(0, 0, 100, 100);
        
        g.drawString(a + "°", 1, 10);
        g.drawString(h + "", 82, 10);
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
        repaint();
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
        repaint();
    }

}

Associare nel Main a jHorizon il gestore di evento MouseMoved:

    private void jHorizonMouseMoved(java.awt.event.MouseEvent evt) {                                    
        int x = evt.getX(), y = evt.getY();
        jHorizon.setA(x - 50);
        jHorizon.setH(50 - y);
    }                                   

Animazione con i Thread

Creare il package animazione e inserire tutti i file java e l’immagine al suo interno. Quando si associa l’icona alla JLabel utilizzare Classpath resource e selezionare la pallina all’interno di src per collegarla con un riferimento relativo. Completare le parti segnalate con […]

package animazione;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JBox extends JPanel implements Runnable {

	private Thread thread;
	private int x = 0, y = 0, vx = 10, vy = 10;
	private JLabel lblPallina;

	/**
	 * Create the panel.
	 */
	public JBox() {
		setLayout(null);

		lblPallina = new JLabel("dfgf");
		lblPallina.setIcon(new ImageIcon(JBox.class
				.getResource("/animazione/pallina.gif")));
		lblPallina.setBounds(0, 0, 10, 10);
		add(lblPallina);
[...]
	}

	@Override
	public void run() {
		while (true) {
			x += vx;
			y += vy;
			if (x >= this.getWidth() - 10 || x <= 0)
				vx = -vx;
[...]
			lblPallina.setBounds(x, y, 10, 10);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {

			}
		}

	}
}

Thread con interfaccia Runnable

package orologio;

import java.awt.Color;
import java.awt.Font;
import java.util.GregorianCalendar;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JOra extends JPanel implements Runnable {

	private Thread thread;
	private JLabel lblOra;
	private JCheckBox chkAttivo;

	public JOra() {
		setBackground(Color.YELLOW);

		lblOra = new JLabel("00:00:00");
		lblOra.setFont(new Font("Courier New", Font.BOLD, 79));
		add(lblOra);

		chkAttivo = new JCheckBox("Attivo");
		chkAttivo.setFont(new Font("Tahoma", Font.PLAIN, 23));
		chkAttivo.setOpaque(false);
		add(chkAttivo);

		thread = new Thread(this);
		thread.setDaemon(true);
		thread.start();
	}

	@Override
	public void run() {
		while (true) {
			if (chkAttivo.isSelected()) {
				GregorianCalendar lt = new GregorianCalendar();
				int ore = lt.get(lt.HOUR);
				int min = lt.get(lt.MINUTE);
				int sec = lt.get(lt.SECOND);
				lblOra.setText((ore < 10 ? "0" + ore : "" + ore) + ":"
						+ (min < 10 ? "0" + min : "" + min) + ":"
						+ (sec < 10 ? "0" + sec : "" + sec));
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
	}

}