首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >鼠标拖动时绘制线条时出现的问题

鼠标拖动时绘制线条时出现的问题
EN

Stack Overflow用户
提问于 2012-12-29 13:12:02
回答 2查看 7K关注 0票数 4

我正在开发一个绘制不同形状的画图小程序。我想在拖动鼠标的同时绘制线条。问题是,当线条出现时,它们如下图所示。

我有一个用一个点(起始点)构造的类线,它有一个名为setDragPoint的方法,它使用鼠标拖动点来绘制线条,同时drawingImage在拖动模式下绘制时会产生太多的闪烁。为什么会发生这种情况?

代码语言:javascript
复制
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class PaintBrush extends Applet implements MouseListener, MouseMotionListener {

Shape shape;
Point startPoint;
Point dragPoint;
ArrayList<Shape> shapes;
Choice shapeChoice;
Choice colorChoice;
Choice fillChoice;
Image drawingImage;
Graphics drawGraphics;
String shapeString, colorString, fillString;
boolean isDragMode;

public void init() {
    shapes = new ArrayList<Shape>();
    shapeChoice = new Choice();
    shapeChoice.addItem("Line");
    shapeChoice.addItem("Rectangle");
    shapeChoice.addItem("RoundRect");
    shapeChoice.addItem("Oval");
    shapeChoice.addItem("FreeHand");

    add(shapeChoice);

    colorChoice = new Choice();
    colorChoice.addItem("Red");
    colorChoice.addItem("Green");
    colorChoice.addItem("Blue");

    add(colorChoice);

    fillChoice = new Choice();
    fillChoice.addItem("Filled");
    fillChoice.addItem("Hollow");
    add(fillChoice);

    shapeString = shapeChoice.getSelectedItem();
    colorString = colorChoice.getSelectedItem();
    fillString = fillChoice.getSelectedItem();

    drawingImage = createImage(getSize().width, getSize().height);
    drawGraphics = drawingImage.getGraphics();
    System.out.println("set up image");
    drawGraphics.setColor(Color.black);
    drawGraphics.fillRect(0, 0, getSize().width, getSize().height);
    drawGraphics.setColor(Color.orange);
    drawGraphics.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
    drawGraphics.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
    startPoint = new Point(0, 0);
    dragPoint = new Point(0, 0);
    addMouseListener(this);
    addMouseMotionListener(this);
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {

    System.out.println("Pressed");

    startPoint.x = e.getX();
    startPoint.y = e.getY();
    repaint();

    switch (shapeString) {
        case "Line":
            shape = new Line(startPoint.x, startPoint.y);  //step 1 here i construct a new line using the start point (the point at which the mouse is pressed)

            break;
        case "FreeHand":
            shape = new FreeShape();
            break;
    }


    }

public void mouseReleased(MouseEvent e) {
    if (isDragMode) {
        shapes.add(shape);
        isDragMode = false;
     }
    repaint();

}

public void mouseMoved(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
    System.out.println("Dragged");
    isDragMode = true;
    dragPoint.x = e.getX();
    dragPoint.y = e.getY();

    switch (shapeString) {
        case "Line":
            shape.setDragPoint(dragPoint.x, dragPoint.y);  //here i set the drag points to the already created line at step 1 
              break;
        case "FreeHand":
            shape = new FreeShape();
            break;
    }

    shape.drawWhileDragging(drawGraphics); // i call this method to draw while mouse is dragging

    repaint();


}

public void paint(Graphics g) {

  update(g);
}
 public void update(Graphics g) {

  // create an off-screen graphics drawing environment if none
  //existed
  // or if the user resized the applet drawing area to a different
 // size
   if (drawingImage == null)
{

System.out.println("Image is Null");
    drawingImage = createImage(getSize().width,getSize().height);
drawGraphics = drawingImage.getGraphics();
}



  // erase the previous image
  drawGraphics.setColor(Color.black);
  drawGraphics.fillRect(0,0,getSize().width,getSize().height);
  drawGraphics.setColor(Color.orange);
  drawGraphics.drawRect(0,0,getSize().width-1,getSize().height-1);
  drawGraphics.drawRect(1,1,getSize().width-3,getSize().height-3);  

   for(Shape s:shapes)
         s.draw(drawGraphics);

  // paint the offscreen image to the applet viewing window
  g.drawImage(drawingImage,0,0,this);

   }
 }


abstract class Shape {

Color shapeColor;
boolean filled;

abstract void draw(Graphics g);

void drawWhileDragging(Graphics g) {
}

void setDragPoint(int x, int y) {
}
}

 class Line extends Shape {

private Point startPoint;
private Point currentPoint;

public Point getStartPoint() {
    return startPoint;
}

public Point getCurrentPoint() {
    return currentPoint;
}

public void setStartPoint(Point point) {
    this.startPoint = point;
}

public void setCurrentPoint(Point point) {
    this.currentPoint = point;
}

void drawWhileDragging(Graphics g) {
    g.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y); 
}

public void draw(Graphics g) {
    g.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
}

Line() {
    startPoint = new Point(0, 0);
    currentPoint = new Point(0, 0);
}

Line(int x1, int y1) {
    this();
    this.startPoint.x = x1; 
    this.startPoint.y = y1;
}

void setDragPoint(int x, int y) {
    this.currentPoint.x = x;
    this.currentPoint.y = y;
    System.out.println("Current-X:" + currentPoint.x + " currentPoint-Y" + currentPoint.y);
    System.out.println("start-X:" + startPoint.x + " startPoint-Y" + startPoint.y);
  }

 }

