1

I Have 3 Questing Regarding My Code 1==> How to remove Selected Shape in my code; when i right click every shape is deleted, 2==> I TOTALLY don't know how to highlight the overlapped are 3==> right now when i click on my JPanel, shape drawn from the point where mouse clicked, where as it should be the in the center of the mouse pointer

Thanks In advance actually i'm new to Java. this is my code,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.JPanel;
import javax.swing.JButton;

import Delete.Selection;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.util.ArrayList;

public class MyPanel extends JPanel {

    ArrayList<MyRect> list = new ArrayList<MyRect>();



    public MyPanel() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {


                if(e.getButton() == MouseEvent.BUTTON1) {
                    MyRect r = new MyRect(e.getX(), e.getY());
                    list.add(r);

                    repaint();
                    }


                    else {
                        list.clear();
                        repaint();
                    }
                } 

            }
        );
        setPreferredSize(new Dimension(600, 400));
        setBackground(Color.CYAN);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);



        for (int i = 0; i < list.size(); i++) {
            MyRect r = list.get(i);
            g.fillRect(r.x, r.y, r.w, r.h);
        }   
    }

class MyRect {
    int x, y, w=100, h=100;
    Color c = Color.BLACK;

    public MyRect(int x, int y, int w, int h, Color color) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.c = color;

    }

    public MyRect(int x, int y) {
        this.x = x;
        this.y = y;
    }


}}
  • 1
    Something like [this](http://stackoverflow.com/questions/22160245/intersection-of-two-rectangles-with-paintcomponent/22160470#22160470) or [this](http://stackoverflow.com/questions/15514738/java-help-overlapping-rectangles-that-paint-a-new-color-where-the-rectangles-ov/15514834#15514834) or [this](http://stackoverflow.com/questions/20927189/detecting-collision-of-two-sprites-that-can-rotate/20928531#20928531)? – MadProgrammer Oct 29 '15 at 22:00
  • @MadProgrammer . Yes Sir Something Like This, – Mohassan Noon Oct 29 '15 at 22:07

1 Answers1

2

how to highlight the overlapped area

You can use the intersection(...) method of the Rectangle class to get a Rectangle to paint:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class IntersectingRectangles extends JPanel
{
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();

        Dimension d = getSize();
        int width = d.width * 3 / 4;
        int height = d.height * 3 / 4;

        Rectangle r1 = new Rectangle(0, 0, width, height);
        g2d.setColor( Color.BLUE );
        g2d.fill( r1 );

        Rectangle r2 = new Rectangle(d.width - width, d.height - height, width, height);
        g2d.setColor( Color.YELLOW );
        g2d.fill( r2 );

        //  Specific solution when using Rectangles only

        Rectangle r3 = r1.intersection(r2);
        g2d.setColor(Color.GREEN);
        g2d.fill(r3);
/*
        //  For a more generic solution using any Shape

        Area area = new Area(r1);
        area.intersect( new Area(r2) );
        g2d.setColor(Color.GREEN);
        g2d.fill(area);
*/
        g2d.dispose();

    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(300, 300);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Intersecting Rectangles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new IntersectingRectangles());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

How to remove Selected Shape in my code; when i right click every shape is deleted,

You need to iterate through the ArrayList containing the Rectangles. Then you can use the Rectangle.contains( yourMousePoint ) method to determine which Rectangle you clicked on. You will need to save the reference to the Rectangle. Then when the loop finishes executing you can remove the Rectangle from the ArrayList.

shape drawn from the point where mouse clicked, where as it should be the in the center of the mouse pointer

Then you need to change the x/y location of the Rectangle. It should be:

int x = mousePoint.x - (width / 2);
int y = mousePoint.y - (height / 2);

where width/height represent the size of the Rectangle you want to draw.

camickr
  • 321,443
  • 19
  • 166
  • 288