How to add a vertial scrollbar in Canvas? - java-me

I am developing a Java ME application using Canvas. The details displayed on the Canvas are plentiful, so I need to scroll down the screen to view the further details.
But how can I add a vertical scrollbar on a Canvas?

I think that the scrollbar should be drawn inside the paint method. And you implement the keyReleased method when clicking the arrow of the scrollbar. To implement the scrolling I would prefer call repaint(); and I will manage the paint method with boolean or some other type of private variable.

Consider your canvas as the source and the scroll bar as destination use the following formula to find the scroll bar position
Target point X co-ordinate: tX = taX + ((tW * (sX - saX)) / sW)
Target point Y co-ordinate: tY = taY - ((tH * (saY - sY)) / sH)
saX : Source axis start X coordinate
saY : Source axis start Y coordinate
sW : Souce Width
sH : Source Height
taX : Target axis start X coordinate
taY : Target axis start Y coordinate
tW : Target Width
tH : Target Height
The target height would be the height of the canvas. You must draw the scroll bar as the topmost element.

Related

AndroidPlot - Labels and text

I am a non-developer product manager for an application built in both Android and iOS. We have a bar graph in iOS that provides text for the content of the graph. It displays Totals for each bar, and percentages for each segment of each bar.
In Android, using AndroidPlot (so I understand) we just display the bars with different color segments and no percent totals or totals. I am told by the developer that we can't show more.
I would display the images here, but stackoverflow tells me I don't have enough reputation points to do this. I have created a link to my dropbox with the images https://www.dropbox.com/sh/2uocm5bn79rerbe/AAB7s9QEEYIRIgXhKbUAaOyDa
Is it possible to use AndroidPlot to emulate this iOS chart or at least represent to same information to the end user?
Your developer is more or less correct but you have options. Androidplot's BarRenderer by default provides only an optional label at the top of each bar, which in your iOS images is occupied by the "available", "new", "used" and "rent" label. That label appears to be unused in your Android screenshot so one option would be to utilize those labels do display your totals.
As far as exactly matching the iOS implementation with Androidplot, the missing piece is the ability to add additional labels horizontally and vertically along the side of each bar. You can extend BarRenderer to do this by overriding it's onRender(...) method. Here's a link for your developer that shows where in the code he'll want to modify onRender(...).
I'd suggest these modifications to add the vertical labels:
Invoke Canvas.save(Canvas.ALL_SAVE_FLAG) to store the default orientation of the Canvas.
Use Canvas.translate(leftX, bottom) to center on the bottom left point of the bar
Rotate the Canvas 90 degrees using Canvas.rotate(90) to enable vertical text drawing
Draw whatever text is needed along the side of the plot; 0,0 now corresponds to the bottom left corner of the bar so start there when invoking canvas.drawText(x,y).
Invoke Canvas.restore() to restore the canvas' original orientation.
After implementing the above, adding horizontal "%" labels should be self evident but if you run into trouble feel free to ask more questions along the way.
UPDATE:
Here's a very basic implementation of the above. First the drawVerticalText method:
/**
*
* #param canvas
* #param paint paint used to draw the text
* #param text the text to be drawn
* #param x x-coord of where the text should be drawn
* #param y y-coord of where the text should be drawn
*/
protected void drawVerticalText(Canvas canvas, Paint paint, String text, float x, float y) {
// record the state of the canvas before the draw:
canvas.save(Canvas.ALL_SAVE_FLAG);
// center the canvas on our drawing coords:
canvas.translate(x, y);
// rotate into the desired "vertical" orientation:
canvas.rotate(-90);
// draw the text; note that we are drawing at 0, 0 and *not* x, y.
canvas.drawText(text, 0, 0, paint);
// restore the canvas state:
canvas.restore();
}
All that's left is to invoke this method where necessary. In your case it should be done once per BarGroup and should maintain a consistent position on the y axis. I added the following code to the STACKED case in BarRenderer.onRender(...), immediately above the break:
// needed some paint to draw with so I'll just create it here for now:
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(PixelUtils.spToPix(20));
drawVerticalText(
canvas,
paint,
"test",
barGroup.leftX,
basePositionY - PixelUtils.dpToPix(50)); // offset so the text doesnt intersect with the origin
Here's a screenshot of the result...sorry it's so huge:
Personally, I don't care for the fixed y-position of these vertical labels and would prefer them to float along the upper part of the bars. To accomplish this I modify my invocation of drawVerticalText(...) to look like this:
// needed some paint to draw with so I'll just create it here for now:
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(PixelUtils.spToPix(20));
// right-justify the text so it doesnt extend beyond the top of the bar
paint.setTextAlign(Paint.Align.RIGHT);
drawVerticalText(
canvas,
paint,
"test",
barGroup.leftX,
bottom);
Which produces this result:

