简体   繁体   English

绘图程序:不断出现错误,我不知道为什么

[英]Drawing program: Keep getting errors and I can't figure out why

I am having trouble figuring out what is wrong with my code can anyone give me some pointers? 我在弄清楚我的代码有什么问题时遇到麻烦,任何人都可以给我一些指针吗? The error message that i am getting is : Error:(122, 74) java: incompatible types: java.awt.Point cannot be converted to double Error:(119, 74) java: incompatible types: java.awt.Point cannot be converted to double Error:(112, 23) java: cannot find symbol symbol: method draw(java.awt.Graphics) location: variable drawCircle of type javafx.scene.shape.Circle 我收到的错误消息是:错误:(122,74)Java:不兼容类型:java.awt.Point无法转换为双倍错误:(119,74)Java:不兼容类型:java.awt.Point无法转换为double错误:(112,23)java:找不到符号符号:方法draw(java.awt.Graphics)位置:变量类型为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");
            }
    }
}

Circle has no constructor that takes Point. Circle没有采用Point的构造函数。 Get the XY from point before passing it to the circle. 从点获取XY,然后再将其传递到圆上。
You are creating its instance here 您正在此处创建其实例

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

but your circle class has a lowercase c. 但您的圈子类别具有小写字母c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚为什么我不断收到找不到符号编译错误 - Can't figure out why i keep getting cannot find symbol compile error 我不知道为什么我得到StackOverFlowError - I can't figure out why I'm getting StackOverFlowError 当我清楚地覆盖平等方法时,我似乎无法弄清楚为什么我一直保持真实 - I can't seem to figure out why I keep getting true when I clearly overridden the equality method 无法弄清楚为什么我得到了StringIndexOutOfBoundsException - Can't figure out why I'm getting a StringIndexOutOfBoundsException 无法弄清楚为什么我得到空值 - Can't figure out why I am getting null values 当我尝试运行程序时,GUI无法加载,我无法弄清楚为什么 - When i try to run my program, the GUI won't load and I can't figure out why 我无法弄清楚为什么我的程序在运行时没有打印 - I can't figure out why my program isn't printing when I run it Java:不断收到 IndexOutOfBoundsException,无法弄清楚我在哪里使用了无效索引 - Java: Keep getting an IndexOutOfBoundsException, can't figure out where I'm using an invalid index 我不明白为什么我的程序没有循环 - I can't figure out why my program isn't looping 您好,我似乎无法弄清楚为什么我的程序无法正常工作 - Hello I can't seem to figure out why my program isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM