从两台机器传输代码

时间:2013-04-01 00:02:41

标签: java error-handling polygon

所以我正在与合作伙伴一起开发Java相关项目的代码,但当他向我发送他的代码时,没有任何工作正常。我正在将这个问题扩展到与从两个不同的Eclipse环境中传输代码相关的所有事情。我相信,我们都在运行1.6。

程序本身应打开一个黑色背景的JFrame,顶部有三个按钮(加载,读取,清除),并根据咔嗒声制作直线,右键单击时关闭形状。它适用于他的Mac,但不适用于我的Mac。我们都在使用Eclipse。 (我也在Java博士上测试过。)

我们是新手所以要温柔:)

这是问题所在:

在他编译并点击几次之后:(我无法上传图片而没有代表10) http://i49.tinypic.com/nwwsqf.jpg 编译后: http://i50.tinypic.com/2lxdkyg.png

感谢您的帮助。我希望这也有助于其他人。

以下是代码:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Shape;
    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.GeneralPath;
import java.awt.image.ImageObserver;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.text.AttributedCharacterIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class PolygonMaker extends JFrame {

    private List<Polygon> polygonList = new ArrayList<Polygon>();
    public Polygon polygonInProgress = new Polygon();
    JPanel drwaingPanel;
    JPanel buttonsPanel;

    public PolygonMaker() {
        polygonList.add(polygonInProgress);
    }

    public static void main(String srg[]) {

        new PolygonMaker().generateDrwaingArea();

    }

    public void generateDrwaingArea() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(this);
        setLayout(new BorderLayout());
        setSize(500, 500);
        setResizable(false);
        setLocation(400, 300);
        setTitle("Polygon Maker");
        setVisible(true);
        addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

                if (e.getButton() == MouseEvent.BUTTON1) {
                    polygonInProgress.add(new Point(e.getX(), e.getY()));
                    System.out.println("clicked");
                } else if (e.getButton() == MouseEvent.BUTTON3) {
                    boolean fill = e.isShiftDown();
                    System.out.println("shift pressed");
                    if (polygonInProgress.size() > 2) {
                        recordPolygon(fill);
                        polygonInProgress = new Polygon();
                        polygonList.add(polygonInProgress);
                    }
                }
                repaint();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        buttonsPanel = createButtonsPanel();

        drwaingPanel = new JPanel();
        drwaingPanel.setBackground(Color.black);

        add(buttonsPanel, BorderLayout.NORTH);
        add(drwaingPanel, BorderLayout.CENTER);
    }

    public JPanel createButtonsPanel() {
        JButton loadData = new JButton("Load Data");
        JButton clear = new JButton("Clear");
        loadData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                File poly = new File(
                        "C:\\Users\\Raghav\\Desktop\\poly.dat");
                try {
                    Scanner scanner = new Scanner(poly);
                    Object options[] = { "Ok", "Cancel" };
                    int selection = JOptionPane
                            .showOptionDialog(
                                    new JFrame(),
                                    "Loading File overides current data .Continue loading?",
                                    "Loading File overides current data .Continue loading?",
                                    JOptionPane.OK_CANCEL_OPTION,
                                    JOptionPane.INFORMATION_MESSAGE, null,
                                    options, options[1]);

                    if (selection == JOptionPane.OK_OPTION) {
                        remove(drwaingPanel);                       
                        remove(buttonsPanel);
                        drwaingPanel = new JPanel();
                        drwaingPanel.setBackground(Color.black);                        
                        buttonsPanel = createButtonsPanel();
                        add(drwaingPanel, BorderLayout.CENTER);                     
                        add(buttonsPanel, BorderLayout.NORTH);
                        validate();
                        polygonList = new ArrayList<Polygon>();
                        while (scanner.hasNextLine()) {

                            String line = scanner.nextLine();
                            Polygon polygon = new Polygon();
                            int numberOfPoints = Integer.parseInt(line
                                    .substring(0, line.indexOf(32)));
                            line = line.substring(line.indexOf(32) + 1);
                            String fillStatus = line.substring(0,
                                    line.indexOf(32));
                            System.out.println(fillStatus);
                            if (fillStatus.equals("false"))
                                polygon.setFillStatus(false);
                            else if (fillStatus.equals("true"))
                                polygon.setFillStatus(true);
                            line = line.substring(line.indexOf(32) + 1);
                            String isCompleted = line.substring(0,
                                    line.indexOf(32));
                            System.out.println(isCompleted);
                            if (isCompleted.equals("false"))
                                polygon.setFillStatus(false);
                            else if (isCompleted.equals("true"))
                                polygon.setFinished(true);
                            line = line.substring(line.indexOf(32) + 1);
                            String redValue = line.substring(0,
                                    line.indexOf(32));
                            System.out.println(redValue);
                            line = line.substring(line.indexOf(32) + 1);
                            String greenValue = line.substring(0,
                                    line.indexOf(32));
                            System.out.println(greenValue);
                            line = line.substring(line.indexOf(32) + 1);
                            String yellowValue = line.substring(0,
                                    line.length());
                            System.out.println(yellowValue);

                            polygon.color = new Color(Integer
                                    .parseInt(redValue), Integer
                                    .parseInt(greenValue), Integer
                                    .parseInt(yellowValue));

                            for (int i = 0; i < numberOfPoints; i++) {
                                line = scanner.nextLine();
                                int x = Integer.parseInt(line.substring(0,
                                        line.indexOf(32)));
                                line = line.substring(line.indexOf(32) + 1);
                                int y = Integer.parseInt(line.substring(0,
                                        line.length()));
                                polygon.add(new Point(x, y));
                            }

                            polygonList.add(polygon);


                        }


                        polygonInProgress= new Polygon();
                        polygonList.add(polygonInProgress); 

                        validate();



                    }

                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    JOptionPane.showMessageDialog(new JFrame(),
                            "Cannot find poly.dat");
                }

                validate();


            }
        });
        JButton saveData = new JButton("Save Data");
        saveData.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                File poly = new File("C:\\Users\\Raghav\\Desktop\\poly.dat");

                try {
                    BufferedWriter bufferedWriter = new BufferedWriter(
                            new FileWriter(poly));
                    for (Polygon polygon : polygonList) {
                        try {

                            bufferedWriter.write(polygon.size() + " "
                                    + polygon.isFilled() + " "
                                    + polygon.isFinished() + " "
                                    + polygon.color.getRed() + " "
                                    + polygon.color.getGreen() + " "
                                    + polygon.color.getBlue());
                            bufferedWriter.newLine();

                            for (int i = 0; i < polygon.size(); i++) {
                                bufferedWriter
                                        .write(polygon.getPoints().get(i).x
                                                + " "
                                                + polygon.getPoints().get(i).y);
                                bufferedWriter.newLine();
                            }

                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                    bufferedWriter.close();
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }

            }
        });

        clear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("clear called");
                remove(drwaingPanel);
                drwaingPanel = new JPanel();
                drwaingPanel.setBackground(Color.black);
                add(drwaingPanel, BorderLayout.CENTER);
                remove(buttonsPanel);
                buttonsPanel = createButtonsPanel();
                add(buttonsPanel, BorderLayout.NORTH);
                validate();
                polygonList = new ArrayList<Polygon>();
                polygonInProgress = new Polygon();
                polygonList.add(polygonInProgress);

            }
        });

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setBackground(Color.black);
        buttonsPanel.add(loadData);
        buttonsPanel.add(saveData);
        buttonsPanel.add(clear);


        return buttonsPanel;

    }

    public void paint(Graphics g) {
        for (Polygon polygon : polygonList)
            polygon.paintComponent(g);
    }

    private void recordPolygon(boolean fill) {
        // TODO Auto-generated method stub

        polygonInProgress.setFillStatus(fill);
        polygonInProgress.setFinished(true);
        remove(buttonsPanel);
        remove(drwaingPanel);
        drwaingPanel = new JPanel();
        drwaingPanel.setBackground(Color.black);
        buttonsPanel = createButtonsPanel();
        add(drwaingPanel, BorderLayout.CENTER);
        add(buttonsPanel, BorderLayout.NORTH);
        validate();
        repaint();

        // polygonList.add(polygonInProgress);
        JOptionPane.showMessageDialog(this, "New " + polygonInProgress.size()
                + "-gon recorded \n " + (polygonList.size())
                + " polygons in all");

    }

}