JavaFX: how to set correct tooltip position?

I have a TextField for which I want a Tooltip to be shown under some circumstances.
After performing checks I run the followig code:
textFieldUsername.setTooltip(new Tooltip("Enter username!"));
textFieldUsername.getTooltip().setAutoHide(true);
textFieldUsername.getTooltip().show(textFieldUsername, 1, 1);
So when somebody tries to login with empty username he gets a prompting Tooltip over the "username" TextField.
But when it comes to action, the Tooltip pops up in the top left corner of the screen.
Should I calculate coords of my scene, then add my TextField coords to them, or there is a way to set these 1, 1 arguments from the call of show() to be relative to the TextField position?
I think the coordinates are always relative to the screen. To calculate component coordinates you need to incorporate scene and window coordinates.
Point2D p = label.localToScene(0.0, 0.0);
label.getTooltip().show(label,
p.getX() + label.getScene().getX() + label.getScene().getWindow().getX(),
p.getY() + label.getScene().getY() + label.getScene().getWindow().getY());

Display UIMenuController in UIBarButton Click

I am trying to display a UIMenuController in a Toolbar button. I have the code below but I am unsure as to what should be in the "SetTargetRect" method.
What should I put in the SetTargetRect method to display the Menu?
Is there anything else that is missing from this code?
ToolbarItems = new UIBarButtonItem[] {
new UIBarButtonItem ("Sort", UIBarButtonItemStyle.Bordered, (sender, e) => {
var menu = UIMenuController.SharedMenuController;
menu.MenuItems = new UIMenuItem[] {
new UIMenuItem ("Current", new Selector ("SortRaceEntrants")),
new UIMenuItem ("Movers", new Selector ("SortRaceEntrants")),
new UIMenuItem ("Opening", new Selector ("SortRaceEntrants")),
new UIMenuItem ("Number", new Selector ("SortRaceEntrants"))
};
//menu.SetTargetRect ();
menu.SetMenuVisible (true, true);
}),
};
UIBarButtonItem inherits from UIBarItem and NSObject, so finding a frame for them is not easy if not impossible. UINavigationBar inherits from UIView and thus is a view (go figure) and has a frame, etc.
What I do is point to a rect inside that view where the UIBarButtonItem should be.
For example:
[myMenu setTargetRect:CGRectMake(10, 10, 20, 25)
inView:self.navigationController.navigationBar];
This will make the UIMenuController 'point' at the leftBarButtonItem.
The same technique can be used for the toolbar.
In your case something like:
[menu setTargetRect:CGRectMake(0,0,40,40)
inView:self.navigationController.toolBar];
would point to the center of the toolBar and thus the center button in the toolBar
According to the API docs, the target rect defines the area that you want to display the menu relative to - iOS will either show the menu above or below the area defined by TargetRect.
When you make this menu visible, UIMenuController positions it
relative to a target rectangle on the screen; this rectangle usually
defines a selection. The menu appears above the target rectangle or,
if there is not enough space for it, below it. The menu’s pointer is
placed at the center of the top or bottom of the target rectangle, as
appropriate. Be sure to set the tracking rectangle before you make the
menu visible. You are also responsible for detecting, tracking, and
displaying selections.
and
This target rectangle (targetRect) is usually the bounding rectangle
of a selection. UIMenuController positions the editing menu above this
rectangle; if there is not enough space for the menu there, it
positions it below the rectangle. The menu’s pointer is placed at the
center of the top or bottom of the target rectangle as appropriate.
Note that if you make the width or height of the target rectangle
zero, UIMenuController treats the target area as a line or point for
positioning (for example, an insertion caret or a single point).
Once it is set, the target rectangle does not track the view; if the
view moves (such as would happen in a scroll view), you must update
the target rectangle accordingly.

How to get screen height in LWUIT out of height reserved for MenuBar and TitleBar?

