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);
Related
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'm using the Java Tutorials example of how to use a JScrollPane (with row/column headers). The example is using a subclass of JLabel to display an image in the Viewport View. I used the sample code for displaying the row/column headers (Rule.java example code) and was perplexed at the bizarre results. I finally removed the call to getClipBounds() (apparently used to determine what region of the row/column header is visible to paint only that region) and painted the entire header, and the problem was resolved. That means that I'm now drawing the entire area (in both the row/column headers and the main Viewport). That strikes me as non-optimal.
Can anyone explain why the Java Tutorials example works properly (other than the source is not the same as that being executed in the example)?
Is it correct for me to be painting the entire pane even though it is only partially visible?
How can I determine what region of the overall object is visible in the Viewport (for row/column headers and the main Viewport) so I can just paint that region?
UPDATE:
I still don't know why the example works, but I've found that if I use JComponent.getVisibleRect() instead of Graphics.getClipBounds() things seem to work as expected. Not sure if this is the correct use of this method.
Look at this code below. I was just painting visible part.
#Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle view = new Rectangle();
if (getParent() instanceof JViewport) {
JViewport vp = (JViewport) getParent();
view = vp.getViewRect();
} else {
view = new Rectangle(0, 0, getWidth(), getHeight());
}
g2d.setColor(getBackground());
g2d.fillRect((int) view.getX(), (int) view.getY(), (int) view.getWidth(), (int) view.getHeight());
g2d.setColor(Color.YELLOW);
double x = view.getX();
double y = view.getY();
double w = view.getWidth();
double h = view.getHeight();
// draw Strings
for (StringShape ss : stringList) {
Rectangle sb = ss.getRectangle(g2d.getFontMetrics(ss.getFont()));
if (containShape(view, sb)) {
g2d.setFont(ss.getFont());
g2d.setColor(ss.getColor());
g2d.drawString(ss.getString(), (int) sb.getX(), (int) sb.getY());
}
}
}
JComponent.getVisibleRect() was the trick. Clearly I misunderstand the meaning/use of getClipBounds().
I am making a 2d engine using Java based on entities. The physics and sprites are done, but I still need to be able to draw text with the BaseText class. For experimental purposes I am using the following code in the Renderer class (that handles drawing all the sprites and such):
BufferGraphics.drawString(((BaseText) Entity).getText(), (int) -(Origin.getX() * PositionTransform), (int) -Origin.getY());
I would like to, however, be able to either move this code into the setText(final String Text) method of the BaseText entity, i.e. when it is called a new image is created containing the text specified (possibly in different fonts and sizes and such, I haven't decided).
My problem is this: I would like to be able to resize (scale) the text to my liking. It would also be nice to have the text converted to an image as I can get the dimensions of it and set the size of the text entity itself.
Basically, what I need follows something along these lines:
Take desired string and feed it into the setText method.
Take the string and draw it onto an image, sized so that the text will fit into it exactly.
Set this new image to the Image field in the entity so that the engine can draw it.
Is this even possible? There may be a way to do this with the FontMetrics class or whatever it may be called, but I'm not so sure as I have not used it before.
Edit : Let me clarify: I want to create a BufferedImage based on the size of some text set to a specific font and size, not size the text to fit an image.
Edit 2: Thanks to this fellow Andrew, whom so graciously provided code, I was able to add some code to the engine that, by all means, just plain should work. Again, however, not even with that drawRect in there, the image either remains either transparent or somehow is not getting drawn. Let me supply some breadcrumbs: -snip-
The stupid thing is that all the other sprites and images and such draw fine, so I am not sure how it could be the Renderer.
By the way, that was the paint() method.
Edit 3:
...
Uh...
...
Oh my.
I am...
...
Text can not explain how hard I belted myself in the face with my left palm.
BaseText.java
#Override
public BufferedImage getImage() {return null;}
Renderer.java
BufferedImage Image = Entity.getImage();
I am
a huge idiot.
Thank you, Andrew, for that code. It worked fine.
Edit 4: By the way, here's the final code that I used:
public void setText(final String Text)
{
Graphics2D Draw = (Graphics2D) Game.View.getBuffer().getDrawGraphics();
FontMetrics Metrics = Draw.getFontMetrics();
Rectangle2D Bounds = Metrics.getStringBounds(Text, Draw);
BufferedImage NewImage = new BufferedImage((int) Bounds.getWidth(), (int) (Bounds.getHeight() + Metrics.getDescent()), BufferedImage.TYPE_INT_RGB);
Draw = (Graphics2D) NewImage.getGraphics();
Draw.setColor(new Color(0xAAFF0000));
Draw.drawRect(0, 0, NewImage.getWidth(), NewImage.getHeight());
Draw.drawString(Text, 0, (int) Bounds.getHeight());
this.Image = NewImage;
this.Text = Text;
this.setSize(new Vector(NewImage.getWidth(), NewImage.getHeight()));
}
Use FontMetrics, GlyphView or the preferred size a JLabel (handy for getting the size needed to display formatted text.
Adjust the sizes of the font in step 1 until it fits. Call BufferedImage.createGraphics() to get a Graphics2D object. Paint the String to that.
I do not understand point 3, so won't comment.
Here is how it would work with either FontMetrics or a JLabel.
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
class TextSize {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Technique 1 - FontMetrics
String s = "The quick brown fox jumps over the lazy dog!";
BufferedImage bi = new BufferedImage(
1,
1,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
FontMetrics fm = g.getFontMetrics();
Rectangle2D b = fm.getStringBounds(s,g);
System.out.println(b);
bi = new BufferedImage(
(int)b.getWidth(),
(int)(b.getHeight() + fm.getDescent()),
BufferedImage.TYPE_INT_RGB);
g = bi.getGraphics();
g.drawString(s,0,(int)b.getHeight());
JOptionPane.showMessageDialog(
null,
new JLabel(new ImageIcon(bi)));
// Technique 3 - JLabel
JLabel l = new JLabel(s);
l.setSize(l.getPreferredSize());
bi = new BufferedImage(
l.getWidth(),
l.getHeight(),
BufferedImage.TYPE_INT_RGB);
g = bi.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,400,100);
l.paint(g);
JOptionPane.showMessageDialog(
null,
new JLabel(new ImageIcon(bi)));
}
});
}
}
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);
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.