0

我是摇摆绘画的新手。我已经阅读了教程,但我没有得到它。当我尝试使用这些值绘制线条时,我已经在 J​​Dialogs 中从用户那里获取输入并将它们解析为整数值,但没有任何反应。请帮忙。有人告诉我使用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();
        }
    });
}
}
4

2 回答 2

1

没有显示任何内容,因为GridBagConstraints没有JFrame设置任何fillweight属性,因此不要DrawingPanel根据需要展开。你可以这样做:

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

或者,您可以简单地使用默认值BorderLayoutJFrame添加面板:

j.add(ob);

旁注:调用绘制子组件super.paintComponent(g)时不要忘记调用。paintComponent

于 2013-03-17T14:43:06.613 回答
0

为了使它起作用,您必须执行一些特定操作:

paint在你的class DDA而不是定义方法paintComponent

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

DrawingPanel将object的实例化移动obactionPerformed methodinDDA类中,完成布局定义。一个错误是您在构造函数方法中调用构造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);

在 before 块之后调用in in对象的paint方法,如下所示:DrawingPanelactionPerformedDDA

ob.repaint(); 
于 2013-03-17T15:23:30.860 回答