• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

drawRect() issues

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, it worked.

Sincerely,
Chris Dancy
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic