crazy4
crazy4

Reputation: 135

Drawing line in swing through user input

I am a newbee in drawing in swing. I have read tutorials but I am not getting it. I have taken input from user in JDialogs and parse them to integer values next when I try to draw line using those values nothing happens. Please help. Someone told me to use opengl binding JOGL is that the way? This is the code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DrawingPanel extends JPanel{
int x0,y0,x1,y1;
DrawingPanel(int ix,int iy,int fx,int fy){
    setLayout(new GridBagLayout());
    x0=ix;
    y0=iy;
    x1=fx;
    y1=fy;
}
public void paintComponent(Graphics g){

    g.drawLine(x0,y0,x1,y1);
}
}
class DDA implements ActionListener{
JFrame j;
JDialog jd;
JButton ok,can;
JTextField ix,iy,fx,fy;
int x0,y0,xf,yf;
DDA(){
    j=new JFrame("DDA Algorithm");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setLocationByPlatform(true);
    j.setLayout(new GridBagLayout());
    j.setExtendedState(Frame.MAXIMIZED_BOTH);
    //j.setSize(500, 500);
    createandrun();
    DrawingPanel ob=new DrawingPanel(x0,y0,xf,yf);

    GridBagConstraints c=new GridBagConstraints();
    c.anchor=GridBagConstraints.PAGE_START;
    j.add(ob,c);
    //j.add(jd);
    j.setResizable(false);
    j.setVisible(true);
    //j.pack();
}
public void createandrun(){
JLabel lx,ly,rx,ry;

jd=new JDialog((Frame)null,"Input Box",true);
//jd.setSize(250, 250);
jd.setLocationRelativeTo(j);
jd.setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();

lx=new JLabel("X0 : ");
ly=new JLabel("Y0 : ");
rx=new JLabel("X1 : ");
ry=new JLabel("Y1 : ");
ix=new JTextField(10);
iy=new JTextField(10);
fx=new JTextField(10);
fy=new JTextField(10);
ok=new JButton("OK");
can=new JButton("Cancel");

c.anchor=GridBagConstraints.PAGE_START;
c.insets=new Insets(5,5,0,5);
c.gridx=c.gridy=0;
jd.add(lx,c);
c.gridx=1;
jd.add(ix,c);
c.gridx=0;
c.gridy=1;
jd.add(ly,c);
c.gridx=1;
jd.add(iy,c);
c.gridx=0;
c.gridy=2;
jd.add(rx,c);
c.gridx=1;
jd.add(fx,c);
c.gridx=0;
c.gridy=3;
jd.add(ry,c);
c.gridx=1;
jd.add(fy,c);
c.gridx=0;
c.gridy=4;
c.insets=new Insets(10,5,5,5);
jd.add(ok,c);
c.gridx=1;
//c.fill=GridBagConstraints.HORIZONTAL;
jd.add(can,c);
ok.addActionListener(this);
can.addActionListener(this);

jd.pack();
jd.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==can){
        jd.dispose();
    }
    if(e.getSource()==ok){
        try{
            x0=Integer.parseInt(ix.getText());
            y0=Integer.parseInt(iy.getText());
            xf=Integer.parseInt(fx.getText());
            yf=Integer.parseInt(fy.getText());
            }
            catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(ok,"Please only numbers!","Invalid Input", JOptionPane.INFORMATION_MESSAGE);
            }
        jd.dispose();
    }
}

public static void main(String s[]){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new DDA();
        }
    });
}
}

Upvotes: 0

Views: 1673

Answers (2)

Reimeus
Reimeus

Reputation: 159754

Nothing is displayed as the GridBagConstraints for the JFrame do not have any fill or weight properties set so do not expand DrawingPanel as required. You could do:

c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;

Alternatively, you could simply use the default BorderLayout of the JFrame and add the panel thus:

j.add(ob);

Side Note: Don't forget to call super.paintComponent(g) when calling paintComponent for painting the child components.

Upvotes: 1

emecas
emecas

Reputation: 1586

In order to it works, you would have to do some specific actions:

Define the method paint in your class DDA instead of paintComponent

public void paint(Graphics g){
    g.drawLine(x0, y0, x1, y1);
}

Move the instantiation of DrawingPanel object ob to the actionPerformed method in DDA class and complete the layout definition. One mistake is that you are calling the constructor of DrawingPanel in the DDA constructor method, even before load x0, y0, xf, yf that are the variables that you are using as line parameters, so include something like this at the end of actionPerformed method:

DrawingPanel ob = new DrawingPanel(x0, y0, xf, yf);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
j.add(ob, c);
j.setResizable(false);
j.setVisible(true);

Call paint method of DrawingPanel object in actionPerformed in DDA after the before block, like this:

ob.repaint(); 

Upvotes: 0

Related Questions