import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;
public class Segui_luce {
public static void main(String[] args) {
Graphics g = new Graphics();
DifferentialPilot df = new DifferentialPilot(5.5, 11.5, Motor.A, Motor.C);
LightSensor ls = new LightSensor(SensorPort.S1, false);
do {
int val = 0, valMax = 0, a = 0, aMax = 0;
for (int x = 0; x < 36; x++) {
val = ls.getLightValue();
g.drawLine(x, 0, x, val / 2);
if (val > valMax) {
valMax = val;
aMax = a;
}
df.rotate(10);
a += 10;
}
df.rotate(aMax);
df.forward();
Delay.msDelay(2000);
g.clear();
} while (!Button.ESCAPE.isDown());
}
}
Categoria: Informatica
Sensore di luce
import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;
public class IstogrammaLuce {
// realizzare un programma che visualizzi l'intensità di luce letta al
// sensore di luce come valore testuale nella posizione 0, 0 sia come
// istogramma rettangolare da sinistra verso destra con un'altezza
// proporzionale all'intensità con refresh ogni 1/10 di secondo
public static void main(String[] args) {
LightSensor ls = new LightSensor(SensorPort.S1, false);
DifferentialPilot dp = new DifferentialPilot(5.5, 11.5, Motor.A,
Motor.C);
Graphics g = new Graphics();
dp.forward();
do {
g.clear();
int val = ls.readValue();
LCD.drawInt(val, 0, 0);
g.fillRect(0, 40, val / 2, 50);
Delay.msDelay(100);
if (val > 50) {
dp.rotate(-90);
dp.forward();
}
} while (!Button.ESCAPE.isDown());
}
}
Sensore di suono con grafico
import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.nxt.SensorPort;
import lejos.nxt.SoundSensor;
import lejos.util.Delay;
public class Suono {
public static void main(String[] args)
{
SoundSensor ss = new SoundSensor(SensorPort.S1);
Graphics g = new Graphics();
int valore = 0, x = 0;
do {
valore = ss.readValue();
g.setColor(1);
g.drawLine(x, 0, x,63);
g.setColor(0);
g.drawLine(x, 0, x, valore/2);
if (x==99)
x = 0;
Delay.msDelay(100);
} while(!Button.ESCAPE.isDown());
}
}
Cubica
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cubica</title>
</head>
<body>
<h1>Cubica</h1>
<p>y = a x³ + b x² + c x + d</p>
<form action="index.jsp" method="get">
a = <input type="text" name="a"/><br/>
b = <input type="text" name="b"/><br/>
c = <input type="text" name="c"/><br/>
d = <input type="text" name="d"/><br/>
<input type="submit" value="Disegna" />
</form>
<p><img width="400" height="400" src="grafico?a=<%=request.getParameter("a")%>&b=<%=request.getParameter("b")%>&c=<%=request.getParameter("c")%>&d=<%=request.getParameter("d")%>" /></p>
</body>
</html>
grafico.java
double a = 0.0, b = 0.0, c = 0.0, d = 0.0;
private double f(double x) {
return a * x * x * x + b * x * x + c * x + d;
}
private int xg(double x) {
return 200 + (int) Math.round(x * 40.0);
}
private int yg(double y) {
return 200 - (int) Math.round(y * 40.0);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/png");
try (OutputStream os = response.getOutputStream()) {
// crea l'immagine
BufferedImage img = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
Graphics g = img.createGraphics();
// cancella lo sfondo
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 400);
// disegna gli assi cartesiani
g.setColor(Color.BLACK);
g.drawLine(0, 200, 400, 200);
g.drawString("X", 390, 190);
g.drawLine(200, 0, 200, 400);
g.drawString("Y", 210, 10);
try {
// legge i parametri
a = Double.parseDouble(request.getParameter("a"));
b = Double.parseDouble(request.getParameter("b"));
c = Double.parseDouble(request.getParameter("c"));
d = Double.parseDouble(request.getParameter("d"));
// visualizza i parametri
g.drawString("a = " + a, 0, 20);
g.drawString("b = " + b, 0, 40);
g.drawString("c = " + c, 0, 60);
g.drawString("d = " + d, 0, 80);
// disegna il grafico
g.setColor(Color.BLUE);
for (double x = -10.0; x < 10.0; x += 0.001) {
double y = f(x);
g.drawLine(xg(x), yg(y), xg(x), yg(y));
}
} catch (Exception e) {
g.drawString("Alcuni parametri mancanti", 0, 20);
g.drawString("o non inseriti correttamente", 0, 40);
}
// invia l'immagine in output
ImageIO.write(img, "png", os);
os.close();
}
}
Grafico e audio del TouchSensor premuto
import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
import lejos.nxt.TouchSensor;
import lejos.util.Delay;
public class Line {
public static void main(String[] args) {
Graphics g = new Graphics();
TouchSensor ts = new TouchSensor(SensorPort.S1);
int x = 0;
int y1 = 32, y2 = 32;
do {
g.setColor(1);
g.drawLine(x, 0, x, 63);
g.setColor(0);
if (ts.isPressed()) {
y2 = 22;
Sound.beep();
} else
y2 = 32;
g.drawLine(x, y1, x, y2);
y1 = y2;
x++;
if (x == 100)
x = 0;
Delay.msDelay(100);
} while (!Button.ESCAPE.isDown());
}
}
Uso del touch-sensor sull’NXT
import lejos.nxt.Button;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;
public class ProvaTouch {
public static void main(String[] args) {
TouchSensor ts = new TouchSensor(SensorPort.S4);
DifferentialPilot dp = new DifferentialPilot(5.5, 11.5, Motor.A, Motor.C);
dp.forward();
do {
if (ts.isPressed()) {
dp.backward();
Delay.msDelay(500);
dp.rotate(180);
dp.forward();
}
} while (!Button.ESCAPE.isDown());
dp.stop();
}
}
Disegno per NXT classe 3E
Realizzare il disegno seguente; i rettangoli devono avere coordinate (0,0) – (4,4) – (8,8) – etc.

