I have a LabelField nested within a TableLayoutManager row. I want the row to be a specific height (the same height as its bitmap background). In order to achieve this, I changed the layout() method of the nested LabelField:
LabelField lblHours = new LabelField(hours,
Color.BLACK, Manager.FIELD_VCENTER) {
protected void layout(int width, int height) {
super.layout(width, 40 //height of the bitmap);
setExtent(width, 40 //height of the bitmap);
}
};
This successfully increased the TableLayoutManager row size. However, once I do this, the LabelField is no longer centered vertically within the row. Any suggestions on how to do that?
Thanks!
The above answer works just make sure that you put in two statements for the above.. as illustrated in the below example.
LabelField importanceLabel = new LabelField("Importance: ",LabelField.FIELD_VCENTER | LabelField.FIELD_RIGHT) {
public void paint(Graphics g) {
g.setColor(0x145085);
super.paint(g);
}
protected void layout(int width, int height) {
super.layout(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
}
};
This is going to be the same response.. and thank you guys.. for the discussion. It helped me.
Try changing setExtent(width, 40) to setExtent(this.getWidth(), 40). This could possibly not work if getWidth() is trying to take up all of the available width (which is what your setExtent() call is doing), and since the text will be drawn left-aligned, it looks like your field isn't centered but in reality it's just taking up the entire cell of your table. Alternatively, if this doesn't work you may be able to do setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 40);
Related
I am making a live wallpaper with a gif image that is put in the raw directory, how can I put the gif in the center of the screen, no matter what device I am using?? I understand why my code right now puts it in the left, because display.getWidth-display.getWidth =0, and same with height, hence (0,0) is in the top left. But what is center?! I cannot seem to figure it out for the life of me. This class is extending WallpaperService.
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
mScaleX = width / (1.5f*mNyan.width());
mScaleY = height / (1.5f*mNyan.height());
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mWidth = (display.getWidth()-display.getWidth());
mHeight = (display.getHeight()-display.getHeight());
nyan();
}
and this is the end where I draw the canvas...
void nyanNyan(Canvas canvas) {
canvas.save();
canvas.scale(mScaleX, mScaleY);
mNyan.setTime(mWhen);
mNyan.draw(canvas, mWidth,mHeight);
canvas.restore();
}
Can anyone help!?! I'm stuck, and I can't move past this. I also tried, without any luck:
void nyanNyan(Canvas canvas) {
canvas.save();
canvas.scale(mScaleX, mScaleY);
mNyan.setTime(mWhen);
mNyan.draw(canvas, mCenterX - mNyan.width() , mCenterY - mNyan.height());
}
As I do not fully understand your code (e.g. where vars like mCenterX are coming from or how they are defined) I can not gurantee the code to run properly without further adjustments.
Assuming that you have access to nyan's width and height (I'll call it nyan.getWidth()) and the canvas' dimensions, then your last mNyan.draw() call should probably look like this:
mNyan.draw(
canvas,
( canvas.getWidth() - nyan.getWidth() ) / 2,
( canvas.getHeight() - nyan.getHeight() ) / 2
);
I want to draw graphics(such as line, polygon and more) on a image in codenameone framework.
I write some codes like below, but i got error and it's not working.
How can i do such a thing anyone can help me.
Thanks in advance
currentForm = new Form();
currentForm.setLayout(new LayeredLayout());
currentForm.applyRTL(true);
final Container mapContainer = new Container(new FlowLayout(Component.RIGHT));
currentForm.addComponent(mapContainer);
Image mapImg = res.getImage("t_map.png");
mapContainer.getUnselectedStyle().setBgImage(mapImg);
mapContainer.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
paints(mapImg.getGraphics());
public void paints(Graphics g) {
g.setColor(0xeecccc);
g.fillRadialGradient(0xffffff, 0x334561, 0,0,90, 200);
g.setColor(0xeecccc);
g.fillRect(10, 10, 80, 130);
g.setColor(0xcceecc);
g.drawString("Write on Graphic",100, 150);
}
You need to create a mutable image which isn't the default, a mutable image is created via Image.createImage(int width,int height) or Image.createImage(int width,int height, int argbBackgroundColor).
Those images will allow getGraphics() to work properly and paint, you can then draw the image you got from the resources onto that mutable image.
I created a CustomBar which extends ProgressBar, it has various methods to customize the appearance. One of this Functions adds a Icon to the progressing part. Since it's bigger that the ProgressBar it gets cropped, which I do not want.
So I tried changing the size of the ProgressBar when the Icon is added.
like this:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = bitmap.getHeight();
setLayoutParams(params);
requestLayout();
This doesn't solve the problem, so I tried changing the size of the Drawable.
It's a LayerDrawable, I tried, setBounds, setLayerInsets, changing the first Layer to a InsetDrawable with padding to get the size I wanted. All to no avail.
So what am I missing? How to properly change the size of a progressBar at runtime?
Found the answer myself, you have to overwrite the bounds, but also for the Canvas.
#Override
protected synchronized void onDraw(Canvas canvas)
{
if(bitmap != null && bitmap.getHeight() > getHeight())
{
canvas.clipRect(0, 0, getWidth(), bitmap.getHeight(), Region.Op.REPLACE);
bounds = getProgressDrawable().getBounds();
bounds.bottom = bitmap.getHeight();
}
doSomething();
}
Important is the Region.Op.REPLACE flag, normally clipping just gets intersected with the Bounds of the Canvas.
i am developing an application in which i need to set two button in HorizontalFieldManage. where one Bitmap should stay left and another LabelField should stay center at horizontally. here i have tried many time but cant able to set first Bitmap at Left so can you please help me out from this..
Here is my Code ::
VerticalFieldManager VFM = new VerticalFieldManager(USE_ALL_WIDTH){
public void paint(Graphics g) {
g.setBackgroundColor(Color.WHITE);
g.clear();
super.paint(g);
}
};
HorizontalFieldManager HFM = new HorizontalFieldManager(FIELD_HCENTER){
public void paint(Graphics g) {
g.setBackgroundColor(Color.WHITE);
g.clear();
super.paint(g);
}
};
Bitmap logom1;
logom1 = Bitmap.getBitmapResource("logo48X48.png");
BitmapField imgField = new BitmapField(logom1,Field.FIELD_LEFT);
LabelField RegistrationLbl = new LabelField("Registration",FIELD_HCENTER | FIELD_BOTTOM);
FontFamily fontFamily[] = FontFamily.getFontFamilies();
Font font = fontFamily[1].getFont(FontFamily.CBTF_FONT, 20);
font = fontFamily[1].getFont(Font.BOLD, 25);
RegistrationLbl.setFont(font);
HFM.add(imgField);
HFM.add(RegistrationLbl);
VFM.add(HFM);
add(VFM);
Signare's general solution could work, but the left margin wasn't quite right. The only calls you need to add are these (split into two lines for clarity), before adding your label to the HFM object:
int labelX = (Display.getWidth() - RegistrationLbl.getPreferredWidth()) / 2;
RegistrationLbl.setMargin(0, 0, 0, labelX - imgField.getPreferredWidth());
This assumes the class (Manager) that this is in takes up the full screen width (Display.getWidth()).
Read this for a good description of what margin is.
Also, note that setMargin() was undocumented in the APIs before 6.0, but I believe it was actually available (but, undocumented) back to OS 4.5 or so.
Edit: by the way, you are assigning your font object, and then immediately assigning it to something else. That doesn't look right either, although it doesn't affect the problem centering the label.
Another Edit: as illustrated by Rupak's comment, this code only works if you have a fixed orientation display. If the label is supposed to center itself again on device orientation change, then you need more than this. Please just add more clarification to the question, if that's needed, and someone will help!
try this (This is not a correct way)-
HorizontalFieldManager VFM = new HorizontalFieldManager(){
public void paint(Graphics g) {
g.setBackgroundColor(Color.WHITE);
g.clear();
super.paint(g);
}
};
HorizontalFieldManager LogoHFM = new HorizontalFieldManager(FIELD_LEFT);
Bitmap logom1;
logom1 = Bitmap.getBitmapResource("logo48X48.png");
BitmapField imgField = new BitmapField(logom1);
LogoHFM.add(imgField);
LabelField RegistrationLbl = new LabelField("Registration",FIELD_HCENTER);
FontFamily fontFamily[] = FontFamily.getFontFamilies();
Font font = fontFamily[1].getFont(FontFamily.CBTF_FONT, 20);
font = fontFamily[1].getFont(Font.BOLD, 25);
RegistrationLbl.setFont(font);
RegistrationLbl.setMargin(0,0,0,(Display.getWidth()-logom1.getWidth())/4);
VFM.add(LogoHFM);
VFM.add(RegistrationLbl);
add(VFM);
I've created a custom swing component. I can see it (the grid from the paint method is drawn), but the buttons that are added (verified by println) aren't shown. What am I doing wrong?
Background information: I'm trying to build a tree of visible objects like the Flash/AS3 display list.
public class MapPanel extends JComponent { // or extends JPanel, same effect
private static final long serialVersionUID = 4844990579260312742L;
public MapPanel(ShapeMap map) {
setBackground(Color.LIGHT_GRAY);
setPreferredSize(new Dimension(1000,1000));
setLayout(null);
for (Layer l : map.getLayers()) {
// LayerView layerView = new LayerView(l);
// add(layerView);
System.out.println(l);
JButton test = new JButton(l.getName());
add(test);
validate();
}
}
#Override
protected void paintComponent(Graphics g) {
// necessary?
super.paintComponent(g);
// background
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
// grid
g.setColor(Color.GRAY);
for (double x = 0; x < getWidth(); x += 10) {
g.drawLine((int)x, 0, (int)x, getHeight());
}
for (double y = 0; y < getHeight(); y += 10) {
g.drawLine(0, (int)y, getWidth(), (int)y);
}
}
}
Setting null as the layout manager and then adding buttons will not have any effect. A layout manager is responsible for computing the bounds of the children components, and setting layout manager to null effectively leaves all your buttons with bounds = (0,0,0,0).
Try calling test.setBounds(10, 10, 50, 20) as a quick test to see if the buttons appear. If they do, they will be shown at exactly the same spot. From there you can either install a custom layout manager that gives each button the required bounds, or use one of the core / third party layout managers.
It would be easier for us to diagnose your problem if you gave us a SSCCE. As it stands, we may not have enough information to fix your problem.
I can see it (the grid from the paint
method is drawn),
I don't know what that means, there is no paint() method in the posted code. (But I suppose it is easy enough to assume that you meant paintComponent(g))
However, it looks like the problem is that you are uisng a "null layout". The children will not paint unless you manually set the size and location of the children.
You should probably read a quick tutorial on LayoutManagers. It may make things easier for you when drawing components.