class FreeShape extends Shape {

private ArrayList<Point> dragPoints = new ArrayList<Point>();

public ArrayList<Point> getDragPoints() {
    return dragPoints;
}

public void setDragPoints(Point point) {
    dragPoints.add(point);
}

public void draw(Graphics g) {
}

public FreeShape() {
  }
}


class Rectangle extends Shape {

public void draw(Graphics g) {
   }
 }


class Oval extends Shape {

public void draw(Graphics g) {
   }
 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-29 14:01:32

我最近写了一个类似类型的应用程序。这是屏幕截图。正如您所看到的,它还没有完全发展起来。

现在,我也面临着和你现在面临的类似的问题。你要做的是。

  • Double Buffer所有绘制操作
  • 不会通过调用repaint来清除屏幕。实际上,Repaint首先用背景颜色填充屏幕&这就是您所看到的闪烁。

您可以在Image中制作当前屏幕画布的副本。Image将在每次绘图操作后更新。因此,您要做的不是通过调用repaint来清除屏幕,而是在画布上绘制Image。这就像是双缓冲。

每次拖动鼠标时,您都会在代码中调用 repaint 。这就是闪烁的原因。

更新

我在新更新的代码中发现了三个主要问题

  • drawWhileDragging方法中,您不会更改线条图形上下文绘制颜色。所以这条线实际上是用黑色绘制的,你的背景也是黑色的。结果你看不到任何东西。
  • 在同样的方法中,你正在传递一个drawingImage的图形上下文(即引用)。因此,这条线实际上是在屏幕外的图像上绘制的,而不是在屏幕上绘制的。在mouseDragged方法中,每次拖动后都会调用
  • 。结果,实际上没有任何东西被绘制为

我已经在我的机器上运行了你的代码,并做了必要的更改。我只发布更改后的方法,以保持简短。

下面是更新后的mouseDragged方法

代码语言:javascript
复制
    public void mouseDragged(MouseEvent e) {
        System.out.println("Dragged");
        isDragMode = true;
        dragPoint.x = e.getX();
        dragPoint.y = e.getY();

        switch (shapeString) {
            case "Line":
                shape.setDragPoint(dragPoint.x, dragPoint.y);  //here i set the drag points to the already created line at step 1
                break;
            case "FreeHand":
                shape = new FreeShape();
                break;
        }

        getGraphics().drawImage(drawingImage, 0,0,null); //Added this line
        shape.drawWhileDragging(getGraphics()); // i call this method to draw while mouse is dragging
    }

下面是更新后的drawWhileDragging方法

代码语言:javascript
复制
    void drawWhileDragging(Graphics g) {
        g.setColor(Color.ORANGE);
        g.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
        g.setColor(Color.BLACK);
    }

嗯,我已经把颜色设为橙色了。你要做的就是根据Choice菜单设置颜色。

您也可以实现一个类似的类比来绘制其他形状。

票数 1
EN

Stack Overflow用户

发布于 2012-12-29 15:16:28

你遇到的问题归结为你是如何“尝试”执行双缓冲的。与其在更新线位置时清除后台缓冲区,不如简单地连续绘制到它-有点像在黑板上绘制。

相反,您需要清除图形内容并重新应用更新-令人惊讶的是,这是paint实际工作的一部分,但您从不调用super.paint

很少需要覆盖顶级容器,也不应该很少需要覆盖顶级容器的paint方法。抛开其他所有东西不谈,没有双重缓冲。

取而代之的是,使用像JPanel这样的东西。

我已经更新了您的示例,不包括颜色。颜色实际上应该由形状保持,并在绘制时应用。

代码语言:javascript
复制
public class BadPaint06 extends JApplet {

    public void init() {
        setLayout(new BorderLayout());
    }

    @Override
    public void start() {
        add(new PaintPane());
    }

    public class PaintPane extends JPanel implements MouseListener, MouseMotionListener {

        Shape shape;
        Point startPoint;
        Point dragPoint;
        ArrayList<Shape> shapes;
        Choice shapeChoice;
        Choice colorChoice;
        Choice fillChoice;
        Image drawingImage;
        Graphics drawGraphics;
        String shapeString, colorString, fillString;
        boolean isDragMode;