I want to get the height and width of the screen which I could draw objects on. It's the space which is other than the the MenuBar (the footer containing commands) and other than the TitleBar.
TIP: I know that Display.getInstance().getDisplayHeight() returns the height of the screen but this is for the full sceen. So how could I, at least, know the height of the MenuBar and TitleBar to substract them from this height ?
I found the answer for those who are interested:
To get the height of TitleBar: Form.getTitleComponent().getPreferredH()
To get the height of AddressBar: Form.getSoftButton(0).getParent().getPreferredH()
So the answer of my question is:
Display.getInstance().getDisplayHeight()
- form.getTitleComponent().getPreferredH()
- (getSoftbuttonCount() < 1) ? 0 : form.getSoftButton(0).getParent().getPreferredH()

How to set a title for a Border with LWUIT 1.4 ? How to coloriate odd and pair Table rows?

I use LWUIT 1.4
1) In my Form there is a Container based on a BoxLayout ( Y axis ) , there are two Label's added to this container , and I want to create a titled Border to be placed into the container so it surrounds the two labels. I know to create a Border , but I do not know how to set a title to the Border with LWUIT 1.4 ! So how to set a title to a Border with LWUIT 1.4 ?
2) In my Form there is a Table based on the DefaultTableModel class whose getValueAt method is implemented with an enumeration of a recordstore , and I want that the rows of the Table are colored according to the index of the row : for example when the row index is odd then its background color should be white , and if the row index is pair then its background color should be gray. How to achieve that ?
3) Why do not the TableLayout.Constraint methods work ? I want to make two columns of a Table to have equal size , that is 50% of the Table total width for each column , but when I run the application then the first column is not equally sized with the second column when its data are not long enough ! So how to make the columns equally sized ?
Thank you very much indeed
1) There is a titled border in the trunk but I think it was added in 1.4.
You can draw something like this by overriding the Container paintBorder method (notice you will need sufficient component padding for the border to appear properly. This is the code from the SVN version, should work with very little changes (just change c to this):
Font f=c.getStyle().getFont();
int titleW=f.stringWidth(borderTitle);
int topPad=c.getStyle().getPadding(Component.TOP);
int topY=y+(topPad-thickness)/2;
if (c.isRTL()) {
g.fillRect(x+width-TITLE_MARGIN, topY, TITLE_MARGIN , thickness); //top (segment before the title)
g.fillRect(x, topY, width-(TITLE_MARGIN +titleW+TITLE_SPACE*2), thickness); //top (segment after the title)
g.drawString(borderTitle, x+width-(TITLE_MARGIN +titleW+TITLE_SPACE), y+(topPad-f.getHeight())/2);
} else {
g.fillRect(x, topY, TITLE_MARGIN , thickness); //top (segment before the title)
g.fillRect(x+TITLE_MARGIN +titleW+TITLE_SPACE*2, topY, width-(TITLE_MARGIN +titleW+TITLE_SPACE*2), thickness); //top (segment after the title)
g.drawString(borderTitle, x+TITLE_MARGIN+TITLE_SPACE, y+(topPad-f.getHeight())/2);
}
g.fillRect(x, y+height-thickness, width, thickness); //bottom
g.fillRect(x, topY, thickness, height); //left
g.fillRect(x+width-thickness, topY, thickness, height); //right
2) Derive table and override the method:
protected Component createCell(Object value, int row, int column, boolean editable)
call super.createCell() and set the UIID of the returned value to "OddRow","EvenRow" appropriately. Style in the resource editor or theme to anything you like.
3) I'm not aware of such an issue. If this happens on the current SVN you should file an issue in the projects issue tracker.
I downloaded the latest Resource Editor from your blog site , and I defined the background and border "selected" versions of the component TableCell , but the extra-rectangle is always shown in runtime when I click on a last column table cell ! I tried to call tableName.repaint() in the component focusGained() implemented method , because I registered it to a focuslistener , but the rectangle does not disappear.
I created a Dialog to show the selected row number and when the Dialog is shown when I click the third softbutton then the rectangle disappeared !!! Perhaps the focus is lost from the table cell !! And whenever I fire then I got the same row number as before ; so there is no row number error. So what code should I write , or what property should I edit in the Editor to get the same effect as of displaying the Dialog to make the rectangle disappear ?

Resources