拖动鼠标绘制线组时出现滞后的原因 [英] Cause of Lag in Painting a Line Set by Dragging the Mouse

查看:96
本文介绍了拖动鼠标绘制线组时出现滞后的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写了一个applet,允许用户设置背景颜色,然后单击"pen"颜色,然后在窗口框架中单击并绘制一条线.我通过在拖动鼠标的每个x和y点填充椭圆形来设置这条线.我还允许用户使用-和+键通过增加或减小椭圆的半径来更改线的大小.我的问题是,在绘制线条时会有一些滞后的原因.我相信这是在mouseDrag方法中执行的,该方法的执行速度限制了可以在其上绘制椭圆的x和y点的数量.有什么办法可以减轻这种滞后现象,从而使线条更好些?

I have written an applet in Java that allows the user to set background color, and "pen" color then click and draw in the window frame to drawn a line. I set this line by filling ovals at each x and y point of the dragging mouse. I also allows user to use the - and + keys to change the size of the line by increasing or decreasing the radius of the ovals. My issue is that there is some cause of lag in the drawing of the line. I believe it is in the mouseDrag method and the speed at which that method is executed limits how many x and y points can be attained to paint ovals on. Is there any way to alleviate this lag to allow for better lines?

import java.applet.Applet;
import java.awt.Event;
import java.awt.Color;
import java.awt.Graphics;

public class Screen extends Applet
{
    int x = -20, y = -20;
    int height = 20, width = 20;
    boolean black, blue, cyan, green, magenta, orange, pink, red, white, yellow;

    public void init()
    {
    setSize(400,400);
    setBackground(Color.white);
    }
    public void paint(Graphics g)
    {
    if (black == true)
        g.setColor(Color.BLACK);
    else if (blue == true)
        g.setColor(Color.BLUE);
    else if (cyan == true)
        g.setColor(Color.CYAN);
    else if (green == true)
        g.setColor(Color.GREEN);
    else if (magenta == true)
        g.setColor(Color.MAGENTA);
    else if (orange == true)
        g.setColor(Color.ORANGE);
    else if (pink == true)
        g.setColor(Color.PINK);
    else if (red == true)
        g.setColor(Color.RED);
    else if (white == true)
        g.setColor(Color.WHITE);
    else if (yellow == true)
        g.setColor(Color.YELLOW);

    g.fillOval(x, y, width, height);
    }
    public boolean mouseDrag(Event e, int xPos, int yPos)
    {
    x = xPos;
    y = yPos;
    paint(getGraphics());
    return true;
    }
    public boolean keyDown(Event e, int key)
    {
    black = false;
    blue = false;
    cyan = false;
    green = false;
    magenta = false;
    orange = false;
    pink = false;
    red = false;
    white = false;
    yellow = false;

    if (key == 'n')
    {
        x =-20;
        y =-20;
        update(getGraphics());
    }
    else if (key == 'x')
        black = true;
    else if (key == 'b')
        blue = true; 
    else if (key == 'c')
        cyan = true;
    else if (key == 'g')
        green = true;
    else if (key == 'm')
        magenta = true;
    else if (key == 'o')
        orange = true;
    else if (key == 'p')
        pink = true;
    else if (key == 'r')
        red = true;
    else if (key == 'w')
        white = true;
    else if (key == 'y')
        yellow = true;
    else if (key == '+')
    {
        height += 5;
        width += 5;
    }
    else if (key == '-')
    {
        height += -5;
        width += -5;
    }
    else if (key == 'X')
        setBackground(Color.BLACK);
    else if (key == 'B' )
        setBackground(Color.BLUE); 
    else if (key == 'C')
        setBackground(Color.CYAN);
    else if (key == 'G')
        setBackground(Color.GREEN);
    else if (key == 'M')
        setBackground(Color.MAGENTA);
    else if (key == 'O')
        setBackground(Color.ORANGE);
    else if (key == 'P')
        setBackground(Color.PINK);
    else if (key == 'R')
        setBackground(Color.RED);
    else if (key == 'W')
        setBackground(Color.WHITE);
    else if (key == 'Y')
        setBackground(Color.YELLOW);
    return true;
    }
}

推荐答案

您的绘画看起来值得怀疑:

Your painting looks questionable:

  • 您正在直接呼叫paint(...),这绝对不应该做.
  • 您不会在绘画替代中调用super.paint(...)方法.
  • 尝试最小化您的GUI,然后还原它,并告诉我您的绘图消失了多少.
  • You're calling paint(...) directly, something that should never be done.
  • You're not calling the super.paint(...) method inside of your paint override.
  • Try minimizing your GUI and then restoring it, and tell me how much of your drawing disappears into the nothingness.

相反,您应该:

    如果要建议绘制GUI,请
  • 致电repaint()
  • 在绘画覆盖中调用super方法
  • 创建一个椭圆形(也许是Ellipse2D)的集合,并通过for循环在您的绘制方法中对其进行绘制.
  • 删除所有这些AWT代码,然后使用JPanel的paintComponent(...)方法绘制Swing.
  • 而不是绘制单个椭圆,而是绘制连接点的线.您可以通过更改描边来更改厚度.
  • 将绘制的每个曲线保存到BufferedImage上,然后使用paintComponent方法绘制该图像.
  • call repaint() when you want to suggest that the GUI be painted
  • call the super method in your painting override
  • create a collection of ovals, perhaps Ellipse2D, and paint them in your painting method via a for loop.
  • Chuck all this AWT code and instead draw in Swing in a JPanel's paintComponent(...) method.
  • Rather than draw individual ellipses, draw lines connecting points. You can change thickness by changing the Stroke.
  • Save each curve drawn onto a BufferedImage which is then drawn in the paintComponent method.

修改:
有关完整的示例,请在此处查看我的答案:更改JPanel图形g的颜色绘制线

Edit:
For a complete example, please see my answer here: Changing JPanel Graphics g color drawing line

这篇关于拖动鼠标绘制线组时出现滞后的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