I am writing a 'sketchPad' type program in Java. My FREEHAND and LINE implementation works fine, but when I do a RECT or an OVAL if I draw from top left to bottom right it works fine, but the opposite way doesn't function. It will draw from my starting point then down to the bottom right again depending on the size I try to draw. I have learned a little bit about Affine Transform and thought that may be a way to go, but I need help implementing it properly, or doing it a better way.
Here is the effected portion of the code:
public void paint(){
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
graphics2D.setPaint(toolBar.getCurrentColor());
repaint();
}
});
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
String currentPaintType = toolBar.getPaintType();
if(currentPaintType != "FREEHAND"){
currentX = e.getX();
currentY = e.getY();
if(currentPaintType == "LINE"){
if (graphics2D != null){
graphics2D.drawLine(oldX, oldY, currentX, currentY);
}
}else if(currentPaintType == "OVAL"){
if (graphics2D != null){
graphics2D.drawOval(oldX, oldY, getWidthHeight(oldX,currentX), getWidthHeight(oldY,currentY));
}
}else if(currentPaintType == "RECT"){
if (graphics2D != null){
graphics2D.drawRect(oldX, oldY, getWidthHeight(oldX,currentX), getWidthHeight(oldY,currentY));
}
}
repaint();
}
}
});
}
//Code to calculate the proper width and height based on X/Y points
public int getWidthHeight(int old, int current){
int i=0;
if(old>current){
i=old-current;
}else{
i=current-old;
}
return i;
}