Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
File Manager
/
Ebook
/
Informatique
/
JAVA
/
Code
:
Rec.java
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
import java.awt.event.*; import javax.swing.*; import java.awt.*; /** * @author Agbeladem */ public class Rec extends JFrame { public Rec() { super("rectangle"); setPreferredSize(new java.awt.Dimension(400, 400)); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel pane = new Pan(); add(pane); pack(); setVisible(true); } public static void main(String[] args) { new Rec(); } } class Pan extends JPanel { private Point origin; private Point end; private Rectangle r1, r2, r3; private Rectangle selectedRectangle; // Used to track selected shape private int x, y ; // Used to track mouse coordinates public Pan() { MouseAdapter adapter = new Lis(); addMouseMotionListener(adapter); addMouseListener(adapter); } class Lis extends MouseAdapter { public void mouseDragged(MouseEvent e) { end = e.getPoint(); repaint(); } public void mousePressed(MouseEvent e) { origin = e.getPoint(); } public void mouseClicked(MouseEvent e) { System.out.print("bonjour"); x = e.getX(); y = e.getY(); if (r1.containsPoint(x, y)){ System.out.print("bingo"); } else { System.out.print("nada"); } } } public void paint(Graphics g) { g.clearRect(0, 0, getWidth(), getHeight()); g.setColor(Color.YELLOW); if(origin != null) { int tmp; int x1 = (int)(origin.getX()); int y1 = (int)(origin.getY()); int x2 = (int)(end.getX()); int y2 = (int)(end.getY()); if(x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; } if(y1 > y2) { tmp = y1; y1 = y2; y2 = tmp; } r1 = g.fillRect(x1, y1, x2-x1, y2-y1); } } }