Creare JRetta sottoclasse di JOxy avente i due attributi “m” e “q” che disegna all’interno di un sistema di riferimento il grafico della retta in forma esplicita.
package it.liceocuneo.math; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.io.Serializable; import javax.swing.JPanel; public class JOxy extends JPanel implements Serializable { private String titleX = "X", titleY = "Y"; private int scaleX = 40, scaleY = 40; private int traslX = 10, traslY = 10; public JOxy() { this.setPreferredSize(new Dimension(200, 200)); this.setBackground(Color.WHITE); } public int xg(double xr) { return (int) Math.round(this.getWidth() * traslX / 100 + xr * scaleX); } public int yg(double yr) { return (int) Math.round(this.getHeight() * (100 - traslY) / 100 - yr * scaleY); } public double xr(int xg) { return (xg - this.getWidth() * traslX / 100) / (double) scaleX; } public double yr(int yg) { return (this.getHeight() * (100 - traslY) / 100 - yg) / (double) scaleY; } public double getMinX() { return xr(0); } public double getMaxX() { return xr(this.getWidth()); } public double getMinY() { return yr(this.getHeight()); } public double getMaxY() { return yr(0); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int w = this.getWidth(), h = this.getHeight(); // asse X g.drawLine(0, yg(0), w, yg(0)); for (int i = (int) getMinX(); i <= (int) getMaxX(); i++) { g.drawLine(xg(i), yg(0) - 5, xg(i), yg(0) + 5); g.drawString(i + "", xg(i) - g.getFontMetrics().stringWidth(i + "") / 2, yg(0) + 5 + g.getFontMetrics().getAscent()); } g.drawString(titleX, w - 5 - g.getFontMetrics().stringWidth(titleX), yg(0) - 10); // asse Y g.drawLine(xg(0), 0, xg(0), h); for (int i = (int) getMinY(); i <= (int) getMaxY(); i++) { g.drawLine(xg(0) - 5, yg(i), xg(0) + 5, yg(i)); g.drawString(i + "", xg(0) - 7 - g.getFontMetrics().stringWidth(i + ""), yg(i) - 1 + g.getFontMetrics().getAscent() / 2); } g.drawString(titleY, xg(0) + 10, 15); } public String getTitleX() { return titleX; } public void setTitleX(String titleX) { this.titleX = titleX; } public String getTitleY() { return titleY; } public void setTitleY(String titleY) { this.titleY = titleY; } public int getScaleX() { return scaleX; } public void setScaleX(int scaleX) { if (scaleX >= 20) this.scaleX = scaleX; } public int getScaleY() { return scaleY; } public void setScaleY(int scaleY) { if (scaleY >= 20) this.scaleY = scaleY; } public int getTraslX() { return traslX; } public void setTraslX(int traslX) { this.traslX = traslX; } public int getTraslY() { return traslY; } public void setTraslY(int traslY) { this.traslY = traslY; } }