Chris Dancy

Ranch Hand

Posts: 136

posted 11 years ago

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));

}

}

//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));

}

}

("Anger is not an emotion, its a symptom of fear.")

Jeff Albertson

Ranch Hand

Posts: 1780