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
importjava.sql.SQLException;importtotalcross.db.sqlite.SQLiteUtil;importtotalcross.io.IOException;importtotalcross.sample.util.Colors;importtotalcross.sql.PreparedStatement;importtotalcross.sql.Statement;importtotalcross.sys.Settings;importtotalcross.sys.Vm;importtotalcross.ui.Button;importtotalcross.ui.Check;importtotalcross.ui.Container;importtotalcross.ui.Edit;importtotalcross.ui.ImageControl;importtotalcross.ui.ScrollContainer;importtotalcross.ui.dialog.MessageBox;importtotalcross.ui.event.ControlEvent;importtotalcross.ui.event.Event;importtotalcross.ui.gfx.Color;importtotalcross.ui.image.Image;importtotalcross.ui.image.ImageException;importtotalcross.util.InvalidDateException;publicclassLoginextendsScrollContainer {privateEdit edPass, edLogin;privateCheck ch;privateButton btLogin, btRegister;privateImageControl ic;privateSQLiteUtil util;publicvoidinitUI(){try {setBackForeColors(Colors.BACKGROUND,Colors.ON_BACKGROUND); ic =newImageControl(new Image("images/logo.png"));ic.scaleToFit=true;ic.centerImage=true;add(ic, LEFT, TOP+100, FILL, PARENTSIZE+30); edLogin =newEdit();edLogin.caption="Login";//edLogin.setBackColor(Color.RED);add(edLogin, CENTER, AFTER+60, PARENTSIZE+90, PREFERRED+30); edPass =newEdit();edPass.caption="Password";//edPass.setBackColor(Color.RED);edPass.setMode(Edit.PASSWORD_ALL);add(edPass, SAME, AFTER+70, PARENTSIZE+90, PREFERRED+30); ch =newCheck("Remember Me");add(ch, LEFT+86, AFTER+100, PARENTSIZE, PREFERRED+30); btLogin =newButton("Login");btLogin.setBackColor(Color.WHITE);add(btLogin, CENTER, AFTER+140, PARENTSIZE+80, PREFERRED+60); btRegister =newButton("Register Now");btRegister.transparentBackground=true;btRegister.setBorder(BORDER_NONE);add(btRegister, CENTER, AFTER, PARENTSIZE+30, PREFERRED+20);btRegister.addPressListener(e -> {Vm.exec("url","http://www.totalcross.com",0,true);});//Creating Database util =newSQLiteUtil(Settings.appPath,"database.db");Vm.debug(util.fullPath);Statement st =util.con().createStatement();st.execute("create table if not exists person (login varchar(20), password varchar(20))");st.close(); } catch (IOException | ImageException | SQLException e) {// TODO Auto-generated catch blocke.printStackTrace(); } }publicvoidonEvent(Event e){try{switch(e.type){caseControlEvent.PRESSED:if(e.target== btLogin){doInsert(); } } }catch(Exception ee){MessageBox.showException(ee,true); } }privatevoiddoInsert() throwsSQLException,InvalidDateException,ImageException {if (edLogin.getLength() ==0||edPass.getLength() ==0){MessageBox mb =newMessageBox("Message","Please fill all fields!",newString[]{"Close"});mb.setBackForeColors(Color.WHITE,Color.BLACK);mb.popup(); }else {// simple example of how you can insert data into SQLite..String sql ="insert into person values(?,?)";PreparedStatement st =util.con().prepareStatement(sql);st.setString(1,edLogin.getText());st.setString(2,edPass.getText());st.executeUpdate();st.close(); MessageBox mbox =newMessageBox(null,"Data inserted successfully!");mbox.setBackForeColors(Color.WHITE,Color.BLACK);mbox.popup(); } }}
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