通过用户输入绘制线条

时间:2013-03-17 14:15:19

标签: java swing drawing paint

我是一个在摇摆中画画的新人。我已经阅读了教程,但我没有得到它。我在JDialogs中从用户那里获取了输入,并在我尝试使用这些值绘制线条时将它们解析为整数值。请帮忙。有人告诉我使用opengl绑定JOGL的方式是什么?这是代码:

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();
        }
    });
}
}

2 个答案:

答案 0 :(得分:1)

由于GridBagConstraints的{​​{1}}没有设置任何JFramefill属性,因此不会显示任何内容,因此请勿根据需要展开{​​{1}}。你可以这样做:

weight

或者,您可以简单地使用DrawingPanel的默认c.fill = GridBagConstraints.BOTH; c.weightx = 1; c.weighty = 1; 并添加面板:

BorderLayout

旁注:在调用JFrame绘制子组件时,不要忘记调用j.add(ob);

答案 1 :(得分:0)

为了起作用,你必须做一些具体的行动:

paint而不是class DDA

中定义方法paintComponent
public void paint(Graphics g){
    g.drawLine(x0, y0, x1, y1);
}

DrawingPanel对象ob的实例化移至actionPerformed类中的method DDA并完成布局定义。一个错误是您在DrawingPanel构造函数方法中调用DDA的构造函数,甚至在加载x0, y0, xf, yf之前,这是您用作行参数的变量,所以包括这样的内容在actionPerformed方法结束时:

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);

在前一个块之后的paintDrawingPanel中调用actionPerformed对象的DDA方法,如下所示:

ob.repaint();