和...

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;
import java.awt.Point;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Polygon {

    static final int POINT_SIZE = 0;
    static Random rnd = new Random();
    private List<Point> points = new ArrayList<Point>();
    public Color color = new Color(rnd.nextInt(255), rnd.nextInt(255),
            rnd.nextInt(255));
    private boolean isFilled = false;
    private boolean isFinished = false;
    private Point lastPointOfPloygon;

    public Polygon() {

    }

    public void write(PrintStream ps) {

    }

    public void read(Scanner scanner) {

    }

    public boolean isFinished() {
        return isFinished;

    }

    public boolean isFilled() {
        return isFilled;

    }

    public List<Point> getPoints() {
        return points;
    }

    public void setFilled(boolean isFilled) {
        this.isFilled = isFilled;
    }

    public void setFillStatus(boolean status) {
        isFilled = status;
    }

    public void paintComponent(Graphics g) {
        System.out.println("paint commponebt calles");
        g.setColor(color);
        for (int i = 0; i < points.size(); i++) {

            if (i == points.size() - 1) {
                System.out.println("last point");
                if (isFinished == true) {
                    System.out.println(isFinished + "connect");
                    if (isFilled == true) 
                    g.fillOval(points.get(i).x, points.get(i).y, 5, 5);
                    g.drawLine(points.get(i).x, points.get(i).y,
                            points.get(0).x, points.get(0).y);
                }
            } else {
                if (isFilled == true) 
                    g.fillOval(points.get(i).x, points.get(i).y, 5, 5);
                    g.drawLine(points.get(i).x, points.get(i).y,
                            points.get(i + 1).x, points.get(i + 1).y);

            }
        }
    }

    public int size() {
        return points.size();
    }

    public void add(Point point) {
        points.add(point);

    }

    public void setFinished(Boolean finishedValue) {
        isFinished = finishedValue;
    }

    @Override
    public String toString() {
        return null;

    }
}

