Picture"

From Documentation
(Created page with "{{ZKSpreadsheetEssentials3PageHeader}} {{ZSS EE}} = Overview = We can add, move, and delete a picture of a Spreadsheet with <javadoc directory="zss">org.zkoss.zss.api.Range...")
 
m (correct highlight (via JWB))
 
(One intermediate revision by the same user not shown)
Line 19: Line 19:
 
</source>
 
</source>
  
A picture is a simple object that you can only get its ID and position. Current supported picture formats are listed in <javadoc directory="zss">org.zkoss.zss.api.model.Picture.Format</javadoc> which include <tt>EMF, WMF, PICT, JPEG, PNG</tt>, and <tt>DIB</tt>.
+
A picture is a simple object that you can only get its ID and position. Current supported picture formats are listed in <javadoc directory="zss">org.zkoss.zss.api.model.Picture.Format</javadoc> which include <code>EMF, WMF, PICT, JPEG, PNG</code>, and <code>DIB</code>.
  
The <javadoc directory="zss">SheetAnchor</javadoc> represents a picture's position on a sheet. When adding or moving a picture, you must provide one <tt>SheetAnchor</tt> to assign a picture's position. You can create a <tt>SheetAnchor</tt> by passing 4 index numbers, left top corner's and right bottom's row and column of an image. After you add a picture, you will get the newly-created <javadoc directory="zss">org.zkoss.zss.api.model.Picture</javadoc> object. You had better store it somewhere you can retrieve it back later if you plan to delete or move it. Otherwise, you can only get them from a <tt>Sheet</tt> method:
+
The <javadoc directory="zss">SheetAnchor</javadoc> represents a picture's position on a sheet. When adding or moving a picture, you must provide one <code>SheetAnchor</code> to assign a picture's position. You can create a <code>SheetAnchor</code> by passing 4 index numbers, left top corner's and right bottom's row and column of an image. After you add a picture, you will get the newly-created <javadoc directory="zss">org.zkoss.zss.api.model.Picture</javadoc> object. You had better store it somewhere you can retrieve it back later if you plan to delete or move it. Otherwise, you can only get them from a <code>Sheet</code> method:
 
<source lang='java'>
 
<source lang='java'>
 
public List<Picture> getPictures();
 
public List<Picture> getPictures();
Line 32: Line 32:
 
</source>
 
</source>
  
Then, you can pass <tt>AImage</tt> to <javadoc directory="zss">org.zkoss.zss.api.SheetOperationUtil</javadoc> to add a picture:
+
Then, you can pass <code>AImage</code> to <javadoc directory="zss">org.zkoss.zss.api.SheetOperationUtil</javadoc> to add a picture:
 
<source lang='java'>
 
<source lang='java'>
 
public static void addPicture(Range range, AImage image);
 
public static void addPicture(Range range, AImage image);
 
</source>
 
</source>
This method will create a <tt>SheetAnchor</tt> internally based on the image's size.
+
This method will create a <code>SheetAnchor</code> internally based on the image's size.
 
.
 
.
  
Line 45: Line 45:
 
If we click "Add", it will add a ZK logo picture and update picture items in the Listbox on the top right corner. Select a picture item in the listbox, enter destination row and column index in 2 Intboxes, then click "Move". The selected picture will be moved to specified position. The "Delete" button will delete the selected picture.
 
If we click "Add", it will add a ZK logo picture and update picture items in the Listbox on the top right corner. Select a picture item in the listbox, enter destination row and column index in 2 Intboxes, then click "Move". The selected picture will be moved to specified position. The "Delete" button will delete the selected picture.
  
Notice that there are 5 columns in the Listbox on the top right corner which display information about pictures we add. The ID is a picture's ID generated automatically by Spreadsheet. The row and column represents a picture's position of the left top corner in 0-based index and the last row and last column represents symmetric position of a picture (right bottom corner). For example, in the screenshot, the leftmost picture whose ID is "rid1_1", its left top corner is at "B7" represented in column index "1" and row index "6". Its right bottom corner is at "C12" represented in column index "11" and row index "2". These position information is stored in <tt>SheetAnchor</tt>. When adding or moving a picture, you must provide one <tt>SheetAnchor</tt> to assign a picture's position. The <tt>SheetOperationUtil</tt> provides methods to simplify this.
+
Notice that there are 5 columns in the Listbox on the top right corner which display information about pictures we add. The ID is a picture's ID generated automatically by Spreadsheet. The row and column represents a picture's position of the left top corner in 0-based index and the last row and last column represents symmetric position of a picture (right bottom corner). For example, in the screenshot, the leftmost picture whose ID is "rid1_1", its left top corner is at "B7" represented in column index "1" and row index "6". Its right bottom corner is at "C12" represented in column index "11" and row index "2". These position information is stored in <code>SheetAnchor</code>. When adding or moving a picture, you must provide one <code>SheetAnchor</code> to assign a picture's position. The <code>SheetOperationUtil</code> provides methods to simplify this.
  
 
Let's see this application's controller codes:
 
