1

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

程序本身应该打开一个黑色背景的 JFrame,顶部有三个按钮(加载、读取、清除),并根据点击制作直线,右键单击时关闭形状。它在他的 Mac 上运行良好,但在我的 Mac 上运行良好。我们都在使用 Eclipse。(我也在 Dr. 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;

    }
}
4

3 回答 3

3

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

  1. 该代码(假设它适用于您的合作伙伴)可能无法识别某些导入。当您在 eclipse 中键入时,它会在您键入时导入您需要的导入,即使您重新导入它也可能看不到它需要的地方,如果这是问题,请尝试将鼠标悬停在红色概述的代码上并选择修复.
  2. 他/她可能正在导入和/或引用您没有的东西。这可能是因为您的某个版本关闭或附加组件有而另一个没有,只需让他/她解决这个问题。
  3. 为了传输java代码(使用eclipse),我个人复制了我分配给它的整个工作空间折叠,而不仅仅是它自己的代码,这也将确保他们拥有所需的所有依赖文件(如果是游戏,则如图像或声音文件) .
  4. 如果您运行的是不同版本的 Mac OS,它也会影响 java 代码,但这不太可能,我在编程时倾向于使用 windows 或 Linux,所以我不知道 Mac 版本可能存在的所有细微差异。

如果您有文件共享服务器、工作站或其他同时编写代码的方式,您可以尝试将您的项目导入到那里,这样这个问题就不会再次发生。很高兴见到新的程序员,保持下去,永远不要害怕提出问题,这个网站上的每个人都非常乐意提供帮助!祝你的项目好运:)

于 2013-04-01T00:14:00.043 回答
1

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

C:\Users\Raghav\Desktop\poly.dat

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

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

并参考代码中的 DATA_FILE。

问题可能不是这个,不幸的是,您的问题对问题的性质非常模糊,您说“没有任何效果”或者“在他的 Mac 上运行良好,但在我的 Mac 上运行良好”。这就是 Code-Guru 和 knowbody 一直在问的问题。如果您能详细说明问题,我们可能会给出更好的答案。例如:程序没有运行吗?程序是否因异常而崩溃,异常是什么?程序是否没有按预期运行(它做了什么,它应该做什么)?

于 2013-04-01T00:32:42.790 回答
0

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

于 2013-04-01T00:20:10.553 回答