Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////FORM DIBUJAR////////////////////////////
- public final class Dibujar extends javax.swing.JFrame {
- private Connection con;
- private Statement st;
- private ResultSet rs;
- private boolean pintar = false;
- private int x1, y1, d1, d2;
- private int fig;
- private ArrayList<Figura> array = new ArrayList<>();
- public Dibujar() throws IOException {
- this.getContentPane().setBackground(Color.BLACK);
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- btnGuardar = new javax.swing.JButton();
- btnCargar = new javax.swing.JButton();
- btnLinea = new javax.swing.JButton();
- btnRectangulo = new javax.swing.JButton();
- btnCirculo = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- formMouseClicked(evt);
- }
- });
- btnGuardar.setText("Guardar");
- btnGuardar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnGuardarActionPerformed(evt);
- }
- });
- btnCargar.setText("Cargar");
- btnCargar.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnCargarActionPerformed(evt);
- }
- });
- btnLinea.setBackground(new java.awt.Color(0, 204, 51));
- btnLinea.setFont(new java.awt.Font("AR CHRISTY", 0, 12)); // NOI18N
- btnLinea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proyecto/u3/linea.png"))); // NOI18N
- btnLinea.setText("Lineas");
- btnLinea.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnLineaActionPerformed(evt);
- }
- });
- btnRectangulo.setBackground(new java.awt.Color(255, 102, 51));
- btnRectangulo.setFont(new java.awt.Font("AR CHRISTY", 0, 12)); // NOI18N
- btnRectangulo.setIcon(new javax.swing.ImageIcon("C:\\Users\\Naieli\\Documents\\SEMESTRE 5\\TAP\\U1\\figuras\\figuras\\cuadrado-rojo-icono-8697-96.png")); // NOI18N
- btnRectangulo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnRectanguloActionPerformed(evt);
- }
- });
- btnCirculo.setBackground(new java.awt.Color(51, 51, 255));
- btnCirculo.setFont(new java.awt.Font("AR CHRISTY", 0, 12)); // NOI18N
- btnCirculo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proyecto/u3/circulo-verde-icono-4055-128.png"))); // NOI18N
- btnCirculo.setText("Circulos");
- btnCirculo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnCirculoActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(30, 30, 30)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(btnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(btnLinea, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(28, 28, 28)
- .addComponent(btnRectangulo, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
- .addComponent(btnCirculo, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(19, 19, 19))))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(btnLinea, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnRectangulo, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(btnCirculo, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 359, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(btnCargar, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
- .addComponent(btnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(29, 29, 29))
- );
- pack();
- }// </editor-fold>
- private void formMouseClicked(java.awt.event.MouseEvent evt) {
- // TODO add your handling code here:
- Graphics pen = this.getGraphics();
- pen.setColor(Color.CYAN);
- Figura aux = new Figura();
- if (!pintar) {
- pintar = true;
- x1 = evt.getX();
- y1 = evt.getY();
- } else {
- if (fig == 1) {
- d1 = Math.abs(evt.getX() - x1);
- d2 = Math.abs(evt.getY() - y1);
- equis(evt.getX(), evt.getY());
- aux.setNombre("Rectangulo");
- aux.setX1(x1);
- aux.setY1(y1);
- aux.setD1(d1);
- aux.setD2(d2);
- pen.drawRect(x1, y1, d1, d2);
- array.add(aux);
- aux=new Figura();
- } else if (fig == 2) {
- aux.setNombre("Linea");
- aux.setX1(x1);
- aux.setY1(y1);
- aux.setD1(evt.getX());
- aux.setD2(evt.getY());
- array.add(aux);
- pen.drawLine(x1, y1, evt.getX(), evt.getY());
- aux=new Figura();
- } else if (fig == 3) {
- d1 = (int) Math.sqrt(Math.pow((x1 - evt.getX()), 2) + Math.pow((y1 - evt.getY()), 2));
- aux.setNombre("Circulo");
- aux.setX1(x1 - (int) (d1 / 2));
- aux.setY1(y1 - (int) (d1 / 2));
- aux.setD1(d1);
- aux.setD2(d1);
- array.add(aux);
- pen.drawOval((x1 - (int) (d1 / 2)), (y1 - (int) (d1 / 2)), d1, d1);
- aux=new Figura();
- }
- pintar = false;
- }
- }
- private void btnCirculoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- this.fig = 3;
- }
- private void btnRectanguloActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- this.fig = 1;
- }
- private void btnLineaActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- this.fig = 2;
- }
- private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
- st=con.createStatement();
- for(int i=0; i<array.size();i++){
- String query =String.format("INSERT INTO dibujos(Nombre,x1,y1,x2,y2) VALUES('%s',%d,%d,%d,%d);",array.get(i).getNombre(),array.get(i).getX1(),array.get(i).getY1(),array.get(i).getD1(),array.get(i).getD2());
- System.out.println(query);
- st.executeUpdate(query);
- }
- }catch(Exception e){
- System.out.println("error: "+e);
- }
- }
- private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:g.setColor(Color.red);
- array.clear();
- try{
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
- st=con.createStatement();
- String query ="SELECT * FROM dibujos";
- rs = st.executeQuery(query);
- System.out.println("Contenido");
- while(rs.next()){
- String Nombre = rs.getString("Nombre");
- int xx1 = rs.getInt("x1");
- int yy1 = rs.getInt("y1");
- int xx2 = rs.getInt("x2");
- int yy2 = rs.getInt("y2");
- array.add(new Figura(Nombre,xx1,yy1,xx2,yy2));
- }
- }catch(Exception e){
- System.out.println("error");
- }
- Graphics g=this.getGraphics();
- g.setColor(Color.PINK);
- for(int c=0;c<array.size();c++){
- System.out.println(array.get(c).getTodo());
- if(array.get(c).getNombre().equalsIgnoreCase("Rectangulo")){
- g.drawRect(array.get(c).getX1(), array.get(c).getY1(), array.get(c).getD1(), array.get(c).getD2());
- }
- else if(array.get(c).getNombre().equalsIgnoreCase("Linea")){
- g.drawLine(array.get(c).getX1(), array.get(c).getY1(), array.get(c).getD1(), array.get(c).getD2());
- }
- else if(array.get(c).getNombre().equalsIgnoreCase("Circulo")){
- g.drawOval(array.get(c).getX1(), array.get(c).getY1(), array.get(c).getD1(), array.get(c).getD2());
- }
- }
- }
- private void equis(int x2, int y2) {
- if (x1 > x2 && y1 > y2) {
- x1 = x2;
- y1 = y2;
- } else if (x1 < x2 && y1 > y2) {
- y1 = y2;
- } else if (x1 > x2 && y1 < y2) {
- x1 = x2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement