因此,我正在与合作伙伴一起为 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;
}
}