Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

drawRect() issues  RSS feed

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got a quick question if anyone can answer it. I want the drawRect(int,int,int,int) method to draw a rect in from one point to the next point. The problem is that drawRect() only draws from the topLeft corner of the screen to the bottomRight corner of the screen. If you try to draw it another way, it doesent work. Here is my code, can someone please tell me what im doing wrong? thanks
//Main class
class MouseDrawer extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
x1 = e.getX();
y1 = e.getY();
fUpperLeft.setText(String.format("%d,%d",x1,y1));
mainPanel2.repaint();
}
}
class MouseMotionDrawer extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
x2 = e.getX();
y2 = e.getY();
mainPanel2.setValues(x1,y1,x2,y2);
fArea.setText(String.format("%d",mainPanel2.getArea()));
fPerimeter.setText(String.format("%d",mainPanel2.getPerimeter()));
fLowerRight.setText(String.format("%d,%d",x2,y2));
fUpperLeft.setText(String.format("%d,%d",x1,y1));
mainPanel2.repaint();
}
}
}
class MyCirclePanel extends JPanel
{
private int length,width,x1,y1,x2,y2;
public void setValues(int x1,int y1,int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public int getPerimeter()
{
return Math.abs(length*2)+(length*2);
}
public int getArea()
{
return Math.abs(length*width);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(x1 < x2 && y1 < y2)
{
length = x2-x1;
width = y2-y1;
g.drawRect(x1,y1,x2-x1,y2-y1);
}
else if(x1 > x2 && y1 < y2)
{
length = y2-y1;
width = x1-x2;
g.drawRect(x1,y1,y2-y1,(x1-x2));
}
else if(x1 < x2 && y1 > y2)
{
length = x2-x1;
width = y1-y2;
g.drawRect(x1,y1,x2-x1,(y1-y2));
}
else // x1 > x2 && y1 > y2
length = x1-x2;
width = y1-y2;
g.drawRect(x1,y1,(x1-x2),(y1-y2));
}
}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried running your code, but what about something like this to simplify the logic:
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, it worked.

Sincerely,
Chris Dancy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!