Last active
May 22, 2019 09:57
-
-
Save alvareztech/9022642 to your computer and use it in GitHub Desktop.
Java: Draw shapes 2D and free hand mode.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.Shape; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseMotionAdapter; | |
import java.awt.geom.Line2D; | |
import java.util.ArrayList; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
/** | |
* Clase Dibujar | |
* @author Daniel Alvarez (a3dany) | |
*/ | |
public class Dibujar extends JComponent { | |
private Point inicioArrastre; | |
private Point finArrastre; | |
private ArrayList<Shape> lineas = new ArrayList<Shape>(); | |
public Dibujar() { | |
super(); | |
addMouseListener(new MouseAdapter() { | |
public void mousePressed(MouseEvent e) { // cuando se presiona el mouse | |
inicioArrastre = new Point(e.getX(), e.getY()); | |
repaint(); | |
} | |
public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse | |
finArrastre = new Point(e.getX(), e.getY()); | |
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y); | |
lineas.add(linea); | |
repaint(); | |
} | |
}); | |
addMouseMotionListener(new MouseMotionAdapter() { | |
public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse | |
finArrastre = new Point(e.getX(), e.getY()); | |
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y); | |
lineas.add(linea); | |
inicioArrastre = new Point(finArrastre.x, finArrastre.y); | |
repaint(); | |
} | |
}); | |
} | |
public void paint(Graphics g) { | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setColor(Color.RED); | |
for (Shape linea : lineas) { // dibuja todos las elipses | |
g2.draw(linea); | |
} | |
} | |
private Line2D.Float crearLinea(int x1, int y1, int x2, int y2) { | |
return new Line2D.Float(x1, y1, x2, y2); | |
} | |
public static void main(String[] a3d) { | |
JFrame ventana = new JFrame("Dibujar"); | |
ventana.setSize(400, 300); | |
ventana.setLocationRelativeTo(null); | |
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
ventana.add(new Dibujar()); | |
ventana.setVisible(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.Shape; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseMotionAdapter; | |
import java.awt.geom.Ellipse2D; | |
import java.util.ArrayList; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
/** | |
* Clase DibujarElipses | |
* @author Daniel Alvarez (a3dany) | |
*/ | |
public class DibujarElipses extends JComponent { | |
private Point inicioArrastre; | |
private Point finArrastre; | |
private ArrayList<Shape> elipses = new ArrayList<Shape>(); | |
public DibujarElipses() { | |
super(); | |
addMouseListener(new MouseAdapter() { | |
public void mousePressed(MouseEvent e) { // cuando se presiona el mouse | |
inicioArrastre = new Point(e.getX(), e.getY()); | |
finArrastre = inicioArrastre; | |
repaint(); | |
} | |
public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse | |
Shape elipse = crearElipse(inicioArrastre.x, inicioArrastre.y, e.getX(), e.getY()); | |
elipses.add(elipse); | |
inicioArrastre = null; | |
finArrastre = null; | |
repaint(); | |
} | |
}); | |
addMouseMotionListener(new MouseMotionAdapter() { | |
public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse | |
finArrastre = new Point(e.getX(), e.getY()); | |
repaint(); | |
} | |
}); | |
} | |
public void paint(Graphics g) { | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setColor(Color.RED); | |
for (Shape elipse : elipses) { // dibuja todos las elipses | |
g2.fill(elipse); | |
} | |
if (inicioArrastre != null && finArrastre != null) { // se esta arrastrando el raton? | |
Shape elipse = crearElipse(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y); | |
g2.draw(elipse); | |
} | |
} | |
private Ellipse2D.Float crearElipse(int x1, int y1, int x2, int y2) { | |
// ajusta la elipse | |
return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); | |
} | |
public static void main(String[] a3d) { | |
JFrame ventana = new JFrame("Dibujar Elipses"); | |
ventana.setSize(400, 300); | |
ventana.setLocationRelativeTo(null); | |
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
ventana.add(new DibujarElipses()); | |
ventana.setVisible(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.Shape; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseMotionAdapter; | |
import java.awt.geom.Line2D; | |
import java.util.ArrayList; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
/** | |
* Clase DibujarLineas | |
* @author Daniel Alvarez (a3dany) | |
*/ | |
public class DibujarLineas extends JComponent { | |
private Point inicioArrastre; | |
private Point finArrastre; | |
private ArrayList<Shape> lineas = new ArrayList<Shape>(); | |
public DibujarLineas() { | |
super(); | |
addMouseListener(new MouseAdapter() { | |
public void mousePressed(MouseEvent e) { // cuando se presiona el mouse | |
inicioArrastre = new Point(e.getX(), e.getY()); | |
finArrastre = inicioArrastre; | |
repaint(); | |
} | |
public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse | |
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, e.getX(), e.getY()); | |
lineas.add(linea); | |
inicioArrastre = null; | |
finArrastre = null; | |
repaint(); | |
} | |
}); | |
addMouseMotionListener(new MouseMotionAdapter() { | |
public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse | |
finArrastre = new Point(e.getX(), e.getY()); | |
repaint(); | |
} | |
}); | |
} | |
public void paint(Graphics g) { | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setColor(Color.RED); | |
for (Shape linea : lineas) { // dibuja todos las elipses | |
g2.draw(linea); | |
} | |
if (inicioArrastre != null && finArrastre != null) { // se esta arrastrando el raton? | |
Shape linea = crearLinea(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y); | |
g2.draw(linea); | |
} | |
} | |
private Line2D.Float crearLinea(int x1, int y1, int x2, int y2) { | |
return new Line2D.Float(x1, y1, x2, y2); | |
} | |
public static void main(String[] a3d) { | |
JFrame ventana = new JFrame("Dibujar Lineas"); | |
ventana.setSize(400, 300); | |
ventana.setLocationRelativeTo(null); | |
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
ventana.add(new DibujarLineas()); | |
ventana.setVisible(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.Shape; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseMotionAdapter; | |
import java.awt.geom.Rectangle2D; | |
import java.util.ArrayList; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
/** | |
* Clase DibujarRectangulos | |
* @author Daniel Alvarez (a3dany) | |
*/ | |
public class DibujarRectangulos extends JComponent { | |
private Point inicioArrastre; | |
private Point finArrastre; | |
private ArrayList<Shape> rectangulos = new ArrayList<Shape>(); | |
public DibujarRectangulos() { | |
super(); | |
addMouseListener(new MouseAdapter() { | |
public void mousePressed(MouseEvent e) { // cuando se presiona el mouse | |
inicioArrastre = new Point(e.getX(), e.getY()); | |
finArrastre = inicioArrastre; | |
repaint(); | |
} | |
public void mouseReleased(MouseEvent e) { // cuando se deja de presionar el mouse | |
Shape rectangulo = crearRectangulo(inicioArrastre.x, inicioArrastre.y, e.getX(), e.getY()); | |
rectangulos.add(rectangulo); | |
inicioArrastre = null; | |
finArrastre = null; | |
repaint(); | |
} | |
}); | |
addMouseMotionListener(new MouseMotionAdapter() { | |
public void mouseDragged(MouseEvent e) { // cuando se esta arrastrando el mouse | |
finArrastre = new Point(e.getX(), e.getY()); | |
repaint(); | |
} | |
}); | |
} | |
public void paint(Graphics g) { | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setColor(Color.RED); | |
for (Shape rectangulo : rectangulos) { // dibuja todos los rectangulos | |
g2.fill(rectangulo); | |
} | |
if (inicioArrastre != null && finArrastre != null) { // se esta arrastrando el raton? | |
Shape rectangulo = crearRectangulo(inicioArrastre.x, inicioArrastre.y, finArrastre.x, finArrastre.y); | |
g2.draw(rectangulo); | |
} | |
} | |
private Rectangle2D.Float crearRectangulo(int x1, int y1, int x2, int y2) { | |
// ajusta el rectangulo | |
return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2)); | |
} | |
public static void main(String[] a3d) { | |
JFrame ventana = new JFrame("Dibujar Rectangulos"); | |
ventana.setSize(400, 300); | |
ventana.setLocationRelativeTo(null); | |
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
ventana.add(new DibujarRectangulos()); | |
ventana.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment