I have problem with inserting Editbox in StringGrid. I have one StringGrid and one Editbox on my form. The editbox shows just fine in the selected col and row but i can insert just one number in the selected cell... can anyone explain me how to put more numbers in there?
tnx!
//Editbox on change event
void __fastcall TForm3::StringGridEditChange(TObject *Sender)
{
int x,y;
x = Form3->VariableGrid->Col;
y = Form3->VariableGrid->Row;
if (VariableGrid->Col !=2 )
{
VariableGrid->Col = 2;
}
if (VariableGrid->Row <1 )
{
VariableGrid->Row = 1;
ShowMessage("Prevention!");
}
VariableGrid->Cells[VariableGrid->Col][VariableGrid->Row] = StringGridEdit->Text;
StringGridEdit->Visible = false;
VariableGrid->SetFocus();
//StringGrid on selectCell event
void __fastcall TForm3::VariableGridSelectCell(TObject *Sender, int ACol, int ARow,
bool &CanSelect)
{
if(ACol == 2 && ARow>0)
{
TRect R = VariableGrid->CellRect(ACol, ARow);
R.Left += VariableGrid->Left;
R.Right += VariableGrid->Left;
R.Top += VariableGrid->Top;
R.Bottom += VariableGrid->Top;
StringGridEdit->Left = R.Left + 1;
StringGridEdit->Top = R.Top + 1;
StringGridEdit->Width = (R.Right + 1) - R.Left;
StringGridEdit->Height = (R.Bottom + 1) - R.Top;
//Show the editbox
StringGridEdit->Visible = true;
StringGridEdit->SetFocus();
}
CanSelect = true;
}
in C++ Builder XE2
Related
I have two images with similar sizes that show similar scenes. How can we show two images in two frames and when panning or zooming in the left image, it pans and zooms in the right one? I don't want to concatenate the images though.
Is there a solution to do this? Both python or c++ OpenCV are fine.
About zoom in/out:
The basic idea is deciding the scale changed every time on mouse wheel. After you get the current scale (v.s. origin image) and correct region of image you want to show on screen, you can get the position and length of rectangle on scaled image. So you can draw this rectangle on scaled image.
In my github,checking OnMouseWheel () and RefreshSrcView () in Fastest_Image_Pattern_Matching/ELCVMatchTool/ELCVMatchToolDlg.cpp may give what you want.
About showing two images simutaneouly with same region:
use two picture boxes with MFC framework or other UI builder.
or use two cv::namedWindow () without framework
Effect:
Part of the code:
BOOL CELCVMatchToolDlg::OnMouseWheel (UINT nFlags, short zDelta, CPoint pt)
{
POINT pointCursor;
GetCursorPos (&pointCursor);
ScreenToClient (&pointCursor);
// TODO: 在此加入您的訊息處理常式程式碼和 (或) 呼叫預設值
if (zDelta > 0)
{
if (m_iScaleTimes == MAX_SCALE_TIMES)
return TRUE;
else
m_iScaleTimes++;
}
if (zDelta < 0)
{
if (m_iScaleTimes == MIN_SCALE_TIMES)
return TRUE;
else
m_iScaleTimes--;
}
CRect rect;
//GetWindowRect (rect);
GetDlgItem (IDC_STATIC_SRC_VIEW)->GetWindowRect (rect);//重要
if (m_iScaleTimes == 0)
g_dCompensationX = g_dCompensationY = 0;
int iMouseOffsetX = pt.x - (rect.left + 1);
int iMouseOffsetY = pt.y - (rect.top + 1);
double dPixelX = (m_hScrollBar.GetScrollPos () + iMouseOffsetX + g_dCompensationX) / m_dNewScale;
double dPixelY = (m_vScrollBar.GetScrollPos () + iMouseOffsetY + g_dCompensationY) / m_dNewScale;
m_dNewScale = m_dSrcScale * pow (SCALE_RATIO, m_iScaleTimes);
if (m_iScaleTimes != 0)
{
int iWidth = m_matSrc.cols;
int iHeight = m_matSrc.rows;
m_hScrollBar.SetScrollRange (0, int (m_dNewScale * iWidth - m_dSrcScale * iWidth) - 1 + BAR_SIZE);
m_vScrollBar.SetScrollRange (0, int (m_dNewScale * iHeight - m_dSrcScale * iHeight) - 1 + BAR_SIZE);
int iBarPosX = int (dPixelX * m_dNewScale - iMouseOffsetX + 0.5);
m_hScrollBar.SetScrollPos (iBarPosX);
m_hScrollBar.ShowWindow (SW_SHOW);
g_dCompensationX = -iBarPosX + (dPixelX * m_dNewScale - iMouseOffsetX);
int iBarPosY = int (dPixelY * m_dNewScale - iMouseOffsetY + 0.5);
m_vScrollBar.SetScrollPos (iBarPosY);
m_vScrollBar.ShowWindow (SW_SHOW);
g_dCompensationY = -iBarPosY + (dPixelY * m_dNewScale - iMouseOffsetY);
//滑塊大小
SCROLLINFO infoH;
infoH.cbSize = sizeof (SCROLLINFO);
infoH.fMask = SIF_PAGE;
infoH.nPage = BAR_SIZE;
m_hScrollBar.SetScrollInfo (&infoH);
SCROLLINFO infoV;
infoV.cbSize = sizeof (SCROLLINFO);
infoV.fMask = SIF_PAGE;
infoV.nPage = BAR_SIZE;
m_vScrollBar.SetScrollInfo (&infoV);
//滑塊大小
}
else
{
m_hScrollBar.SetScrollPos (0);
m_hScrollBar.ShowWindow (SW_HIDE);
m_vScrollBar.SetScrollPos (0);
m_vScrollBar.ShowWindow (SW_HIDE);
}
RefreshSrcView ();
return CDialogEx::OnMouseWheel (nFlags, zDelta, pt);
}
I'm trying to click and drag on a TCanvas and change the background color to clHighlight as I drag down and change it back to clWhite if I were to reverse the drag direction. Pretty much what would happen were you to click on this text you are reading and drag down or up, same idea anyhow. I've gleaned what I could from this link
and I've edited the code below extensively since I first posted this, the code below now does what I set out to do except that the text in the selection can get messed up if you don't drag at an even pace, which is unacceptable. If you really move the mouse up and down quickly it will definitely get messed up. If you move the mouse
at a steady pace it works well which makes me fear there may not be a solution to this problem. I'm continuing to look at that, if anyone has any suggestions that'd be great. I've also gotten a couple "Out of resources" errors, not sure what's up with that yet, I'm freeing the Bitmaps, no other resources are involved that I'm aware of.
The following code is in a MouseMove event.srect is the selected rectangle in the TImage's Canvas. drect is the same srect translated to 0,0. bm1 contains the selected rect copied from the TImage's Canvas using CopyRect. bm2 contains bm1 with the clWhite (background) changed to clHighlight (or the reverse) using BrushCopy. BitBlt is then used to copy bm2 back into the original TImage's selected rectangle.
// vp is derived from TImage
if (Y > sel_data->_last_y)
{
TRect srect = Rect(sel_data->_rect.Left,sel_data->_last_y,sel_data->_rect.Right, Y);
TRect drect = Rect(sel_data->_rect.Left,0,sel_data->_rect.Right, sel_data->_rect.Height() - 1);
Graphics::TBitmap* bm1 = new Graphics::TBitmap;
bm1->Width = srect.Width();
bm1->Height = srect.Height();
Graphics::TBitmap* bm2 = new Graphics::TBitmap;
bm2->Width = srect.Width();
bm2->Height = srect.Height();
bm1->Canvas->CopyRect(drect, vp->Canvas, srect);
bm2->Canvas->Brush->Color = clHighlight;
bm2->Canvas->BrushCopy(drect, bm1, drect, clWindow);
BitBlt(vp->Canvas->Handle, srect.Left, srect.Top, srect.Width(), srect.Height(),
bm2->Canvas->Handle, 0, 0, SRCCOPY);
vp->Refresh();
delete bm1;
delete bm2;
}
else if (Y < sel_data->_last_y)
{
TRect srect = Rect(sel_data->_rect.Left, Y,sel_data->_rect.Right, sel_data->_last_y);
TRect drect = Rect(sel_data->_rect.Left,0,sel_data->_rect.Right, sel_data->_rect.Height() - 1);
Graphics::TBitmap* bm1 = new Graphics::TBitmap;
bm1->Width = srect.Width();
bm1->Height = srect.Height();
Graphics::TBitmap* bm2 = new Graphics::TBitmap;
bm2->Width = srect.Width();
bm2->Height = srect.Height();
bm1->Canvas->CopyRect(drect, vp->Canvas, srect);
bm2->Canvas->Brush->Color = clWhite;
bm2->Canvas->BrushCopy(drect, bm1, drect, clHighlight);
int w = srect.Width();
int h = srect.Height();
BitBlt(vp->Canvas->Handle, srect.Left, srect.Top, w, h, bm2->Canvas->Handle, 0, 0, SRCCOPY);
vp->Refresh();
delete bm1;
delete bm2;
}
sel_data->_last_y = Y;
}
This is what I ended up with, all the glitches have been fixed as far as I know, I'm including the entire MouseMove event handler. The above mentioned link clued me in that I needed a CopyRect, a BrushCopy, and then a BitBlt.
void __fastcall Tviewer_ui::viewerPageMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
if (_selecting)
{
TColor to_color = 0;
TColor from_color = 0;
Tviewer_page* vp = static_cast<Tviewer_page *>(Sender);
Tsel_data* sel_data = &vp->_sel_datas[vp->_c_sel];
// Scroll window if needed
int sb_pos = _scrollBox->VertScrollBar->Position;
if (Y > sb_pos - vp->_top + _scrollBox->Height - 10)
_scrollBox->VertScrollBar->Position += 10;
else
if (Y < sb_pos - vp->_top + 10)
_scrollBox->VertScrollBar->Position -= 10;
int y1 = 0;
int y2 = 0;
if (Y > sel_data->_start_y)
{
if (Y > sel_data->_last_y)
{
y1 = sel_data->_last_y;
y2 = Y;
from_color = clWhite;
to_color = clHighlight;
}
else if (Y < sel_data->_last_y)
{
y1 = Y;
y2 = sel_data->_last_y;
from_color = clHighlight;
to_color = clWhite;
}
sel_data->_rect.Bottom = Y;
}
else if (Y < sel_data->_start_y)
{
if (Y < sel_data->_last_y)
{
y1 = Y;
y2 = sel_data->_last_y;
from_color = clWhite;
to_color = clHighlight;
}
else if (Y > sel_data->_last_y)
{
y1 = sel_data->_last_y;
y2 = Y;
from_color = clHighlight;
to_color = clWhite;
}
sel_data->_rect.Top = Y;
}
int height = abs(y1 - y2);
if (height > 0)
{
TRect srect = Rect(sel_data->_rect.Left, y1, sel_data->_rect.Right, y2);
int width = srect.Width();
TRect drect = Rect(sel_data->_rect.Left,0,sel_data->_rect.Right, height);
Graphics::TBitmap* bm1 = new Graphics::TBitmap;
bm1->Width = width;
bm1->Height = height;
Graphics::TBitmap* bm2 = new Graphics::TBitmap;
bm2->Width = width;
bm2->Height = height;
bm1->Canvas->CopyRect(drect, vp->Canvas, srect);
bm2->Canvas->Brush->Color = to_color;
bm2->Canvas->BrushCopy(drect, bm1, drect, from_color);
BitBlt(vp->Canvas->Handle, srect.Left, srect.Top, width, height, bm2->Canvas->Handle, 0, 0, SRCCOPY);
vp->Refresh();
delete bm1;
delete bm2;
}
sel_data->_last_y = Y;
}
}
I'm using TIdHTTP to make HTTP GET requests to get wiki pages (executed until x >= 9 times using a for loop statement).
I am trying to use TStopWatch to measure the HTTP round trip request/response time in milliseconds of each GET requests.
I have tried somes methods, but, I still in doubt that it was right or not. So, please tell me what's the right method to do that. Thank you.
Btw, the code I have is:
//---------------------------------------------------------------------------
int MSec = 0;
TTimeSpan TTSpan;
TDateTime StartTime;
DWORD AStartTime = 0;
DWORD CStartTime = 0;
String ACount, Codes, Milli, Result;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
IdHTTP->ConnectTimeout = 3048;
IdHTTP->ReadTimeout = 2048;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdHTTPWork(TObject *ASender, TWorkMode AWorkMode, __int64 AWorkCount)
{
if (AWorkMode == wmRead)
ACount = "size: [" + String(AWorkCount) + " bytes]";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WorkClick(TObject *Sender)
{
static int Number = 1;
TStopwatch SWatch = TStopwatch::StartNew();
// CStartTime = GetTickCount();
// DWORD Start = timeGetTime();
// AStartTime = GetTickCount();
// CStartTime = TTSpan.Milliseconds;
Work->Enabled = false;
ListBox->Items->Clear();
ListBox->Items->BeginUpdate();
for (int i = 0; i < 9; i++)
{
try
{
SWatch.Start();
try
{
IdHTTP->Get("http://en.wikipedia.org/");
Sleep(1000);
}
catch (const Exception &E)
{
Label1->Caption = E.Message;
}
SWatch.Stop();
TTSpan = SWatch.Elapsed;
MSec = TTSpan.Milliseconds;
// MSec = TTSpan.Milliseconds / 10;
// MSec = SWatch.ElapsedMilliseconds / 10;
// TTSpan = SWatch.Elapsed;
// MSec = TTSpan.Milliseconds - CStartTime;
Milli = " time: [" + String(MSec) + " ms]";
Codes = " code: [" + IdHTTP->Response->ResponseText.LowerCase() + "]";
ListBox->Items->Add("(" + String(Number++) + "). " + ACount + Codes + Milli);
// AStartTime = GetTickCount();
// ListBox->Items->Add("(" + String(Number++) + "). " + ACount + Codes + Result.sprintf(L" time: [%i ms]",(aStartTime-bStartTime)));
}
__finally
{
Work->Enabled = true;
IdHTTP->Disconnect(true);
ListBox->Items->EndUpdate();
}
}
}
There is a perspective image issue when I read a picture that is taken from the camera. When the direction is north, the picture looks like needed to be rotated 270 degrees. When the direction is east, picture should be rotated 180 degrees. But it's good when the direction is west. I tried getMetaData().getKeyValue("orientation") in EncodedImage for producing a good rotating formula, but it returned empty string. Please help me how to solve this problem.
Found solution here:
https://gist.github.com/3788313
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
public class ExifRotate {
/**
* Flip the image horizontally.
*/
public static final int FLIP_H = 1;
/**
* Flip the image vertically.
*/
public static final int FLIP_V = 2;
/**
* Flip the image horizontally and vertically.
*/
public static final int FLIP_HV = 3;
/**
* Rotate the image 90 degrees clockwise.
*/
public static final int FLIP_90CW = 4;
/**
* Rotate the image 90 degrees counter-clockwise.
*/
public static final int FLIP_90CCW = 5;
/**
* Rotate the image 180 degrees.
*/
public static final int FLIP_180 = 6;
private final static int read2bytes(InputStream in) throws IOException {
return in.read() << 8 | in.read();
}
private final static int readByte(InputStream in) throws IOException {
return in.read();
}
public static Bitmap readImageFromFile(String filename, int width, int height) throws IOException {
EncodedImage img = null;
byte[] data = null;
FileConnection file = null;
try {
file = (FileConnection) Connector.open(filename, Connector.READ);
int fileSize = (int) file.fileSize();
if (fileSize == 0) {
throw new IOException("File is empty");
}
data = new byte[fileSize];
InputStream input = file.openInputStream();
input.read(data);
input.close();
img = EncodedImage.createEncodedImage(data, 0, data.length);
int orientation = -1;
if ( filename.toLowerCase().endsWith(".jpg") || filename.toLowerCase().endsWith(".jpeg")) {
ByteArrayInputStream is = new ByteArrayInputStream( data );
orientation = getRotation(is);
}
if ( orientation == 2 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_H);
} else if ( orientation == 3 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_180);
} else if ( orientation == 4 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_V);
} else if ( orientation == 5 ) {
Bitmap tmp = rotateBitmap(img.getBitmap(), ImageUtil.FLIP_H);
tmp = rotateBitmap(tmp, ImageUtil.FLIP_90CCW);
return tmp;
} else if ( orientation == 6 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_90CW);
} else if ( orientation == 7 ) {
Bitmap tmp = rotateBitmap(img.getBitmap(), ImageUtil.FLIP_H);
tmp = rotateBitmap(tmp, ImageUtil.FLIP_90CW);
return tmp;
} else if ( orientation == 8 ) {
return rotateBitmap(img.getBitmap(), ImageUtil.FLIP_90CCW);
} else {
return img.getBitmap();
}
} finally {
if (file != null) {
try { file.close(); }
catch(Exception ex){}
}
}
}
public static int getRotation(InputStream in) throws IOException {
int [] exif_data = new int[100];
int n_flag = 0, set_flag = 0;
int is_motorola = 0;
/* Read File head, check for JPEG SOI + Exif APP1 */
for (int i = 0; i < 4; i++)
exif_data[i] = readByte(in);
if (exif_data[0] != 0xFF || exif_data[1] != 0xD8 || exif_data[2] != 0xFF || exif_data[3] != 0xE1)
return -2;
/* Get the marker parameter length count */
int length = read2bytes(in);
// exif_data = new int[length];
/* Length includes itself, so must be at least 2 */
/* Following Exif data length must be at least 6 */
if (length < 8)
return -1;
length -= 8;
/* Read Exif head, check for "Exif" */
for (int i = 0; i < 6; i++)
exif_data[i] = in.read();
if (exif_data[0] != 0x45 || exif_data[1] != 0x78 || exif_data[2] != 0x69 || exif_data[3] != 0x66 || exif_data[4] != 0 || exif_data[5] != 0)
return -1;
/* Read Exif body */
length = length > exif_data.length ? exif_data.length : length;
for (int i = 0; i < length; i++)
exif_data[i] = in.read();
if (length < 12)
return -1; /* Length of an IFD entry */
/* Discover byte order */
if (exif_data[0] == 0x49 && exif_data[1] == 0x49)
is_motorola = 0;
else if (exif_data[0] == 0x4D && exif_data[1] == 0x4D)
is_motorola = 1;
else
return -1;
/* Check Tag Mark */
if (is_motorola == 1) {
if (exif_data[2] != 0)
return -1;
if (exif_data[3] != 0x2A)
return -1;
} else {
if (exif_data[3] != 0)
return -1;
if (exif_data[2] != 0x2A)
return -1;
}
/* Get first IFD offset (offset to IFD0) */
int offset;
if (is_motorola == 1) {
if (exif_data[4] != 0)
return -1;
if (exif_data[5] != 0)
return -1;
offset = exif_data[6];
offset <<= 8;
offset += exif_data[7];
} else {
if (exif_data[7] != 0)
return -1;
if (exif_data[6] != 0)
return -1;
offset = exif_data[5];
offset <<= 8;
offset += exif_data[4];
}
if (offset > length - 2)
return -1; /* check end of data segment */
/* Get the number of directory entries contained in this IFD */
int number_of_tags;
if (is_motorola == 1) {
number_of_tags = exif_data[offset];
number_of_tags <<= 8;
number_of_tags += exif_data[offset + 1];
} else {
number_of_tags = exif_data[offset + 1];
number_of_tags <<= 8;
number_of_tags += exif_data[offset];
}
if (number_of_tags == 0)
return -1;
offset += 2;
/* Search for Orientation Tag in IFD0 */
for (;;) {
if (offset > length - 12)
return -1; /* check end of data segment */
/* Get Tag number */
int tagnum;
if (is_motorola == 1) {
tagnum = exif_data[offset];
tagnum <<= 8;
tagnum += exif_data[offset + 1];
} else {
tagnum = exif_data[offset + 1];
tagnum <<= 8;
tagnum += exif_data[offset];
}
if (tagnum == 0x0112)
break; /* found Orientation Tag */
if (--number_of_tags == 0)
return -1;
offset += 12;
}
/*
* if (set_flag==1) { Set the Orientation value if (is_motorola==1) {
* exif_data[offset+2] = 0; Format = unsigned short (2 octets)
* exif_data[offset+3] = 3; exif_data[offset+4] = 0; Number Of
* Components = 1 exif_data[offset+5] = 0; exif_data[offset+6] = 0;
* exif_data[offset+7] = 1; exif_data[offset+8] = 0; exif_data[offset+9]
* = set_flag; exif_data[offset+10] = 0; exif_data[offset+11] = 0; }
* else { exif_data[offset+2] = 3; Format = unsigned short (2 octets)
* exif_data[offset+3] = 0; exif_data[offset+4] = 1; Number Of
* Components = 1 exif_data[offset+5] = 0; exif_data[offset+6] = 0;
* exif_data[offset+7] = 0; exif_data[offset+8] = set_flag;
* exif_data[offset+9] = 0; exif_data[offset+10] = 0;
* exif_data[offset+11] = 0; } }
*/
// else {
/* Get the Orientation value */
if (is_motorola == 1) {
if (exif_data[offset + 8] != 0)
return -1;
set_flag = exif_data[offset + 9];
} else {
if (exif_data[offset + 9] != 0)
return -1;
set_flag = exif_data[offset + 8];
}
if (set_flag > 8)
return -1;
// }
/* Write out Orientation value */
if (n_flag == 1)
System.out.println("set_flag " + set_flag);
else
System.out.println("set_flag " + set_flag);
return set_flag;
}
public static Bitmap rotateBitmap(Bitmap src, int operation) {
int width = src.getWidth();
int height = src.getHeight();
int[] inPixels = new int[width*height];
src.getARGB(inPixels, 0, width, 0, 0, width, height);
int x = 0, y = 0;
int w = width;
int h = height;
int newX = 0;
int newY = 0;
int newW = w;
int newH = h;
switch (operation) {
case FLIP_H:
newX = width - (x + w);
break;
case FLIP_V:
newY = height - (y + h);
break;
case FLIP_HV:
newW = h;
newH = w;
newX = y;
newY = x;
break;
case FLIP_90CW:
newW = h;
newH = w;
newX = height - (y + h);
newY = x;
break;
case FLIP_90CCW:
newW = h;
newH = w;
newX = y;
newY = width - (x + w);
break;
case FLIP_180:
newX = width - (x + w);
newY = height - (y + h);
break;
}
int[] newPixels = new int[newW * newH];
int index, newRow, newCol, newIndex;
if ( operation == FLIP_H ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = row;
newCol = w - col - 1;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_V ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = h - row - 1;
newCol = col;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_HV ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = col;
newCol = row;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_90CW ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = col;
newCol = h - row - 1;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_90CCW ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = w - col - 1;
newCol = row;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
} else if ( operation == FLIP_180 ) {
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
index = row * width + col;
newRow = h - row - 1;
newCol = w - col - 1;
newIndex = newRow * newW + newCol;
newPixels[newIndex] = inPixels[index];
}
}
}
Bitmap dst = new Bitmap( newW, newH );
dst.setARGB(newPixels, 0, newW, 0, 0, newW, newH);
return dst;
}
}
I am adding multiple label fields in HFM, the text of which is coming from a String[]. Suppose there are 5 labels. I want the labels in Left, HCentre and Right of HFM. I m creating the labelfields like,
String[] labels = {------};
for( int i=0;labels.length;i++)
{
LabelField labelField = new LabelField(labels[i],Field.FOCUSABLE|Field.ACTION_INVOKE){
public void paint(Graphics g) {
-------
-------
}
protected void layout( int width, int height ){
super.layout(Display.getWidth()/3, getContentHeight());
setExtent(Display.getWidth()/3, getContentHeight());
//setPosition(Display.getWidth()/3,0);
};
HFM.add(labelField);
}
But it is not coming in the format I want.
How to do it? am I doing something wrong in Field's layout()? Please correct me if I am wrong.
I am attaching the screenshot of what I am getting now, but the alignment I want is 1st LabelField to the Left of HFM, 2nd at the Centre and 3rd at the Right of HFM, but only the thing is I am getting the LabelTexts from a String[].
According to your requirement , this Sample class will help you ..
import net.rim.device.api.ui.*;
public class JustifiedEvenlySpacedHorizontalFieldManager extends Manager
{
private static final int SYSTEM_STYLE_SHIFT = 32;
public JustifiedEvenlySpacedHorizontalFieldManager()
{
this( 0 );
}
public JustifiedEvenlySpacedHorizontalFieldManager( long style )
{
super( USE_ALL_WIDTH | style );
}
protected void sublayout( int width, int height )
{
int availableWidth = width;
int numFields = getFieldCount();
int maxPreferredWidth = 0;
int maxHeight = 0;
// There may be a few remaining pixels after dividing up the space
// we must split up the space between the first and last buttons
int fieldWidth = width / numFields;
int firstFieldExtra = 0;
int lastFieldExtra = 0;
int unUsedWidth = width - fieldWidth * numFields;
if( unUsedWidth > 0 ) {
firstFieldExtra = unUsedWidth / 2;
lastFieldExtra = unUsedWidth - firstFieldExtra;
}
int prevRightMargin = 0;
// Layout the child fields, and calculate the max height
for( int i = 0; i < numFields; i++ ) {
int nextLeftMargin = 0;
if( i < numFields - 1 ) {
Field nextField = getField( i );
nextLeftMargin = nextField.getMarginLeft();
}
Field currentField = getField( i );
int leftMargin = i == 0 ? currentField.getMarginLeft() : Math.max( prevRightMargin, currentField.getMarginLeft() ) / 2;
int rightMargin = i < numFields - 1 ? Math.max( nextLeftMargin, currentField.getMarginRight() ) / 2 : currentField.getMarginRight();
int currentVerticalMargins = currentField.getMarginTop() + currentField.getMarginBottom();
int currentHorizontalMargins = leftMargin + rightMargin;
int widthForButton = fieldWidth;
if( i == 0 ) {
widthForButton = fieldWidth + firstFieldExtra;
} else if( i == numFields -1 ) {
widthForButton = fieldWidth + lastFieldExtra;
}
layoutChild( currentField, widthForButton - currentHorizontalMargins, height - currentVerticalMargins );
maxHeight = Math.max( maxHeight, currentField.getHeight() + currentVerticalMargins );
prevRightMargin = rightMargin;
nextLeftMargin = 0;
}
// Now position the fields, respecting the Vertical style bits
int usedWidth = 0;
int y;
prevRightMargin = 0;
for( int i = 0; i < numFields; i++ ) {
Field currentField = getField( i );
int marginTop = currentField.getMarginTop();
int marginBottom = currentField.getMarginBottom();
int marginLeft = Math.max( currentField.getMarginLeft(), prevRightMargin );
int marginRight = currentField.getMarginRight();
switch( (int)( ( currentField.getStyle() & FIELD_VALIGN_MASK ) >> SYSTEM_STYLE_SHIFT ) ) {
case (int)( FIELD_BOTTOM >> SYSTEM_STYLE_SHIFT ):
y = maxHeight - currentField.getHeight() - currentField.getMarginBottom();
break;
case (int)( FIELD_VCENTER >> SYSTEM_STYLE_SHIFT ):
y = marginTop + ( maxHeight - marginTop - currentField.getHeight() - marginBottom ) >> 1;
break;
default:
y = marginTop;
}
setPositionChild( currentField, usedWidth + marginLeft, y );
usedWidth += currentField.getWidth() + marginLeft;
prevRightMargin = marginRight;
}
setExtent( width, maxHeight );
}
}
please have a look on this link :-
[http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-managers/ta-p/488276][1]
you will get all kinds of Custom Components
Cheers ..!!!
[1]: http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-managers/ta-p/488276