import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
public class Disegna {
public static void main(String[] args) {
Graphics g = new Graphics();
int w = g.getWidth() - 1;
int h = g.getHeight() - 1;
for (int x = 0; x < h; x += 4) {
// lato orizzontale superiore
g.drawLine(x, x, w - x, x);
// lato orizzontale inferiore
g.drawLine(x, h - x, w - x, h - x);
// lato verticale sinistro
g.drawLine(x, x, x, h - x);
// lato verticale destro
g.drawLine(w - x, x, w - x, h - x);
}
Button.ESCAPE.waitForPressAndRelease();
}
}
Serpentone sull’NXT
import javax.microedition.lcdui.Graphics;
import lejos.nxt.Button;
import lejos.util.Delay;
/*
* 1 = su
* 4 = sinistra 2 = destra
* 3 = giù
*/
public class Serpentone {
public static void main(String[] args) {
Graphics g = new Graphics();
int p = 1, x = 49, y = 63; // direzione e posizione iniziale
do {
// se bottone sinistro, sterza a sinistra
if (Button.LEFT.isDown()) {
p -= 1;
if (p == 0)
p = 4;
}
// se bottone destro, sterza a destra
if (Button.RIGHT.isDown()) {
p += 1;
if (p == 5)
p = 1;
}
// se sta andando in su ed esce rientra da sotto
if (p == 1) {
y -= 1;
if (y == -1)
y = 63;
}
// se sta andando in giù ed esce rientra da sopra
if (p == 3) {
y += 1;
if (y == 64)
y = 0;
}
// se sta andando a destra ed esce rientra da sinistra
if (p == 2) {
x += 1;
if (x == 100)
x = 0;
}
// se sta andando a sinistra ed esce rientra da destra
if (p == 4) {
x -= 1;
if (x == -1)
x = 99;
}
g.drawLine(x, y, x, y);
Delay.msDelay(1000);
} while (!Button.ESCAPE.isDown());
}
}
Esempi di servlet per la 5E in preparazione alla verifica
- LoginSemplice (di Isabella Damiano)
- LoginDefinitivo (di Davide Campana)
- Sondaggio (di Simone Cavallera)
P.S. × Isabella Damiano: il tuo programma non funzionava perché avevi scritto getParameter(“pasword”) al posto di getParameter(“password”)
NXT: bottoni e schermo
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.util.Delay;
public class Bottoni {
public static void main(String[] arg) {
DifferentialPilot dp = new DifferentialPilot(5.5, 11.5, Motor.A, Motor.C);
dp.forward();
int i = 0;
do {
LCD.drawInt(i++, 0, 0);
Delay.msDelay(100);
if (Button.ENTER.isDown())
LCD.drawString("ENTER premuto", 0, 1);
else
LCD.drawString(" ", 0, 1);
} while (!Button.ESCAPE.isDown());
}
}