Kirti
Kirti

Reputation: 701

How to draw horizontal line in blackberry

I tried following code for draw a single line horizontal line but it is not working. i am not getting what is the problem.

HorizontalFieldManager horline = new HorizontalFieldManager()
{
    protected void paint(Graphics graphics) 
    {
        super.paint(graphics);
        graphics.setColor(Color.RED);
        graphics.drawLine(5, 21,10, 20);                      
    }
};

Upvotes: 1

Views: 185

Answers (2)

Dinesh Chandra
Dinesh Chandra

Reputation: 329

Use this

HorizontalFieldManager horline = new HorizontalFieldManager()
{
   protected void paint(Graphics graphics) 
   {
      super.paint(graphics);
      graphics.setColor(Color.RED);
   }
};
horline.add(new SeparatorField(SeparatorField.LINE_HORIZONTAL|SeparatorField.VISUAL_STATE_FOCUS));

Upvotes: 1

Nate
Nate

Reputation: 31045

There's at least a couple problems here:

Extent

The extent of a field (or manager) is basically the size of that field on screen. This size is normally set by a Field object in its layout() method, or by a Manager object in its sublayout() method. The problem is that your HorizontalFieldManager does not override these methods to set the size (by calling setExtent()), and it doesn't look like you add any fields to the manager. So, I believe your horline manager object simply has a size of {0, 0}. Drawing outside its extent doesn't do anything.

Manager vs Field

Manager classes are containers for fields. In this case, all you have is a line. I would definitely not use a Manager (including HorizontalFieldManager) for this, since you're not putting any fields into it, and there is overhead to adding Manager objects. Use a lighter-weight Field, or maybe even modify the paint() or paintBackground() method on whatever class contains this code ... you don't show us that, so I can't say for sure.

If you want to represent the line with a Field, then this will work:

  Field line = new Field() {
     protected void layout(int width, int height) {
        setExtent(20, 21);            
     }
     protected void paint(Graphics g) {
        int oldColor = g.getColor();
        g.setColor(Color.RED);
        g.drawLine(5, 21,10, 20);          
        g.setColor(oldColor);
     }         
  };
  add(line);

Note that I am setting the extent to width=20, height=21, because those are the maximum coordinates you pass to drawLine(). Also, because your y values are 20 and 21, this isn't actually a truly horizontal line.

add()

This might have simply been left out of the code you show to keep the question short, but whether you use a Manager or a Field, you do need to remember to call add() for your field/manager object. Objects created, but not added to a screen, will never show. In your case, the setExtent() problem would also have caused this problem.


Update:

As Dinesh shows in his answer, you could also solve the problem by using SeparatorField. However, I believe that only gives you purely horizontal/vertical lines. Because of the coordinates in your code, I wasn't sure if you needed the ability to draw lines of any orientation ... if you do, then overriding paint() is necessary. If not, use SeparatorField ... but hopefully, you learned something from this answer, too :).

Upvotes: 2

Related Questions