Search…
ImageControl

Overview

A control that can show an image bigger than its area and that can be dragged using a pen to show the hidden parts.
Note that, by default, events (and dragging) are disabled. You must call setEventsEnabled to allow dragging.

Source Code

Login TCSample
1
import java.sql.SQLException;
2
3
import totalcross.db.sqlite.SQLiteUtil;
4
import totalcross.io.IOException;
5
import totalcross.sample.util.Colors;
6
import totalcross.sql.PreparedStatement;
7
import totalcross.sql.Statement;
8
import totalcross.sys.Settings;
9
import totalcross.sys.Vm;
10
import totalcross.ui.Button;
11
import totalcross.ui.Check;
12
import totalcross.ui.Container;
13
import totalcross.ui.Edit;
14
import totalcross.ui.ImageControl;
15
import totalcross.ui.ScrollContainer;
16
import totalcross.ui.dialog.MessageBox;
17
import totalcross.ui.event.ControlEvent;
18
import totalcross.ui.event.Event;
19
import totalcross.ui.gfx.Color;
20
import totalcross.ui.image.Image;
21
import totalcross.ui.image.ImageException;
22
import totalcross.util.InvalidDateException;
23
24
public class Login extends ScrollContainer {
25
private Edit edPass, edLogin;
26
private Check ch;
27
private Button btLogin, btRegister;
28
private ImageControl ic;
29
private SQLiteUtil util;
30
31
public void initUI(){
32
try {
33
setBackForeColors(Colors.BACKGROUND, Colors.ON_BACKGROUND);
34
ic = new ImageControl(new Image("images/logo.png"));
35
ic.scaleToFit = true;
36
ic.centerImage = true;
37
add(ic, LEFT, TOP+100, FILL, PARENTSIZE+30);
38
39
edLogin = new Edit();
40
edLogin.caption = "Login";
41
//edLogin.setBackColor(Color.RED);
42
add(edLogin, CENTER, AFTER+60, PARENTSIZE+90, PREFERRED+30);
43
44
edPass = new Edit();
45
edPass.caption = "Password";
46
//edPass.setBackColor(Color.RED);
47
edPass.setMode(Edit.PASSWORD_ALL);
48
add(edPass, SAME, AFTER+70, PARENTSIZE+90, PREFERRED+30);
49
50
ch = new Check("Remember Me");
51
add(ch, LEFT+86, AFTER+100, PARENTSIZE, PREFERRED+30);
52
53
btLogin = new Button("Login");
54
btLogin.setBackColor(Color.WHITE);
55
add(btLogin, CENTER, AFTER+140, PARENTSIZE+80, PREFERRED+60);
56
57
btRegister = new Button("Register Now");
58
btRegister.transparentBackground = true;
59
btRegister.setBorder(BORDER_NONE);
60
add(btRegister, CENTER, AFTER, PARENTSIZE+30, PREFERRED+20);
61
btRegister.addPressListener(e -> {Vm.exec("url", "http://www.totalcross.com", 0, true);});
62
63
//Creating Database
64
util = new SQLiteUtil(Settings.appPath,"database.db");
65
Vm.debug(util.fullPath);
66
67
Statement st = util.con().createStatement();
68
st.execute("create table if not exists person (login varchar(20), password varchar(20))");
69
st.close();
70
71
} catch (IOException | ImageException | SQLException e) {
72
// TODO Auto-generated catch block
73
e.printStackTrace();
74
}
75
}
76
77
public void onEvent(Event e){
78
try{
79
switch(e.type){
80
case ControlEvent.PRESSED:
81
if(e.target == btLogin){
82
doInsert();
83
}
84
}
85
}catch(Exception ee){
86
MessageBox.showException(ee, true);
87
}
88
}
89
90
private void doInsert() throws SQLException, InvalidDateException, ImageException {
91
if (edLogin.getLength() == 0 || edPass.getLength() == 0){
92
MessageBox mb = new MessageBox("Message","Please fill all fields!",new String[]{"Close"});
93
mb.setBackForeColors(Color.WHITE, Color.BLACK);
94
mb.popup();
95
}else {
96
// simple example of how you can insert data into SQLite..
97
String sql = "insert into person values(?,?)";
98
PreparedStatement st = util.con().prepareStatement(sql);
99
st.setString(1, edLogin.getText());
100
st.setString(2, edPass.getText());
101
st.executeUpdate();
102
st.close();
103
104
MessageBox mbox = new MessageBox(null,"Data inserted successfully!");
105
mbox.setBackForeColors(Color.WHITE, Color.BLACK);
106
mbox.popup();
107
108
}
109
}
110
}
Copied!
Do not forget to create a folder called "images" inside /src/main/resources and save the logo.png image inside it [images].

Methods

Type
Name
Description
Constructor
ImageControl(Image img)
Constructs an ImageControl using the given image.
Constructor
ImageControl()
Constructs with no initial image. You must set the image with the setImage method.
void
setEventsEnabled(boolean enabled)
Pass true to enable dragging and events on the image.
void
setImage(Image img)
Sets the image to the given one. If the image size is different, you must explicitly call setRect again if you want to resize the control.
void
setImage(Image img, boolean resetPositions)
Sets the image to the given one, optionally resetting the image position. If the image size is different, you must explicitly call setRect again if you want to resize the control.
int
getImageHeight()
Returns the image's height; when scaling, returns the scaled height
int
getImageWidth()
Returns the image's width; when scaling, returns the scaled width.
Image
getImage()
Returns the current image assigned to this ImageControl.
void
setBackground(Image img)
Sets the given image as a freezed background of this image control.
boolean
moveTo(int newX, int newY)
Moves to the given coordinates, respecting the current moving policy regarding

References

Last modified 2yr ago