g.drawRect在背景后面绘制矩形

时间:2018-03-26 21:31:22

标签: java swing graphics graphics2d

我想制作一个基本清除屏幕的清晰方法。我试过做repaint();我看到的是另一个代码。现在我尝试g.drawRect,但它将它绘制在背景之后(what the program looks like)。

我做了一个明确的方法,我试图改变那些没有用的背景颜色,然后我试着用一个红色的矩形覆盖窗户,但两者都没有用。

    public void clear(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.setBackground(Color.RED);
    g2d.setColor(Color.RED);
    g2d.drawRect(0, 0, 640, 480);

}


@Override
public void paintComponent(Graphics gfx) {
    super.paintComponent(gfx);
    Graphics2D g = (Graphics2D) gfx;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    g.setColor(Color.BLACK);
    g.drawString(welcome, 10, 20);

    g.setColor(Color.RED);
    g.setStroke(new BasicStroke(2));        
    for (GeneralPath old_path : old_paths ) {
        g.draw(old_path);
    }

    g.setColor(Color.GREEN);
    g.setStroke(new BasicStroke(2));
    g.draw(p);   
    clear(g);

}

下面是完整的代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Drawing extends JPanel implements MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;

    GeneralPath p=new GeneralPath();

    String welcome = "Welcome";

    LinkedList<GeneralPath> old_paths= new LinkedList<GeneralPath>();


    public Drawing() {


        try
        {
        setCursor(Toolkit.getDefaultToolkit().createCustomCursor(ImageLoader.loadImage("218.png"),new Point(10,10),"custom cursor"));
        }catch(Exception e){}


        addMouseListener(this);
        addMouseMotionListener(this);

    }

    public void clear(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setBackground(Color.RED);
        g2d.setColor(Color.RED);
        g2d.drawRect(0, 0, 640, 480);

    }


    @Override
    public void paintComponent(Graphics gfx) {
        super.paintComponent(gfx);
        Graphics2D g = (Graphics2D) gfx;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.setColor(Color.BLACK);
        g.drawString(welcome, 10, 20);

        g.setColor(Color.RED);
        g.setStroke(new BasicStroke(2));        
        for (GeneralPath old_path : old_paths ) {
            g.draw(old_path);
        }

        g.setColor(Color.GREEN);
        g.setStroke(new BasicStroke(2));
        g.draw(p);   
        clear(g);

    }

    @Override
    public void mouseClicked(MouseEvent event)
    {

      if (event.getClickCount() == 2) {
          System.out.println("clicked");
          Graphics g = getGraphics();

          clear(g);
          g.setColor(Color.RED);
          g.drawRect(0, 0, 640, 480);


      }
    }

    @Override
    public void mouseEntered(MouseEvent me) {

    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void mousePressed(MouseEvent me) {
        old_paths.add(p);
        p=new GeneralPath();
        p.moveTo(me.getX(), me.getY());
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        p.lineTo(me.getX(), me.getY());
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent me) {        
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Drawing());
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

此...

if (event.getClickCount() == 2) {
      System.out.println("clicked");
      Graphics g = getGraphics();

      clear(g);
      g.setColor(Color.RED);
      g.drawRect(0, 0, 640, 480);


  }

不是绘画如何在Swing中起作用。您需要停下来阅读Performing Custom PaintingPainting in AWT and Swing以更好地了解绘画在Swing中的工作原理

一个非常简单的解决方案可能就是简单地清除&#34;清除&#34;变量,它改变了paintComponent的工作方式,例如......

private boolean cleared = false;

@Override
public void paintComponent(Graphics gfx) {
    super.paintComponent(gfx);
    Graphics2D g = (Graphics2D) gfx;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    if (cleared) {
    } else {    
        g.setColor(Color.BLACK);
        g.drawString(welcome, 10, 20);

        g.setColor(Color.RED);
        g.setStroke(new BasicStroke(2));        
        for (GeneralPath old_path : old_paths ) {
            g.draw(old_path);
        }

        g.setColor(Color.GREEN);
        g.setStroke(new BasicStroke(2));
        g.draw(p);   
    }

}

@Override
public void mouseClicked(MouseEvent event)
{

  if (event.getClickCount() == 2) {
      cleared = true;
  }
}
然而,这确实使绘画过程有点复杂。

另一种方法可能是从old_paths List删除所有元素。

另一种方法可能是使用BufferedImage作为主渲染表面,然后简单地将其绘制到组件