绘图程序:不断出错,我无法弄清楚原因

时间:2016-11-10 07:46:59

标签: java

我无法弄清楚我的代码有什么问题,任何人都可以给我一些指示吗?我得到的错误消息是: 错误:(122,74)java:不兼容的类型:java.awt.Point无法转换为double 错误:(119,74)java:不兼容的类型:java.awt.Point无法转换为double 错误:(112,23)java:找不到符号   symbol:方法绘制(java.awt.Graphics)   location:类型为javafx.scene.shape.Circle的变量drawCircle

import javafx.scene.shape.Circle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Michael VanKlompenberg
 * Date:  11/5/2016.
 * CIS 117 Java Programming 1
 * Description:
 */
public class DrawingProgram {
    public static void main(String[] args) {
        DrawingFrame f = new DrawingFrame();
        f.setTitle("Drawing Program");
        f.setSize(462, 312);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}

class circle {
    private  int size;
    private Point point;
    private Color color;

    public circle(int size, Point point, Color color) {
        this.size = size;
        this.point = point;
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
    public void draw(Graphics g){
        g.setColor(color);
        g.fillOval(getPoint().x,getPoint().y, size,size);
    }
}

class   DrawingPanel extends JPanel implements MouseMotionListener {

    private int circleDiameter;
    private Color circleColor;
    private Circle drawingCircle;
    private ArrayList<Circle> circleArrayList = new ArrayList<Circle>();

    public int getCircleDiameter() {
        return circleDiameter;
    }

    public void setCircleDiameter(int circleDiameter) {
        this.circleDiameter = circleDiameter;
    }

    public Color getCircleColor() {
        return circleColor;
    }

    public void setCircleColor(Color circleColor) {
        this.circleColor = circleColor;
    }

    public Circle getDrawingCircle() {
        return drawingCircle;
    }

    public void setDrawingCircle(Circle drawingCircle) {
        this.drawingCircle = drawingCircle;
    }

    public DrawingPanel(int circleDiameter, Color circleColor) {
        this.circleDiameter = circleDiameter;
        this.circleColor = circleColor;
        addMouseMotionListener(this);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Iterator<Circle> circleIterator = circleArrayList.iterator();

        Circle drawCircle;
        while(circleIterator.hasNext()) {
            drawCircle = (Circle) circleIterator.next();
            drawCircle.draw(g);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (e.isMetaDown()) {
            Circle newCircle = new Circle(getCircleDiameter(), e.getPoint(), this.getBackground());
            circleArrayList.add(newCircle);
        }else {
            Circle newCircle = new Circle(getCircleDiameter(), e.getPoint(), getCircleColor());
            circleArrayList.add(newCircle);
        }
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
}
class DrawingFrame extends JFrame implements ActionListener {
    private DrawingPanel drawPanel;
    private final int SMALL =4;
    private final int MEDIUM =8;
    private final int LARGE =10;

    //Main menu bar
    private JMenuBar mainBar = new JMenuBar();
    private JMenu file = new JMenu("File");
    private JMenu menuSize = new JMenu("Size");
    private JMenu menuColor = new JMenu("Color");
    private JMenu help = new JMenu("Help");

    //Button groups for color and size radio buttons
    private ButtonGroup size = new ButtonGroup();
    private ButtonGroup color = new ButtonGroup();

    //Sub menu items
    private JMenuItem clear = new JMenuItem("Clear");
    private JMenuItem exit = new JMenuItem("Exit");

    // Size radio buttons
    private JRadioButtonMenuItem small = new JRadioButtonMenuItem("Small");
    private JRadioButtonMenuItem medium = new JRadioButtonMenuItem("Medium");
    private JRadioButtonMenuItem large = new JRadioButtonMenuItem("Large");
    private JMenuItem about = new JMenuItem("About");

    //Color radio buttons
    private JRadioButtonMenuItem red = new JRadioButtonMenuItem("Red");
    private JRadioButtonMenuItem blue = new JRadioButtonMenuItem("Blue");
    private JRadioButtonMenuItem green = new JRadioButtonMenuItem("Green");
    private JRadioButtonMenuItem yellow = new JRadioButtonMenuItem("Yellow");
    private JRadioButtonMenuItem orange = new JRadioButtonMenuItem("Orange");

    public DrawingFrame() {
        drawPanel = new DrawingPanel(SMALL,Color.RED);
        drawPanel.setBackground(Color.WHITE);

        //actionlistners for menu items that are clicked on
        file.addActionListener(this);
        menuSize.addActionListener(this);
        menuColor.addActionListener(this);
        help.addActionListener(this);

        //add buttons and menu items to their groups/location
        setJMenuBar(mainBar);
        mainBar.add(file);
        mainBar.add(menuSize);
        mainBar.add(menuColor);
        mainBar.add(help);
        file.add(clear);
        file.add(exit);
        size.add(small);
        size.add(medium);
        size.add(large);
        color.add(red);
        color.add(blue);
        color.add(green);
        color.add(yellow);
        color.add(orange);

        // add Mnemonics
        file.setMnemonic('F');
        menuSize.setMnemonic('z');
        menuColor.setMnemonic('o');
        help.setMnemonic('H');
        clear.setMnemonic('C');
        exit.setMnemonic('x');
    }



    @Override
    public void actionPerformed(ActionEvent e) {
    if(small.isSelected())
        drawPanel.setCircleDiameter(SMALL);
        if(medium.isSelected())
            drawPanel.setCircleDiameter(MEDIUM);
        if(large.isSelected())
            drawPanel.setCircleDiameter(LARGE);

        if (red.isSelected())
            drawPanel.setCircleColor(Color.RED);
        if (blue.isSelected())
            drawPanel.setCircleColor(Color.BLUE);
        if (green.isSelected())
            drawPanel.setCircleColor(Color.GREEN);
        if (yellow.isSelected())
            drawPanel.setCircleColor(Color.YELLOW);
        if (orange.isSelected())
            drawPanel.setCircleColor(Color.ORANGE);

        String arg = e.getActionCommand();
            if (arg.equals("Exit")) {
                System.exit(0);
            }
            if (arg.equals("Clear")){
                drawPanel = new DrawingPanel(SMALL,Color.RED);
            }
            if (arg.equals("About")) {
                JOptionPane.showMessageDialog(null, "Drawing Program, version 1.0\n"+
                        "by Michael VanKlompenberg\nNovember 9, 2016");
            }
    }
}

1 个答案:

答案 0 :(得分:0)

Circle没有构造函数接受Point。在将X Y传递到圆圈之前从中获取X Y. 您正在此处创建其实例

Circle newCircle = new Circle(getCircleDiameter(), e.getPoint(), getCircleColor());

但你的圈子类有一个小写c。