简体   繁体   English

在Java中绘制矩形时如何防止刷新

[英]How to prevent flushing when draw a rectangle in Java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  
import java.util.List;
import java.util.ArrayList;

class DrawningBoard extends Canvas implements MouseMotionListener{
      private List<Point> points = new ArrayList<>();
      private List<List<Point>> curves = new ArrayList<>();
      private boolean isRectangle = false;
      private int xr, yr, widthr, heightr;

public void setIsRectangle(boolean trueOrFalse){
    isRectangle = trueOrFalse;
}
public boolean getIsRectangle(){
    return isRectangle;
}

public DrawningBoard(){
    setBackground(Color.MAGENTA);
    addMouseMotionListener(this); 
    addMouseListener(new MouseAdapter(){
        public void mouseReleased(MouseEvent e){
            curves.add(points);
            points = new ArrayList<>();
        }
        public void mousePressed(MouseEvent e){
            points.add(new Point(e.getX(), e.getY()));
        }
    });
}

public void paint(Graphics g){
    g.setColor(Color.CYAN);
    if(isRectangle){
        g.drawRect(xr, yr, widthr, heightr);
    }
    for(List<Point> curve : curves){
        for(int i = 0; i < curve.size() - 1; i++){
            g.drawLine((int)curve.get(i).getX(), (int)curve.get(i).getY(), (int)curve.get(i + 1).getX(), (int)curve.get(i + 1).getY());
        }
    }
}

public void mouseDragged(MouseEvent e){  
    Graphics g = getGraphics();
    g.setColor(Color.CYAN);
    
    int xx = e.getX();
    int yy = e.getY();
    
    int x = (int)points.get(points.size() - 1).getX();
    int y = (int)points.get(points.size() - 1).getY();
    int dx = xx-x;
    int dy = yy-y;

    if(isRectangle){
        if(dx >= 0 && dy >= 0){
            xr = x;
            yr = y;
            widthr = dx;
            heightr = dy;
        } else if(dx < 0 && dy < 0){
            xr = xx;
            yr = yy;
            widthr = -dx;
            heightr = -dy;
        } else if(dx >= 0 && dy < 0){
            xr = x;
            yr = yy;
            widthr = dx;
            heightr = -dy;
        } else if(dx < 0 && dy >= 0){
            xr = xx;
            yr = y;
            widthr = -dx;
            heightr = dy;
        }
        repaint();
    } 
    else {
        g.drawLine(xx, yy, (int)points.get(points.size() - 1).getX(), (int)points.get(points.size() - 1).getY());
        points.add(new Point(xx, yy));
    }
    
}

public void mouseMoved(MouseEvent e) { }  
}

class GUI extends JFrame implements ActionListener{
private JPanel[] panel;
private JButton[] button;
private DrawningBoard board;

public GUI(String title){
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    setSize(416, 400);
    
    board = new DrawningBoard();
    board.setBounds(0,0, 400, 400);
    
    panel = new JPanel[3];
    panel[0] = new JPanel();
    panel[1] = new JPanel();
    panel[2] = new JPanel();
    panel[0].setLayout(null);
    panel[1].setLayout(null);
    panel[2].setLayout(null);
    panel[0].setBounds(0, 0, 400, 20);
    panel[1].setBounds(0, 20, 400, 400);
    panel[2].add(panel[0]);
    panel[2].add(panel[1]);
    panel[0].setBackground(Color.red);
    
    button = new JButton[5];
    button[0] = new JButton("Rectangle");
    button[1] = new JButton("b1");
    button[2] = new JButton("b2");
    button[3] = new JButton("b3");
    button[4] = new JButton("b4");
    button[0].addActionListener(this);
    button[3].addActionListener(this);

    button[0].setBounds(0, 0, 100, 20);
    button[1].setBounds(100, 0, 100, 20);
    button[2].setBounds(200, 0, 100, 20);
    button[3].setBounds(300, 0, 100, 20);
    button[4].setBounds(0, 0, 100, 20);
    
    panel[0].add(button[0]);
    panel[0].add(button[1]);
    panel[0].add(button[2]);
    panel[0].add(button[3]);
    panel[0].add(button[4]);
    
    panel[1].add(board);

    add(panel[2]);  
    
    show();
}
public void actionPerformed(ActionEvent e){
    String command = e.getActionCommand();
    if(command.equals("Rectangle")) 
        board.setIsRectangle(!board.getIsRectangle());
}
}


public class Paint{
public static void main(String []str){
    new GUI("Paint");
}
}

I want to create a Paint application.我想创建一个 Paint 应用程序。 If I draw multiple curves on the board and then I want to draw a如果我在板上画了多条曲线,然后我想画一个
rectangle (using drawRect(x, y, width, height) ), those curves are repainted and results some flushing as result of use repaint() method.矩形(使用drawRect(x, y, width, height) ),这些曲线被重新绘制并由于使用repaint()方法而导致一些刷新。 How can I avoid that flushing?我怎样才能避免这种潮红?
I tried to use update() method, but many rectangles are drawn when mouse dragging.我尝试使用update()方法,但是在鼠标拖动时绘制了许多矩形。

Swing is double buffered by default.默认情况下,Swing 是双缓冲的。

public void paint(Graphics g){

Don't override paint().不要覆盖paint()。 You have done this incorrectly and have lost the benefit of double buffering.你做错了,失去了双缓冲的好处。

Instead, custom painting should be done by overriding the paintComponent(...) method.相反,自定义绘制应该通过覆盖paintComponent(...)方法来完成。 Read the section from the Swing tutorial on Custom Painting for more information and working examples.有关更多信息和工作示例,请阅读 Swing 教程中关于自定义绘画的部分。

You can also check out the DrawOnComponent example from Custom Painting Approaches for a more complete example that does what you want.您还可以查看自定义绘画方法中DrawOnComponent示例,以获得更完整的示例, DrawOnComponent您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM