gr.drawRect( (int)(houseX*xScale) , (int)(houseY*yScale) , (int)(houseW*xScale), (int)(houseH*yScale)); // house
Essentially, you scale every X value by xScale
and every Y value by yScale
.
This is like doing a zoom or a shrink operation with an image on your phone.
However, since drawRect()
expects integer arguments, you need to use a type cast (int)
to convert from floating point.
Here is the modified method that fits the drawing to whatever window the user has made.
public void paintComponent ( Graphics gr ) { super.paintComponent( gr ); double xScale = ((double)getWidth()) /width; double yScale = ((double)getHeight())/height; gr.setColor( Color.orange ); // there is no color brown gr.drawRect( (int)(houseX*xScale) , (int)(houseY*yScale) , (int)(houseW*xScale), (int)(houseH*yScale)); // house gr.drawRect( (int)(doorX *xScale) , (int)(doorY*yScale) , (int)(doorW*xScale) , (int)(doorH *yScale)); // door gr.drawRect( (int)(lWindX*xScale) , (int)(lWindY*yScale) , (int)(lWindW*xScale), (int)(lWindH*yScale)); // lwind gr.drawRect( (int)(rWindX*xScale) , (int)(rWindY*yScale) , (int)(rWindW*xScale), (int)(rWindH*yScale)); // rwind gr.drawRect( (int)(trunkX*xScale) , (int)(trunkY*yScale) , (int)(trunkW*xScale), (int)(trunkH*yScale)); // trunk gr.drawLine( (int)(roof1X1*xScale), (int)(roof1Y1*yScale), (int)(roof1X2*xScale), (int)(roof1Y2*yScale) ); gr.drawLine( (int)(roof2X1*xScale), (int)(roof2Y1*yScale), (int)(roof2X2*xScale), (int)(roof2Y2*yScale) ); gr.setColor( Color.green ); gr.drawOval( (int)(treeX*xScale), (int)(treeY*yScale), (int)(treeW*xScale), (int)(treeH*yScale) ); gr.drawLine( (int)(L1X1*xScale), (int)(L1Y1*yScale), (int)(L1X2*xScale), (int)(L1Y2*yScale) ); // line 1 gr.drawLine( (int)(L2X1*xScale), (int)(L2Y1*yScale), (int)(L2X2*xScale), (int)(L2Y2*yScale) ); // line 2 gr.drawLine( (int)(L3X1*xScale), (int)(L3Y1*yScale), (int)(L3X2*xScale), (int)(L3Y2*yScale) ); // line 3 gr.drawLine( (int)(L4X1*xScale), (int)(L4Y1*yScale), (int)(L4X2*xScale), (int)(L4Y2*yScale) ); // line 4 gr.drawLine( (int)(L5X1*xScale), (int)(L5Y1*yScale), (int)(L5X2*xScale), (int)(L5Y2*yScale) ); // line 5 }
As it is written, the picture will be distorted to fit the shape of the frame. It might be nice to "lock" the proportions so that shapes are not distorted.
Suggest a way to do that.