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

Java 鼠标拖动时绘制线时出现的问题,java,graphics,applet,awt,java-2d,Java,Graphics,Applet,Awt,Java 2d,我正在开发一个绘制不同形状的绘图小程序。我想在拖动鼠标时画线。问题是,当线条出现时,它们如下图所示 我有一条使用一个点(起点)构建的类线 它有一个名为setDragPoint的方法,该方法在拖动时使用鼠标拖动点绘制线条,drawingImage在拖动模式下绘制时会产生太多闪烁。为什么会这样 import java.applet.*; import java.awt.event.*; import java.awt.*; import java.util.*; public class Pai

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

我有一条使用一个点(起点)构建的类线 它有一个名为
setDragPoint
的方法,该方法在拖动时使用鼠标拖动点绘制线条,
drawingImage
在拖动模式下绘制时会产生太多闪烁。为什么会这样

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) {
   }
 }
import java.applet.*;
导入java.awt.event.*;
导入java.awt.*;
导入java.util.*;
公共类PaintBrush扩展小程序实现MouseListener、MouseMotionListener{
形状;
点起点;
点牵引点;
阵列列表形状;
选择形状;
颜色选择;
选择与选择;
图像绘制图像;
图形绘制图形;
字符串形状字符串、彩色字符串、填充字符串;
布尔码;
公共void init(){
形状=新的ArrayList();
shapeChoice=新选项();
shapeChoice.addItem(“行”);
shapeChoice.addItem(“矩形”);
shapeChoice.addItem(“RoundRect”);
shapeChoice.addItem(“椭圆形”);
shapeChoice.addItem(“徒手”);
添加(shapeChoice);
colorChoice=新选项();
颜色选择。添加项(“红色”);
颜色选择。添加项(“绿色”);
颜色选择。添加项(“蓝色”);
添加(颜色选择);
fillChoice=新选项();
fillChoice.addItem(“已填充”);
fillChoice.addItem(“空心”);
添加(填充选择);
shapeString=shapeChoice.getSelectedItem();
colorString=colorChoice.getSelectedItem();
fillString=fillChoice.getSelectedItem();
drawingImage=createImage(getSize().width,getSize().height);
drawGraphics=drawingImage.getGraphics();
System.out.println(“设置图像”);
drawGraphics.setColor(颜色:黑色);
drawGraphics.fillRect(0,0,getSize().width,getSize().height);
drawGraphics.setColor(颜色为橙色);
drawGraphics.drawRect(0,0,getSize().width-1,getSize().height-1);
drawGraphics.drawRect(1,1,getSize().width-3,getSize().height-3);
起始点=新点(0,0);
拖动点=新点(0,0);
addMouseListener(这个);
addMouseMotionListener(此);
}
公共无效鼠标事件(鼠标事件e){
}
公共无效mouseExited(MouseEvent e){
}
公共无效mouseClicked(MouseEvent e){
}
公共无效鼠标按下(MouseEvent e){
系统输出打印项次(“按下”);
startPoint.x=e.getX();
startPoint.y=e.getY();
重新油漆();
交换机(shapeString){
案例“行”:
shape=新线(startPoint.x,startPoint.y);//步骤1这里我使用起点(按下鼠标的点)构造一条新线
打破
“徒手”一案:
形状=新的自由形状();
打破
}
}
公共无效MouseEvent(MouseEvent e){
if(isdFragmode){
形状。添加(形状);
isdFragmode=false;
}
重新油漆();
}
public void mouseMoved(MouseEvent e){
}
公共无效鼠标标记(鼠标事件e){
System.out.println(“拖动”);
IsdFragmode=true;
dragPoint.x=e.getX();
dragPoint.y=e.getY();
交换机(shapeString){
案例“行”:
shape.setDragPoint(dragPoint.x,dragPoint.y);//在这里,我在步骤1中将拖动点设置为已创建的线
打破
“徒手”一案:
形状=新的自由形状();
打破
}
shape.drawWhileDragging(drawGraphics);//我调用此方法在鼠标拖动时进行绘制
重新油漆();
}
公共空间涂料(图g){
更新(g);
}
公共空间更新(图g){
//创建屏幕外图形绘制环境(如果没有)
//存在
//或者如果用户将小程序绘图区域的大小调整为不同的大小
//大小
如果(drawingImage==null)
{
System.out.println(“图像为空”);
drawingImage=createImage(getSize().width,getSize().height);
drawGraphics=drawingImage.getGraphics();
}
//删除上一个图像
drawGraphics.setColor(颜色:黑色);
drawGraphics.fillRect(0,0,getSize().width,getSize().height);
drawGraphics.setColor(颜色为橙色);
drawGraphics.drawRect(0,0,getSize().width-1,getSize().height-1);
drawGraphics.drawRect(1,1,getSize().width-3,getSize().height-3);
用于(形状s:形状)
s、 绘图(绘图);
//将屏幕外图像绘制到小程序查看窗口
g、 drawImage(drawingImage,0,0,this);
}
}
抽象类形状{
颜色形状颜色;
布尔填充;
抽象空图(图g);
拉丝时的空隙(图g){
}
void setDragPoint(整数x,整数y){
}
}
类线延伸形状{
专用点起始点;
专用点-当前点;
公共点getStartPoint(){
返回起始点;
}
公共点getCurrentPoint(){
回流点;
}
公共无效设置起点(点){
this.startPoint=点;
}
公共无效设置电流点(点){
this.currentPoint=点;
}
拉丝时的空隙(图g){
g、 绘制线(起点.x,起点.y,当前点.x,当前点.y);
}
公共空间绘制(图g){
g、 绘制线(起点.x,起点.y,当前点.x,当前点.y);
}
第()行{
起始点=新点(0,0);
currentPoint=新点(0,0);
}
行(int x1,int y1){
这个();
这个.startPoint.x=x1;
这个.startPoint.y=y1;
}
void setDragPoint(整数x,整数y){
此.currentPoint.x=x;
此.currentPoint.y=y;
System.out.println(“电流-X:+currentPoint.X+“currentPoint-Y”+currentPoint.Y”);
System.out.println(“start-X:+startPoint.X+“startPoint-Y”+startPoint.Y”);
}
}
类FreeShape扩展了形状{
私有ArrayList dragPoints=新ArrayList();
公共阵列列表getDragPoints(){
返回拖点数;
}
公共无效设置DragPoints(点){
拖动点。添加(点);
}
公共空间绘制(图g){
}
公共自由形状(){
}
}
类矩形扩展形状{
公共空间绘制(图g){
}
}
    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
    }
    void drawWhileDragging(Graphics g) {
        g.setColor(Color.ORANGE);
        g.drawLine(startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
        g.setColor(Color.BLACK);
    }
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) {
        }
    }
}