3 个答案:

答案 0 :(得分:3)

这可能不起作用的原因有几个:

  1. 代码(假设它适用于您的合作伙伴)可能无法识别某些导入。当您输入eclipse时,它会在您键入时导入您需要的导入,即使您重新导入它也可能看不到它是需要的地方,如果这是问题,请尝试将鼠标悬停在红色所示的代码上并选择修复
  2. 他/她可能正在导入和/或引用您没有的内容。这可能是因为您的版本已关闭或加载,而另一个则没有,只需要求他/她解决此问题。
  3. 要传输java代码(使用eclipse)我亲自复制我分配给它的整个工作空间折叠,而不仅仅是代码本身,这也将确保它们具有所需的所有相关文件(如果它是图像或声音文件)一场比赛)。
  4. 如果您运行的是不同版本的Mac OS,它也可能影响java代码,但这不太可能,我倾向于在编程时使用Windows或Linux,所以我不知道Mac版本可能存在的所有细微差别
  5. 如果你有一个文件共享服务器,工作站或其他一些编写代码的方法,你可以尝试将项目导入到那里,这样这个问题就不会再发生了。很高兴看到新的程序员,坚持下去,永远不要害怕问这个网站上的每个人都非常乐意提供帮助!祝你的项目顺利:))

答案 1 :(得分:1)

您的代码和您的合作伙伴在MacOS上的一个潜在差异必须是您在代码中的两个位置指定文件的路径:

C:\用户\拉哈夫\桌面\ poly.dat

如果写入文件时出现问题,则会发生异常。在代码中使用相同的文件名两次,即使这是“测试代码”,最好只指定一次文件的名称:

static final String DATA_FILE = "C:\\Users\\Raghav\\Desktop\\poly.dat";

并在代码中引用DATA_FILE。

问题可能不是这个,不幸的是你的问题对于问题的性质是相当模糊的,你说“没有用”或“它在他的Mac上运行良好,但在我的运行中却没有”。这就是Code-Guru和知识分子一直在问的问题。如果您能详细说明问题,我们可能会给出更好的答案。例如:程序没有运行?程序是否因异常而崩溃,异常是什么?程序是否表现不如预期(它做了什么,应该做什么)?

答案 2 :(得分:0)

嗯......不是很多问题...... 但是,如果您正在寻找有关如何在两个人之间编辑代码的提示,我建议您使用可以进入ssh的终端和vim。

相关问题