Let's see this application's controller codes:
  
<source lang='java' high='18, 29, 38'>
+
<source lang='java' highlight='18, 29, 38'>
  
 
public class PictureComposer extends SelectorComposer<Component> {
 
public class PictureComposer extends SelectorComposer<Component> {

Latest revision as of 12:52, 19 January 2022




Available in ZK Spreadsheet EE only

Overview

We can add, move, and delete a picture of a Spreadsheet with Range API:

public Picture addPicture(SheetAnchor anchor,byte[] image,Format format);

public void movePicture(SheetAnchor anchor,Picture picture);

public void deletePicture(Picture picture);

A picture is a simple object that you can only get its ID and position. Current supported picture formats are listed in Picture.Format which include EMF, WMF, PICT, JPEG, PNG, and DIB.

The SheetAnchor represents a picture's position on a sheet. When adding or moving a picture, you must provide one SheetAnchor to assign a picture's position. You can create a SheetAnchor by passing 4 index numbers, left top corner's and right bottom's row and column of an image. After you add a picture, you will get the newly-created Picture object. You had better store it somewhere you can retrieve it back later if you plan to delete or move it. Otherwise, you can only get them from a Sheet method:

	public List<Picture> getPictures();

Then, use its ID or position to identify a picture.

If you think passing byte array of an image is troublesome for you, you can use AImage. It has several convenient constructors to create a object for an image like:

AImage image = new AImage(WebApps.getCurrent().getResource("/zklogo.png"));

Then, you can pass AImage to SheetOperationUtil to add a picture:

	public static void addPicture(Range range, AImage image);

This method will create a SheetAnchor internally based on the image's size. .

Example

The screenshot below is a application that can add, move and delete a picture.

Zss-essentials-picture.png

If we click "Add", it will add a ZK logo picture and update picture items in the Listbox on the top right corner. Select a picture item in the listbox, enter destination row and column index in 2 Intboxes, then click "Move". The selected picture will be moved to specified position. The "Delete" button will delete the selected picture.

Notice that there are 5 columns in the Listbox on the top right corner which display information about pictures we add. The ID is a picture's ID generated automatically by Spreadsheet. The row and column represents a picture's position of the left top corner in 0-based index and the last row and last column represents symmetric position of a picture (right bottom corner). For example, in the screenshot, the leftmost picture whose ID is "rid1_1", its left top corner is at "B7" represented in column index "1" and row index "6". Its right bottom corner is at "C12" represented in column index "11" and row index "2". These position information is stored in SheetAnchor. When adding or moving a picture, you must provide one SheetAnchor to assign a picture's position. The SheetOperationUtil provides methods to simplify this.

Let's see this application's controller codes:

public class PictureComposer extends SelectorComposer<Component> {

	@Wire
	private Intbox toRowBox;
	@Wire
	private Intbox toColumnBox;
	@Wire
	private Spreadsheet ss;
	@Wire
	private Listbox pictureListbox;
	
	private ListModelList<Picture> pictureList = new ListModelList<Picture>();

	@Listen("onClick = #addButton")
	public void addByUtil() {
		Range selection = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		try{
			SheetOperationUtil.addPicture(selection,
				new AImage(WebApps.getCurrent().getResource("/zklogo.png")));
			refreshPictureList();
		}catch(IOException e){
			System.out.println("cannot add a picture for "+ e);
		}
	}
	
	@Listen("onClick = #deleteButton")
	public void deleteByUtil() {
		if (pictureListbox.getSelectedItem() != null){
			SheetOperationUtil.deletePicture(Ranges.range(ss.getSelectedSheet()),
					(Picture)pictureListbox.getSelectedItem().getValue());
			refreshPictureList();
		}
	}
	
	@Listen("onClick = #moveButton")
	public void moveByUtil(){
		if (pictureListbox.getSelectedItem() != null){
			SheetOperationUtil.movePicture(Ranges.range(ss.getSelectedSheet()),
					(Picture)pictureListbox.getSelectedItem().getValue(),
					toRowBox.getValue(), toColumnBox.getValue());
			refreshPictureList();
		}
	}
	
	private void refreshPictureList(){
		pictureList.clear();
		pictureList.addAll(ss.getSelectedSheet().getPictures());
		pictureListbox.setModel(pictureList);
	}
}



All source code listed in this book is at Github.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.