简体   繁体   English

Java绘画程序给出很多错误

[英]Java painting program giving many errors

I want to make a simple painting program. 我想制作一个简单的绘画程序。 So,when you drag your mouse a line will draw in a GUI. 因此,当您拖动鼠标时,会在GUI中绘制一条线。 The problem is when the user drags the mouse, it will paint automatically, but my code doesn't work. 问题是当用户拖动鼠标时,它将自动绘制,但是我的代码不起作用。 Can someone please tell me how do this? 有人可以告诉我该怎么做吗? Sorry for my English and if you don't understand my question, look at my code maybe you will than. 对不起,我的英语,如果您不明白我的问题,那么也许可以看看我的代码。

My main class: 我的主班:

import javax.swing.JFrame;
public class MainClass {
    public static void main(String args[]){
        tuna kip = new tuna();
        kip.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        kip.setSize(800,600);
        kip.setVisible(true);
    }
}

This is my other class: 这是我的另一堂课:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class tuna extends JFrame {
    JPanel jpanel = new JPanel();

    public tuna(){
        super("Painting Program");
        jpanel.setBackground(Color.WHITE);
        add(jpanel);

        hand handler = new hand();
        jpanel.addMouseListener(handler);
        jpanel.addMouseMotionListener(handler);
    }

    private class hand implements MouseListener ,MouseMotionListener { //THE ERRORS START TO APPEAR HERE
        public void mouseDragged(MouseEvent event){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.BLACK);
                g.fillRect(event.getX(), event.getY(), 5, 5);
            }
        }
    }
}

When I try to run the code I get too much error messages: 当我尝试运行代码时,收到太多错误消息:

You mean when you try to compile the code you get compile errors. 您的意思是,当您尝试compile代码时,会出现编译错误。

class hand implements MouseListener ,MouseMotionListener 

Your class does not implement all the methods in those listeners. 您的类未在那些侦听器中实现所有方法。 You only implement one method. 您仅实现一种方法。

Read the section from the Swing tutorial on How to Write a MouseMotionListener for a working example. 阅读Swing教程中有关如何编写MouseMotionListener的部分 ,以获取工作示例。

If you only care about the mouseDragged() method then you only need to implement the MouseMotionListener . 如果只关心mouseDragged()方法,则只需要实现MouseMotionListener

Or as a simpler solution you can extend MouseMotionAdapter . 或者,作为更简单的解决方案,您可以扩展MouseMotionAdapter This class implements all the methods of the MouseMotionListener so you only need to override the methods you want to change. 此类实现了MouseMotionListener所有方法,因此您只需要覆盖要更改的方法。 The tutorial also discusses adapters. 本教程还讨论了适配器。

Finally class names SHOULD start with an upper case character. 最后,类名应该以大写字母开头。 Look at the Java API and you will notice this. 查看Java API,您会注意到这一点。 Follow the Java conventions and don't make up your own. 遵循Java约定,不要自己编写。

Yes, there are so many issues in your codes. 是的,您的代码中有很多问题。 I just edited it and make it runnable at least. 我只是对其进行了编辑,至少使其可运行。 Just study and experiment on it. 只需对其进行研究和实验。 I hope you will learn something out of this even if I don't explain all of the changes. 我希望即使我不解释所有更改,您也将从中学到一些东西。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class tuna extends JFrame {
    int x, y, w, h;

    MyPanel jpanel = new MyPanel();

    public tuna(){
        super("Painting Program");
        setLayout(new BorderLayout());
        jpanel.setBackground(Color.WHITE);
        add(jpanel);

        hand handler = new hand();
        jpanel.addMouseListener(handler);
        jpanel.addMouseMotionListener(handler);
    }

    private class hand implements MouseListener , MouseMotionListener { //THE ERRORS START TO APPEAR HERE
        public void mouseClicked(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            x=e.getX();
            y=e.getY();
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mouseDragged(MouseEvent e) {
            w = e.getX() - x;
            h = e.getY() - y;
            jpanel.repaint();
        }

        public void mouseMoved(MouseEvent e) {
        }
    }

    class MyPanel extends JPanel {
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(x, y, w, h);
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM