Java for LEGO EV3 (Part1 - Display)
Prerequisite
LeJOS, Eclipse, Java 7
1 Screen Display
The screen resolution of EV3 is 178px × 128px, and the origin located at the top left corner. When displayed in units of pixels, the value ranges are 0~177 (x-axis) and 0~127 (y-axis). When displayed in units of columns and rows, the value ranges are 0~16 (columns) and 0~7 (rows). Additionally, pixels are mostly used to draw graphics, while the columns and rows are mostly used to display texts.
Coordinate | Letter | Pixel |
---|---|---|
x’s range | 0~16 | 0~177 |
y’s range | 0~7 | 0~127 |
1.1 print methods
Regular print
methods of Java.
System.out.print();
System.out.println();
1.2 LCD class
Usage:
import lejos.hardware.lcd.LCD;
1.2.1 Fields
Type | Field |
---|---|
int | CELL_HEIGHT |
int | CELL_WIDTH |
int | SCREEN_HEIGHT |
int | SCREEN_WIDTH |
1.2.2 Methods
drawChar(char c, int x, int y)
// output 'A' at (0, 0)
LCD.drawChar('A', 0, 0);
drawString(String str, int x, int y)
drawString(String str, int x, int y, boolean inverted)
drawInt(int i, int places, int x, int y)
setPixel(int x, int y, int color)
All methods of LCD
class are static methods, which can be called without creating an instance.
Example:
import lejos.hardware.lcd.LCD;
import lejos.utility.Delay;
public class HelloWorld {
public static void main(String[] args) {
for(int i=0; i<=7; i++) {
LCD.drawString("HELLO WORLD", 0, i, i%2==1);
}
// wait for 5000 milliseconds
Delay.msDelay(5000);
}
new method: msDelay(long period)
Check out the LCD (leJOS EV3 API) for more info of the LCD
class.
1.3 GraphicsLCD interface
Usage:
import lejos.hardware.BrickFinder;
import lejos.hardware.lcd.GraphicsLCD;
public class HelloWorld {
public static void main(String[] args) {
GraphicsLCD g = BrickFinder.getDefault().getGraphicsLCD();
}
}
1.3.1 Fields
Type | Field |
---|---|
int | BASELINE |
int | BLACK |
int | BOTTOM |
int | DOTTED |
int | HCENTER |
int | LEFT |
int | RIGHT |
int | SOLID |
int | TOP |
int | VCENTER |
int | WHITE |
1.3.2 Methods
drawString(String str, int x, int y, int anchor)
drawLine(int x0, int y0, int x1, int y1)
drawRect(int x, int y, int width, int height)
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
fillRect(int x, int y, int width, int height)
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
drawImage(Image src, int x, int y, int anchor)
Example: draw a sine graph
import lejos.hardware.BrickFinder;
import lejos.hardware.lcd.GraphicsLCD;
public class HelloWorld {
public static void main(String[] args) {
GraphicsLCD g = BrickFinder.getDefault().getGraphicsLCD();
// the origin coordinates
int xp=10;
int yp=g.getHeight()/2;
// draw x-axis
g.drawLine(5, yp, 172, yp);
// draw y-axis
g.drawLine(xp, 5, xp, 122);
// draw sine graph
for(int i=-5; i<= 162; i++) {
// each pixel represent 3°
double si = Math.sin((3 * i) * Math.PI / 180);
// calculate the value of y
int yPoint=(int) (si * 30);
// draw pixels
LCD.setPixel(xp + i, yp - yPoint, 1);
}
// wait 5 seconds
Delay.msDelay(5000);
}
}
Check out the GraphicsLCD (leJOS EV3 API) for more info of the GraphicsLCD
interface.