Foros del Web » Programación para mayores de 30 ;) » Java »

problema con graphics

Estas en el tema de problema con graphics en el foro de Java en Foros del Web. Hola, estoy haciendo un programa que lee 2 coordenadas introducidas por el usuario y genera una recta. Estoy haciendo pruebas para dibujar la recta sobre ...
  #1 (permalink)  
Antiguo 03/06/2008, 07:32
 
Fecha de Ingreso: agosto-2006
Mensajes: 65
Antigüedad: 17 años, 8 meses
Puntos: 0
problema con graphics

Hola, estoy haciendo un programa que lee 2 coordenadas introducidas por el usuario y genera una recta.

Estoy haciendo pruebas para dibujar la recta sobre un jFrame, pero tengo un problema; cuando pongo el método paint(Graphics g) con el método drawline dentro, me genera una recta en el jFrame, pero no me deja ver los botones y jTextFields que tengo puestos (no se si será xq el paint se ejecuta por defecto antes y se bloquea).

Si pruebo a poner la implementación del paint en un action performed de un boton, al picar dicho boton me salen muchos errores y no dibuja nada. ¿Qué debo estar haciendo mal? Muchas gracias! Os adjunto el código que tengo:


Código:
public class Frame1 extends JFrame {
    private JPanel jPanel1 = new JPanel();
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JButton jButton4 = new JButton();
    private JButton jButton5 = new JButton();
    private JTextField jTextField1 = new JTextField();
    private JTextField jTextField2 = new JTextField();
    private JButton jButton6 = new JButton();
    private JButton jButton7 = new JButton();
    private JTextField jTextField3 = new JTextField();
    private JTextField jTextField4 = new JTextField();

    Graphics g;
    


    public Frame1() {
        try {
            
            jbInit();
            
          
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(764, 433));
        jPanel1.setBounds(new Rectangle(425, 40, 245, 310));
        jButton1.setText("x inicial");
        jButton1.setBounds(new Rectangle(25, 25, 95, 35));
        jButton2.setText("x final");
        jButton2.setBounds(new Rectangle(25, 75, 95, 35));
        jButton3.setBounds(new Rectangle(30, 125, 100, 35));
        jButton3.setText("DDA");
        jButton4.setText("Bresenham");
        jButton4.setBounds(new Rectangle(30, 180, 100, 35));
        jButton4.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jButton4_actionPerformed(e);
                    }
                });
        jButton5.setText("Punt mig");
        jButton5.setBounds(new Rectangle(30, 230, 100, 35));
        jTextField1.setBounds(new Rectangle(150, 30, 45, 30));
        jTextField2.setBounds(new Rectangle(150, 75, 50, 30));
        jButton6.setText("y inicial");
        jButton6.setBounds(new Rectangle(245, 35, 85, 30));
        jButton7.setText("y final");
        jButton7.setBounds(new Rectangle(245, 75, 85, 30));
        jTextField3.setBounds(new Rectangle(350, 35, 40, 30));
        jTextField4.setBounds(new Rectangle(350, 80, 50, 25));
        this.getContentPane().add(jTextField4,null);
        this.getContentPane().add(jTextField3, null);
        this.getContentPane().add(jButton7, null);
        this.getContentPane().add(jButton6, null);
        this.getContentPane().add(jTextField2, null);
        this.getContentPane().add(jTextField1, null);
        this.getContentPane().add(jButton5, null);
        this.getContentPane().add(jButton4, null);
        this.getContentPane().add(jButton3, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jPanel1, null);
       
        
        this.setVisible(true);
    }

// si descomento esto, me dibuja la linia pero no me deja ver nada + del jframe

   /* public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
       g2.drawLine(300,300,400,400);
        
       
       
        
    }*/
    private void jButton4_actionPerformed(ActionEvent e) {

// aquí me dan muchos errores cuando ejecuto estas instrucciones
        Graphics2D g2 = (Graphics2D) g;
        g2.drawLine(300,300,400,400);
       
  
        int x1 = Integer.parseInt(jTextField1.getText());
        int x2 = Integer.parseInt(jTextField2.getText());
        int y1 = Integer.parseInt(jTextField3.getText());
        int y2 = Integer.parseInt(jTextField4.getText());
            
        int p,y,x,dx,dy;
        
     dx=x2-x1;
     dy=y2-y1;
        p=2*dy-dx;
        y=y1;
        x=x1;
        
       
             g.drawRect(x, y, 1, 1);
            while (x<x2){
                    x++;
                    if(p<=0){
                            p=p+2*dy;
                    }
                    else{
                            p=p+2*dy-2*dx;
                            y++;
                    }
                g.drawRect(x, y, 1, 1);
            }
      
    
    }
  
   public static void main( String args[] ){
            
        new Frame1();
            
        }
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:19.