Costruire una classe JPulsante costituita da un cerchietto di 40 per 40 pixel con bordo nero ed interno giallo. Agganciare all’oggetto due ascoltatori di evento del mouse: mouseEntered e mouseExited in modo che passandoci sopra il mouse il pulsante diventi con lo sfondo rosso, e togliendovi da sopra il mouse lo sfondo ritorni giallo. Suggerimento: definire un attributo boolean “over” true/false per registrare lo stato del mouse (sopra o non sopra l’oggetto).
Esercizi in preparazione alla verifica
- Costruire una classe che disegni una scacchiera bianca e nera di dimensione massima 256×256 pixel
- Costruire una classe che serva a rappresentare su un istogramma a rettangoli di colori diversi l’andamento di un titolo azionario durate i vari giorni della settimana; definire gli attributi rend1, rend2, etc. per ogni giorno della settimana.
JOxy
package assicartesiani;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
public class JOxy extends JComponent {
private int scalaX, scalaY, traslX, traslY;
private String asseX, asseY;
public JOxy() {
setPreferredSize(new Dimension(400, 400));
setScalaX(50);
setScalaY(50);
setTraslX(40);
setTraslY(40);
setAsseX("X");
setAsseY("Y");
}
public int xg(double x) {
return (int) Math.round(getTraslX() + x * getScalaX());
}
public int yg(double y) {
return (int) Math.round(getHeight() - 1 - getTraslY() - y * getScalaY());
}
public double xr(int x) {
return (double) (x - getTraslX()) / getScalaX();
}
public double yr(int y) {
return (double) (getHeight() - 1 - getTraslY() - y) / getScalaY();
}
@Override
public void paintComponent(Graphics g) {
int w = getWidth() - 1;
int h = getHeight() - 1;
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(getFont());
FontMetrics fm = g2.getFontMetrics();
g2.setColor(getBackground());
g2.fillRect(0, 0, w, h);
g2.setColor(getForeground());
// asse x
g2.drawLine(0, yg(0), w, yg(0));
g2.drawLine(w - 20, yg(0) - 10, w, yg(0));
g2.drawLine(w - 20, yg(0) + 10, w, yg(0));
for (int x = -getTraslX() / getScalaX(); x < (w - getTraslX()) / getScalaX(); x++) {
g2.drawLine(xg(x), yg(0) - 5, xg(x), yg(0) + 5);
String xs = x + "";
g2.drawString(xs, xg(x) - fm.stringWidth(xs) / 2, yg(0) + 5 + fm.getHeight());
}
g2.drawString(getAsseX(), w - 5 - fm.stringWidth(getAsseX()), yg(0) - 15 - fm.getDescent());
// asse y
g2.drawLine(xg(0), h, xg(0), 0);
g2.drawLine(xg(0) - 10, 20, xg(0), 0);
g2.drawLine(xg(0) + 10, 20, xg(0), 0);
for (int y = -getTraslY() / getScalaY(); y < (h - getTraslY()) / getScalaY(); y++) { g2.drawLine(xg(0) - 5, yg(y), xg(0) + 5, yg(y)); String ys = y + ""; g2.drawString(ys, xg(0) - 10 - fm.stringWidth(ys), yg(y) + fm.getAscent() / 2); } g2.drawString(getAsseY(), xg(0) + 15, fm.getHeight()); } public int getScalaX() { return scalaX; } public final void setScalaX(int scalaX) { if (scalaX >= 20) {
this.scalaX = scalaX;
repaint();
}
}
public int getScalaY() {
return scalaY;
}
public final void setScalaY(int scalaY) {
if (scalaY >= 20) {
this.scalaY = scalaY;
repaint();
}
}
public int getTraslX() {
return traslX;
}
public final void setTraslX(int traslX) {
if (traslX >= 0) {
this.traslX = traslX;
repaint();
}
}
public int getTraslY() {
return traslY;
}
public final void setTraslY(int traslY) {
if (traslY >= 0) {
this.traslY = traslY;
repaint();
}
}
public String getAsseX() {
return asseX;
}
public final void setAsseX(String asseX) {
if (asseX != null) {
this.asseX = asseX;
} else {
this.asseX = "";
}
}
public String getAsseY() {
return asseY;
}
public final void setAsseY(String asseY) {
if (asseY != null) {
this.asseY = asseY;
} else {
this.asseY = "";
}
}
}
JCirconferenza
import java.awt.Color;
import java.awt.Graphics;
public class JCirconferenza extends JOxy {
private double a, b, c;
public JCirconferenza() {
setA(0.0);
setB(0.0);
setC(-1.0);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
double xC = -getA()/ 2.0;
double yC = -getB()/ 2.0;
double r = Math.sqrt(xC * xC + yC * yC - getC());
g.setColor(Color.BLUE);
g.drawOval(xg(xC - r), yg(yC + r), (int) Math.round(2.0 * r * getScalaX()), (int) Math.round(2.0 * r * getScalaY()));
}
public double getA() {
return a;
}
public final void setA(double a) {
this.a = a;
repaint();
}
public double getB() {
return b;
}
public final void setB(double b) {
this.b = b;
repaint();
}
public double getC() {
return c;
}
public final void setC(double c) {
this.c = c;
repaint();
}
}
JParabola
import java.awt.Graphics;
public class JParabola extends JOxy {
private double a, b, c;
public JParabola() {
setA(1.0);
setB(0.0);
setC(0.0);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth() - 1;
double x0 = 0.0, y0 = 0.0;
for (int i = 0; i < w; i++) {
double x = xr(i);
double y = getA() * x * x + getB() * x + getC();
if (i == 0) {
x0 = x;
y0 = y;
} else {
g.drawLine(xg(x0), yg(y0), xg(x), yg(y));
x0 = x;
y0 = y;
}
}
}
public double getA() {
return a;
}
public final void setA(double a) {
this.a = a;
repaint();
}
public double getB() {
return b;
}
public final void setB(double b) {
this.b = b;
repaint();
}
public double getC() {
return c;
}
public final void setC(double c) {
this.c = c;
repaint();
}
}
Logo Peano
public class JLogo extends JComponent {
public JLogo() {
setPreferredSize(new Dimension(80, 100));
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(0, 200, 255));
g2.setStroke(new BasicStroke(10.0f));
int[] x = {10, 30, 30, 10, 10, 30, 30, 50, 50, 70, 70, 50, 50, 70};
int[] y = {70, 70, 50, 50, 10, 10, 30, 30, 10, 10, 50, 50, 70, 70};
g2.drawPolyline(x, y, x.length);
g2.setFont(new Font("Arial", Font.BOLD, 24));
g2.setColor(Color.RED);
g2.drawString("Peano", 5, 95);
}
}
Grafica in Java
Cartella compressa contenente il progetto NetBeans con gli esempi: Canvas [ZIP]
OperatoriSuiBit.java
public class OperatoriSuiBit {
public static String bin(int i) {
return Integer.toBinaryString(i)+"b";
}
public static void main(String[] args) {
int a, b, c;
System.out.println("Operatori logici");
a = 10; System.out.println("a = "+a+" = "+bin(a));
b = 12; System.out.println("b = "+b+" = "+bin(b));
c = a & b; System.out.println("c = a & b = "+c+" = "+bin(c));
c = a | b; System.out.println("c = a | b = "+c+" = "+bin(c));
c = ~a; System.out.println("c = ~a = "+c+" = "+bin(c));
System.out.println();
System.out.println("Operatori di spostamento");
a = 20; System.out.println("a = "+a+" = "+bin(a));
c = a << 2; System.out.println("c = a << 2 = "+c+" = "+bin(c));
c = a >> 2; System.out.println("c = a >> 2 = "+c+" = "+bin(c));
c = a >>> 2; System.out.println("c = a >>> 2 = "+c+" = "+bin(c));
b = -20; System.out.println("b = "+b+" = "+bin(b));
c = b << 2; System.out.println("c = b << 2 = "+c+" = "+bin(c));
c = b >> 2; System.out.println("c = b >> 2 = "+c+" = "+bin(c));
c = b >>> 2; System.out.println("c = b >>> 2 = "+c+" = "+bin(c));
}
}
Materiale didattico per la 3ª E – CSS
Classe 1ª I – Esercizio sulla propagazione degli errori
Calcolare con relativo errore il volume di un cilindro di raggio r = (12,05±0,05) mm ed altezza h = (27,15±0,01) mm
A = π r² = π r r
V = A h
π = 3,1415926
A = 456,167 mm²
ΔA = (0+Δr/r+Δr/r) A = 3,786 mm²
V = 12’384,9 mm³ = 12,3849 cm³
ΔV = (3,786/456,167+0,01/27,15) 12’384,9 = 107,4 mm³ = 0,1074 cm³
V = (12,4±0,1) cm³
E% = 0,8 %


