java - 为什么单击时没有出现圆圈?

标签 java swing graphics mouseevent

我必须使用 HoltSoft 的 Ready to Program 中的 Console 类。我不应该使用 swing,所以如果我不能没有 swing,请忽略它。

//imports
import java.awt.*; 
import java.awt.event.*;
import hsa.*;

public class DrawLines extends Panel implements MouseListener, MouseMotionListener
{
    Console c;
    int startX, startY, prevX, prevY; //mouse coordinates
    private boolean dragging; //whether or not the mouse is being dragged
    MouseEvent e;
    public DrawLines ()
    {
        c = new Console (); //creates console window
        addMouseListener (this); //detects press/release
        addMouseMotionListener (this);//detects dragging
    }


    public void mousePressed (MouseEvent e)
    {
        while (!dragging)
        {
            try
            {
                startX = e.getX ();//get the
                startY = e.getY ();//original co-ordinates
                dragging = true;
            }
            catch (NullPointerException q) //because I kept getting this error
            {
            }
        }
    }


    public void mouseDragged (MouseEvent e)
    {
        while (dragging)
        {
            try
            {
                int x = e.getX (); //gets and
                int y = e.getY (); //updates
                prevX = x;         //the mouse
                prevY = y;         //coordinates
            }
            catch (NullPointerException q)//because I kept getting this error
            {
            }
        }
    }


    public void mouseReleased (MouseEvent e)
    {
        dragging = false; //stopped dragging
    }


    public void drawTheLine ()
    {
        mousePressed (e);
        mouseDragged (e);
        c.setColor (Color.black);
        c.fillOval (prevX, prevY, 50, 50); //draws a circle where the mouse is 
        mouseReleased (e);
    }


    public void mouseMoved (MouseEvent e){}
    public void mouseEntered (MouseEvent e){}
    public void mouseExited (MouseEvent e){}
    public void mouseClicked (MouseEvent e){}

    public static void main (String[] args)
    {
        DrawLines a = new DrawLines ();
        a.drawTheLine ();
    }
}

我一直在尝试在控制台中使用 MouseListener 和 MouseMotionListener。起初,程序一直给我错误,所以我添加了 try/catch 结构。现在它没有崩溃,但屏幕上什么也没有出现。为什么?帮忙?

如果我不应该使用 try/catch 来忽略它,我该怎么办?

除了 Console() 之外,我不能为这个程序使用任何东西。这是一项类(class)作业。

最佳答案

看看这个:

public void drawTheLine ()
{
    while (true)
    {
        mousePressed (e);
        mouseDragged (e);
        c.setColor (Color.black);
        c.fillOval (prevX, prevY, 50, 50); //draws a circle where the mouse is 
        mouseReleased (e);
    }
}

您传递的参数“e”为空。在这里声明:

public class DrawLines extends Panel 
    implements MouseListener, MouseMotionListener
{
    MouseEvent e; // IT IS NEVER SET TO ANYTHING! IT IS NULL!!!

你应该在你的构造函数的某个地方这样做,这样它就不再是空的了:

e = (something);

关于java - 为什么单击时没有出现圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14143728/

相关文章:

java - 使用 JOptionPane 按钮创建条件状态

java - 更改一个对象的属性时替换重复代码的设计模式

java - 序列化相对简单的 Java POJO 的最快方法?

api - vulkan api 会处理窗口创建吗?

android - 如何绘制闭合曲线形状?

java - 如何知道 Java 应用程序是否使用 MVC 设计模式?

java - 无法显示图标图像

java - 连接到 JDBC 时使用 getColumnClass() 对 JTable 进行排序

java - JPanel 中的计时器未启动

android - 更改按钮的背景 alpha 而不更改 Android 中的文本 alpha 值?