Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to make rectangle drawn to be vissible when a new rectangle is drawn

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Please some one help with this,
actually what i m tryin to do is, when i mouse click n drag the mouse n release it , i want a rectangle to be drawn.

and when i click on another location then also i want a rectangle to be drawn and the previusly drawn rectangle should also be vissible.

I am able to draw the rectangle but whe i draw a new rectangle the old rectangle vanishes.

i should be able to draw any number of rectangles, and all the rectangle
should be vissible.

can any one plz help me with this.

Plz help

Regards
Sanam

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


/*
<applet code = "Test.class" width = 1020 height = 600>
</applet>
*/
public class Test extends Applet implements MouseListener
{
private Color color = Color.black;
int x1, y1, x2, y2 = 0;
int width, height = 0;
int startX, startY = 0;
boolean drawObject = false;
boolean moveObject = false;
Rectangle myRectangle = new Rectangle(startX,startY,width,height);

public void init()
{
setBackground(Color.gray);
addMouseListener(this);
int i = 5;
}


public void paint(Graphics g)
{
//recover Graphics2D
Graphics2D g2 = (Graphics2D)g;
g2.setColor(color);
if(drawObject)
{
//create Rectangle Object
myRectangle = new Rectangle(startX,startY,width,height);
}
else if (moveObject)
{
myRectangle.setLocation(x2,y2);
}
g2.draw(myRectangle);
}

/////////////////////////////////////////////////////
//--> Start of section dealing with Mouse Events
/////////////////////////////////////////////////////
public void mouseClicked(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{
x1 = e.getX();
y1 = e.getY();
//Check to see if mouse is pressed in the current rectangle.
if(myRectangle.contains(x1,y1))
{
//move the object instead of moving it
drawObject = false;
moveObject = true;
}
else
{
//draw the object, don't move
drawObject = true;
moveObject = false;
}

}

public void mouseReleased(MouseEvent e)
{
//get the positions of where the mouse was released
x2 = e.getX();
y2 = e.getY();
if (x1 != x2 && y1 != y2)
{
//maths to work out where to draw the object
if(x1 < x2 && y1 < y2)
{//1
startX = x1;
startY = y1;
height = y2 - y1;
width = x2 - x1;
}
else if (x1 > x2 && y1 > y2)
{//2
startX = x2;
startY = y2;
height = y1 - y2;
width = x1 - x2;
}
else if (x1 > x2 && y1 < y2)
{//3
startX = x2;
startY = y1;
height = y2 - y1;
width = x1 - x2;
}
else if (x1 < x2 && y1 > y2)
{//4
startX = x1;
startY = y2;
height = y1 - y2;
width = x2 - x1;
}

}
repaint();
}
}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Draw each new Rectangle as before.
When the mouse is released:
1 — make a new rectangle like the one you just drew and add it to an ArrayList
2 — reset a boolean, say dragging, so that the rectangle you just drew with the mouse will no longer be painted in the paint method. This temporary rectangle will then be replaced by the one you just added to the ArrayList after the next step,
3 — call repaint so paint will draw its list of rectangles.

Inside the paint method loop through the ArrayList, get each Rectangle and draw it. Also draw the temporary one that you create with dragging the mouse while the dragging is in progress

[ February 23, 2005: Message edited by: Craig Wood ]
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic