Skip to content

Instantly share code, notes, and snippets.

@donal56
Created December 15, 2019 04:04
Show Gist options
  • Save donal56/54212d0921646515aa3ada7f7d77fc64 to your computer and use it in GitHub Desktop.
Save donal56/54212d0921646515aa3ada7f7d77fc64 to your computer and use it in GitHub Desktop.
Quick cartesian map module
package app;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
//import java.util.concurrent.*;
class App extends TimerTask
{
private static JFrame ventana;
private static JLabel titulo;
private static PlanoCartesiano plano;
private static JTextArea consola;
private static JScrollPane consolaScroll;
private static JScrollBar vertical;
private static Timer timer;
private static String cad = "";
private static final int LIMITE_ULTRASONICO = 20;
private static final int LIMITE_INFRARROJO = 20;
public static void main (String[] args)
{
init();
addComponents();
ventana.setVisible(true);
timer.schedule(new App(), 0, 1000);
// ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
// executor.scheduleAtFixedRate(helloRunnable, 0, 1, TimeUnit.SECONDS);
}
private static void init()
{
ventana = new JFrame("Conexión");
titulo = new JLabel("Conexión PIC-PC");
plano = new PlanoCartesiano(380, PlanoCartesiano.MODE_RANDOM);
consola = new JTextArea();
consolaScroll = new JScrollPane(consola, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
timer = new Timer(true);
System.out.println("Inicio del sistema");
initVentana();
initComponents();
}
private static void initVentana()
{
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
ventana.setSize(500, 500);
ventana.setResizable(false);
ventana.setContentPane(panel);
ventana.setLayout(new BoxLayout(ventana.getContentPane(), BoxLayout.PAGE_AXIS));
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setLocationRelativeTo(null);
}
private static void initComponents()
{
titulo.setHorizontalAlignment(SwingConstants.CENTER);
titulo.setFont(new Font("Calibri", Font.PLAIN, 16));
titulo.setAlignmentX(Component.CENTER_ALIGNMENT);
plano.setAlignmentX(Component.CENTER_ALIGNMENT);
// consolaScroll.setMaximumSize(new Dimension(350,200));
// consolaScroll.setPreferredSize(new Dimension(350,200));
// consolaScroll.setAlignmentX(Component.CENTER_ALIGNMENT);
// consola.setEnabled(false);
}
private static void addComponents()
{
ventana.add(titulo);
ventana.add(Box.createRigidArea(new Dimension(0, 10)));
ventana.add(plano);
// ventana.add(Box.createRigidArea(new Dimension(0, 10)));
// ventana.add(consolaScroll);
}
public void run()
{
// int valorPrueba = 15;
// int usLeft = 0 - valorPrueba;
// int usRight = valorPrueba;
// int infr = valorPrueba;
// int[] coord1 = {usLeft, infr};
// int[] coord2 = {usRight, infr};
// cad = "Punto 1: " + Arrays.toString(coord1) + ", Punto 2: " + Arrays.toString(coord2);
// coord1 = transformarCoord(coord1);
// coord2 = transformarCoord(coord2);
// plano.setCoords(new int[][] {coord1, coord2});
plano.setRandomCoords(3);
cad = "Punto 1: " + Arrays.toString(plano.getRealCoord(1)) + ", Punto 2: " + Arrays.toString(plano.getRealCoord(2));
// vertical = consolaScroll.getVerticalScrollBar();
// vertical.setValue(vertical.getMaximum());
System.out.println(cad);
consola.setText(consola.getText() + "\n" + cad);
ventana.getContentPane().validate();
ventana.getContentPane().repaint();
}
// static Runnable helloRunnable = new Runnable()
// {
// public void run()
// {
// //Content
// }
// };
public static int[] transformarCoord(int[] c)
{
//Esto deberia ser mas general
//Al igual que cuando se recuperan las coordenadas del punto
int limite;
for(int i= 0; i < c.length; i++)
{
int n = c[i];
if(i == 0)
limite = LIMITE_ULTRASONICO;
else
limite = LIMITE_INFRARROJO;
if(Math.abs(n) >= limite)
{
c[i] = plano.getAxisSize() * n / Math.abs(n);
}
else
{
c[i] = plano.getAxisSize() * n / limite;
}
}
return c;
}
}
package app;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
public class PlanoCartesiano extends JPanel
{
private static final long serialVersionUID = 1L;
private int[][] coords;
private final int SIZE;
private final int MODE;
private final int DIVISIONES = 10;
public static final int MODE_RANDOM = 31;
public static final int MODE_INSERT = 32;
public PlanoCartesiano(int size, int mode)
{
super();
this.SIZE = size;
this.MODE = mode;
setMaximumSize(new Dimension(SIZE, SIZE));
setMinimumSize(new Dimension(SIZE, SIZE));
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawGrid(g2d);
for(int i= 0; i < this.coords.length; i++)
{
drawPoint(g2d, i);
}
}
protected void drawGrid(Graphics2D g2d)
{
int center = SIZE / 2;
int cuantos = SIZE / DIVISIONES;
g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 10f}, 2.0f));
g2d.setColor(Color.decode("969696"));
for(int i = 0; i < DIVISIONES; i++)
{
g2d.drawLine(i * cuantos, 0, i * cuantos, SIZE);
}
for(int i = 0; i < DIVISIONES; i++)
{
g2d.drawLine(0, i * cuantos, SIZE, i * cuantos);
}
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(0, center, SIZE, center);
g2d.drawLine(center, 0, center, SIZE);
}
protected void drawPoint(Graphics2D g2d, int index)
{
int[] coords = transformCoord(this.coords[index]);
int x = this.coords[index][0];
int y = this.coords[index][1];
g2d.setPaint(Color.GREEN);
g2d.setStroke(new BasicStroke(5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(x, y, x, y);
g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawOval(x - 8, y - 8, 16, 16);
g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawOval(x - 15, y - 15, 30, 30);
g2d.setPaint(Color.BLACK);
g2d.drawString("x: " + coords[0] + ", y: " + coords[1], x - 15, y - 15);
}
public void setRandomCoords(int n)
{
if(this.MODE == MODE_RANDOM)
{
this.coords = new int[n][2];
for(int i= 0; i < n; i++)
{
this.coords[i] = getRandomCoordinates();
}
}
else
{
throw new NullPointerException("Acceso a modo incorrecto");
}
}
public void setCoords(int[][] coords)
{
if(this.MODE == MODE_INSERT)
{
for(int i = 0; i < coords.length; i++)
{
coords[i] = transformCoord(coords[i]);
}
this.coords = coords;
}
else
{
throw new NullPointerException("Acceso a modo incorrecto");
}
}
public int[] getRealCoord(int i)
{
return coords[i - 1];
}
public int[] getRandomCoordinates()
{
int x= (int) (Math.random() * SIZE);
int y= (int) (Math.random() * SIZE);
return new int[] {x, y};
}
public int getAxisSize()
{
return this.SIZE / 2;
}
private int[] transformCoord(int[] c)
{
int[] arr = new int[2];
arr[0] = c[0] + this.SIZE / 2;
if(c[1] <= 0)
arr[1] = c[1] + this.SIZE / 2;
else
arr[1] = this.SIZE / 2 - c[1];
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment