gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:38:46 26 4
gpt4 key购买 nike

我必须使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com