        public PaintPane() {
            shapes = new ArrayList<Shape>();
            shapeChoice = new Choice();
            shapeChoice.addItem("Line");
            shapeChoice.addItem("Rectangle");
            shapeChoice.addItem("RoundRect");
            shapeChoice.addItem("Oval");
            shapeChoice.addItem("FreeHand");

            add(shapeChoice);

            colorChoice = new Choice();
            colorChoice.addItem("Red");
            colorChoice.addItem("Green");
            colorChoice.addItem("Blue");

            add(colorChoice);

            fillChoice = new Choice();
            fillChoice.addItem("Filled");
            fillChoice.addItem("Hollow");
            add(fillChoice);

            shapeString = shapeChoice.getSelectedItem();
            colorString = colorChoice.getSelectedItem();
            fillString = fillChoice.getSelectedItem();

            startPoint = new Point(0, 0);
            dragPoint = new Point(0, 0);
            addMouseListener(this);
            addMouseMotionListener(this);
        }

        @Override
        public void invalidate() {

            drawingImage = null;

            super.invalidate(); 

        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mouseClicked(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {

            startPoint.x = e.getX();
            startPoint.y = e.getY();
            repaint();

            switch (shapeString) {
                case "Line":
                    shape = new Line(startPoint.x, startPoint.y);  //step 1 here i construct a new line using the start point (the point at which the mouse is pressed)

                    break;
                case "FreeHand":
                    shape = new FreeShape();
                    break;
            }


        }

        public void mouseReleased(MouseEvent e) {
            if (isDragMode) {
                shapes.add(shape);
                isDragMode = false;
            }
            repaint();

        }

        public void mouseMoved(MouseEvent e) {
        }

        public void mouseDragged(MouseEvent e) {
            System.out.println("Dragged");
            isDragMode = true;
            dragPoint.x = e.getX();
            dragPoint.y = e.getY();

            switch (shapeString) {
                case "Line":
                    shape.setDragPoint(dragPoint.x, dragPoint.y);  //here i set the drag points to the already created line at step 1 
                    break;
                case "FreeHand":
                    shape = new FreeShape();
                    break;
            }

            repaint();


        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            System.out.println("In Paint");
            if (drawingImage == null) {
                drawingImage = createImage(getSize().width, getSize().height);
                drawGraphics = drawingImage.getGraphics();
                System.out.println("set up image");
                drawGraphics.setColor(Color.black);
                drawGraphics.fillRect(0, 0, getSize().width, getSize().height);
                drawGraphics.setColor(Color.orange);
                drawGraphics.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
                drawGraphics.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
                drawGraphics.dispose();
            }

            g.drawImage(drawingImage, 0, 0, this);

            for (Shape shape : shapes) {
                shape.draw(g);
            }


            if (shape != null) {
                shape.drawWhileDragging(g); // i call this method to draw while mouse is dragging
            }

        }
    }

    abstract class Shape {

        Color shapeColor;
        boolean filled;

        abstract void draw(Graphics g);

        void drawWhileDragging(Graphics g) {
        }

        void setDragPoint(int x, int y) {
        }
    }

    class Line extends Shape {

        private Point startPoint;
        private Point currentPoint;

        public Point getStartPoint() {
            return startPoint;
        }

        public Point getCurrentPoint() {
            return currentPoint;
        }

        public void setStartPoint(Point point) {
            this.startPoint = point;
        }

        public void setCurrentPoint(Point point) {
            this.currentPoint = point;
        }

        void drawWhileDragging(Graphics g) {
            if (currentPoint != null) {
                g.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
            }
        }

        public void draw(Graphics g) {
            if (currentPoint != null) {
                g.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
            }
        }

        Line() {
            startPoint = new Point(0, 0);
//            currentPoint = new Point(0, 0);
        }

        Line(int x1, int y1) {
            this();
            this.startPoint.x = x1;
            this.startPoint.y = y1;
        }

        void setDragPoint(int x, int y) {
            currentPoint = new Point(x, y);
//            this.currentPoint.x = x;
//            this.currentPoint.y = y;
            System.out.println("Current-X:" + currentPoint.x + " currentPoint-Y" + currentPoint.y);
            System.out.println("start-X:" + startPoint.x + " startPoint-Y" + startPoint.y);
        }
    }

    class FreeShape extends Shape {

        private ArrayList<Point> dragPoints = new ArrayList<Point>();

        public ArrayList<Point> getDragPoints() {
            return dragPoints;
        }

        public void setDragPoints(Point point) {
            dragPoints.add(point);
        }

        public void draw(Graphics g) {
        }

        public FreeShape() {
        }
    }

    class Rectangle extends Shape {

        public void draw(Graphics g) {
        }
    }

    class Oval extends Shape {

        public void draw(Graphics g) {
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14078882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档