Image

Overview

An image is a rectangular image that you can draw or copy onto a surface.

Image objects can not be added directly to the user interface because they are not controllers (that is, they do not extend the Control class).

To display an image in the user interface, you need to use a control that best suits your needs, such as ImageControl, Button, or Animation. If you do not want to use any controls, you can draw the image on the screen using the Graphics object.

It is important to note that some transformation methods return a new instance of the image, while others apply to the current instance. To preserve an image with a single frame, use getFrameInstance (0);

To better understand TotalCross's recommended way to use images in your projects, go to the Colors, Fonts and Images page in the Guideline To Create Apps section.

Source Code

ImagImageAnimationSample.java
import totalcross.game.Animation;
import totalcross.sys.Settings;
import totalcross.ui.Button;
import totalcross.ui.ComboBox;
import totalcross.ui.Container;
import totalcross.ui.Label;
import totalcross.ui.dialog.MessageBox;
import totalcross.ui.event.ControlEvent;
import totalcross.ui.event.Event;
import totalcross.ui.gfx.Color;
import totalcross.ui.image.Image;

public class ImageAnimationSample extends Container {
	final int gap = 5;
	private Button btnStartStop;
	private Animation anim;
	private ComboBox cbEffect;
	private int effect;

	@Override
	public void initUI() {
		super.initUI();
		add(btnStartStop = new Button(" Start/Stop "), CENTER, TOP + Settings.screenHeight / 7, SCREENSIZE + 50,
				SCREENSIZE + 10);
		btnStartStop.setBackForeColors(0x4583d4, 0xFFFFFF);

		add(new Label("Effect: ", LEFT, Color.BLACK, false), LEFT + Settings.screenWidth / 5,
				BOTTOM - Settings.screenHeight / 4);

		String[] items = { "normal", "scaledBy", "smoothScaledBy", "getRotatedScaledInstance", "getTouchedUpInstance",
				"changeColors", "fadedInstance", "applyColor2/dither" };
		ComboBox.usePopupMenu = false;
		add(cbEffect = new ComboBox(items), AFTER, BOTTOM - Settings.screenHeight / 4,
				FILL - Settings.screenHeight / 10, PREFERRED);
		cbEffect.setSelectedIndex(0);
		cbEffect.setBackForeColors(0x3861af, 0xFFFFFF);
		cbEffect.fillColor = 0x4583d4;
		next(false);
	}

	@Override
	public void onAddAgain() {
		next(false);
	}

	@Override
	public void onEvent(Event event) {
		switch (event.type) {
		case ControlEvent.PRESSED: {
			if (event.target == cbEffect && effect != cbEffect.getSelectedIndex()) {
				next(true);
			} else if (event.target == btnStartStop) {
				if (anim.isPaused) {
					anim.resume();
				} else {
					anim.pause();
				}
			}
			break;
		}
		}
	}

	/**
	 * shows next frame
	 */
	private void next(boolean changeEffect) {
		try {
			onRemove();
			Image img = new Image("images/alligator.gif");
			effect = cbEffect.getSelectedIndex();
			double scale = Settings.isIOS() ? 1.5 : 2; // ios has less opengl
														// memory
			int scaledcount = Settings.screenWidth / 500;
			System.out.println(scaledcount + " ," + Settings.screenWidth);
			for (int i = 0; i < scaledcount; i++) {
				img = img.scaledBy(scale, scale);
			}
			switch (effect) {
			case 1:
				img = img.scaledBy(scale, scale);
				break;
			case 2:
				img = img.smoothScaledBy(scale, scale);
				break;
			case 3:
				img = img.getRotatedScaledInstance(50, 90, -1);
				break;
			case 4:
				img = img.getTouchedUpInstance((byte) 50, (byte) 100);
				break;
			case 5:
				img.changeColors(0xFF31CE31, 0xFFFF00FF);
				break;
			case 6:
				img = img.getFadedInstance();
				break;
			case 7:
				img.applyColor2(Color.RED);
				img.getGraphics().dither(0, 0, img.getWidth(), img.getHeight());
				break;
			}
			if (Settings.isOpenGL) {
				img.applyChanges();
			}
			anim = new Animation(img, 120);
			anim.pauseIfNotVisible = true;
			add(anim, CENTER, CENTER, PREFERRED, PREFERRED);
			anim.start(Animation.LOOPS_UNLIMITED);
		} catch (Throwable e) {
			MessageBox.showException(e, true);
		}
	}

	@Override
	public void onRemove() {
		if (anim != null) {
			anim.stop();
			remove(anim);
			anim = null;
		}
	}
}

Do not forget to create a folder called "images" inside /src/main/resources and save the alligator.gif image inside it [images].

Methods

References

Last updated