Files
GTSchoolShit/CS2335/lab4/src/netpaint/client/tools/ImageTool.java
2025-06-07 01:59:34 -04:00

141 lines
4.2 KiB
Java

package netpaint.client.tools;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import netpaint.messaging.ImageMessage;
import netpaint.client.gui.ClientUI;
import netpaint.client.Client;
/**
* Class ImageTool: The image tool allows you to insert an image into
* the graphics panel.
*
* <PRE>
* Revision History:
* v1.0 (Feb. 09, 2004) - Created the ImageTool class
* </PRE>
*
* @author <A HREF="mailto:gtg308i@mail.gatech.edu">Vladimir Urazov</A>
* @version Version 1.0, Feb. 09, 2004
*/
public class ImageTool extends AbstractTool {
/**
* Creates a new <code>ImageTool</code> instance.
*/
public ImageTool() {
super(netpaint.messaging.MessageSettings.DRAW_IMAGE_TYPE);
incrementClickCount();
}
/**
* This function will be called when the user clicks on the graphics
* panel.
*
* @param position a <code>Point</code> value
* @exception Exception if an error occurs
*/
public void processClick(final Point position) throws Exception {
ImageMessage msg = null;
if (getClickCount() == 1) {
//FIXME: Please
clear();
BufferedImage image = ClientUI.getInstance().getImagePanel().getImage();
if (image == null) {
//Tell the user to load an image and THEN click!
ClientUI.getInstance().showWarning("Warning", "First you should paste the URL"
+ "into the URL text area, then click 'Load',"
+ " and then you may click on the"
+ "panel to select where to insert the image");
return;
}
msg = (ImageMessage) getMessage();
//This is the first click:
msg.setXCoord((float) position.getX());
msg.setYCoord((float) position.getY());
incrementClickCount();
} else if (getClickCount() == 2) {
msg = (ImageMessage) getMessage();
msg.setWidth((float) (position.getX() - msg.getXCoord()));
msg.setHeight((float) (position.getY() - msg.getYCoord()));
msg.setURL(ClientUI.getInstance().getImagePanel().getImageURL());
finish();
}
}
/**
* This will clear any progress the tool has made so far. If the
* user has already started drawing the shape, but has not yet
* finished, calling this method will discard all the progress made
* so far and start from the clean slate.
*/
public synchronized void clear() {
resetClickCount();
incrementClickCount();
setMessage(new ImageMessage(Client.getInstance().getUsername()));
}
/**
* If the tool is currently being employed and the user has drawn
* some of the shape, but hasn't yet finished, then this function
* should render the intermediate view of the shape to the
* target. If the tool has already finished, or hasn't even started
* yet, then it should draw nothing.
*
* @param target a <code>Graphics2D</code> value
*/
public synchronized void render(final Graphics2D target) {
ImageMessage msg = (ImageMessage) getMessage();
Point mLocation
= ClientUI.getInstance().getGraphicsPanel().getMousePosition();
if (getClickCount() == 1) {
float xCoord = (float) mLocation.getX();
float yCoord = (float) mLocation.getY();
BufferedImage image
= ClientUI.getInstance().getImagePanel().getImage();
if (image != null) {
target.drawImage(image, null, (int) xCoord, (int) yCoord);
}
} else if (getClickCount() == 2) {
float xCoord = msg.getXCoord();
float yCoord = msg.getYCoord();
float width = (float) (mLocation.getX() - xCoord);
float height = (float) (mLocation.getY() - yCoord);
BufferedImage image
= ClientUI.getInstance().getImagePanel().getImage();
if (image == null) { return; }
if (width < 1) {
width = 1.0f;
}
if (height < 1) {
height = 1.0f;
}
// target.drawImage(bim, null, (int) xCoord, (int) yCoord);
target.drawImage(image,
(int) xCoord,
(int) yCoord,
(int) width, (int) height, null);
}
}
}