user8738397
user8738397

Reputation:

g.drawRect draws the rectangle behind background

I want to make a clear method which basically clears the screen. I tried doing repaint(); which I saw worked in another code. Now I'm trying to do g.drawRect but it draws it behind the background (what the program looks like).

I made a clear method which I tried to change the background color which didn't work, and then I tried to make a red rectangle to cover the window, but both didn't work.

    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);

}

Heres the full code:

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);
    }
}

Upvotes: 0

Views: 1094

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

This...

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

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


  }

Is not how painting works in Swing. You need to stop and take a read through Performing Custom Painting and Painting in AWT and Swing to better understand how painting works in Swing

One, very simple, solution might be to simply have a "cleared" variable, which changes the way paintComponent works, for example...

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;
  }
}

This does, however, make the painting process somewhat complicated.

Another approach might be to remove all the elements from the old_paths List.

Another approach might be to use a BufferedImage as your primary rendering surface and then simply paint it to the component instead

Upvotes: 1

Related Questions