JOxy

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;

public class JOxy extends JComponent {

	private int sx, sy, tx, ty;
	private String asseX, asseY;
	private double xMouse, yMouse;

	private class AscoltaMouse extends MouseMotionAdapter {
		public void mouseMoved(MouseEvent e) {
			int x=e.getX();
			int y=e.getY();
			xMouse=xr(x);
			yMouse=yr(y);
			repaint();
		}
	}

	public JOxy() {
		setPreferredSize(new Dimension(400, 400));
		setSx(50);
		setSy(50);
		setTx(100);
		setTy(100);
		setAsseX("X");
		setAsseY("Y");
		addMouseMotionListener(new AscoltaMouse());
	}

	public int xg(double x) {
		return (int) Math.round(tx + x * sx);
	}

	public int yg(double y) {
		return (int) Math.round(getHeight() - 1 - ty - y * sy);
	}

	public double xr(int x) {
		return (double) (x - tx) / sx;
	}

	public double yr(int y) {
		return (double) (getHeight() - 1 - ty - y) / sy;
	}

	public void paintComponent(Graphics g) {
		int w = getWidth() - 1;
		int h = getHeight() - 1;

		g.setColor(Color.WHITE);
		g.fillRect(0, 0, w, h);
		g.setColor(Color.BLACK);

		// asse X
		g.drawLine(0, yg(0), w, yg(0));
		for (int i = (int) xr(0); i <= (int) xr(w); i++) {
			g.drawLine(xg(i), yg(0) - 5, xg(i), yg(0) + 5);
			g.drawString(i + "", xg(i) + 3, yg(0) - 3);
		}
		g.drawString(asseX, w - 5 - g.getFontMetrics().stringWidth(asseX), yg(0) - 20);

		// asse Y
		g.drawLine(xg(0), 0, xg(0), h);
		for (int i = (int) yr(h); i <= yr(0); i++) {
			g.drawLine(xg(0) - 5, yg(i), xg(0) + 5, yg(i));
			g.drawString(i + "", xg(0) + 3, yg(i) - 3);
		}
		g.drawString(asseY, xg(0) + 20, 15);

		g.drawString("("+xMouse+","+yMouse+")", xg(xMouse), yg(yMouse));
	}

	public int getSx() {
		return sx;
	}

	public void setSx(int sx) {
		if (sx < 20)
			sx = 20;
		this.sx = sx;
	}

	public int getSy() {
		return sy;
	}

	public void setSy(int sy) {
		if (sy < 20)
			sy = 20;
		this.sy = sy;
	}

	public int getTx() {
		return tx;
	}

	public void setTx(int tx) {
		if (tx < 0)
			tx = 0;
		this.tx = tx;
	}

	public int getTy() {
		return ty;
	}

	public void setTy(int ty) {
		if (ty < 0)
			ty = 0;
		this.ty = ty;
	}

	public String getAsseX() {
		return asseX;
	}

	public void setAsseX(String asseX) {
		this.asseX = asseX;
	}

	public String getAsseY() {
		return asseY;
	}

	public void setAsseY(String asseY) {
		this.asseY = asseY;
	}

}