• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

awt layout help

I have a custom component which doesn't seem to be doing what I want.

The code in the root container is:

      setLayout(new GridLayout(1,2));
      
      ColourButtonIcon cbi1 = new ColourButtonIcon(Color.red);
      cbi1.setVisible(true);
      add(cbi1);

      ColourButtonIcon cbi2 = new ColourButtonIcon(Color.green);
      cbi2.setVisible(true);
      add(cbi2);

      repaint();

and the ColourButtonIcon paint method is as follows

      public void paint(Graphics g) {

            Graphics2D g2 = (Graphics2D)g;

            Stroke oldStroke = g2.getStroke();
            g2.setStroke(new BasicStroke(strokeWidth));
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            
            Color c1 = color;
            Color c2 = Color.gray.darker();
            
            g2.setPaint(c1);
            g2.fillRoundRect(
                        2,
                        2,
                        radius - 4,
                        radius - 4,
                        radius - 4,
                        radius - 4);
            g2.setPaint(c2);
            g2.drawRoundRect(
                        (strokeWidth+1)/2,
                        (strokeWidth+1)/2,
                        radius-strokeWidth,
                        radius-strokeWidth,
                        radius,
                        radius
                        );
            g2.setStroke(oldStroke);
      }

However, both ColourButtonIcons are being drawn in the top left corner of my container, rather than correctly in their GridLayout. What am I doing wrong?
0
eeBlueShadow
Asked:
eeBlueShadow
  • 5
  • 4
1 Solution
 
eeBlueShadowAuthor Commented:
Or alternatively, some knowledge of how components draw themselves at different locations (I guess that's something to do with the Graphics object)
0
 
PhilAllenCommented:
http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

Is the place to be

Take along a big big cup of coffee

Phil
0
 
eeBlueShadowAuthor Commented:
That's a pretty good page, but it still doesn't help explain why my two components which I'm adding separately are being drawn over one another.

My only guess would be something to do with calling my fillRoundRect with x=2 and y=2 - that's where they're being drawn on screen rather than relative to anything else - but I don't see any way to avoid that.

That said, I'm probably on completely the wrong track
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
can you post the complete code for that class
0
 
eeBlueShadowAuthor Commented:
this one?


import java.awt.*;

public class ColourButtonIcon extends Component {
      private int strokeWidth = 4;
      private int radius;
      private Color color;
      
      public ColourButtonIcon(Color c, int radius) {
            this.color = c;
            this.radius = radius;
            setSize(radius*2,radius*2);
      }

      public ColourButtonIcon(int radius) {
            this(new Color(255,0,0), radius);
      }
      
      public ColourButtonIcon(Color c) {
            this(c,20);
      }

      public ColourButtonIcon() {
            this(new Color(255,0,0) ,20);
      }

      public void paint(Graphics g) {

            Graphics2D g2 = (Graphics2D)g;

            Stroke oldStroke = g2.getStroke();
            g2.setStroke(new BasicStroke(strokeWidth));
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            
            Color c1 = color;
            Color c2 = Color.gray.darker();
            
            g2.setPaint(c1);
            g2.fillRoundRect(
                        2,
                        2,
                        radius - 4,
                        radius - 4,
                        radius - 4,
                        radius - 4);
            g2.setPaint(c2);
            g2.drawRoundRect(
                        (strokeWidth+1)/2,
                        (strokeWidth+1)/2,
                        radius-strokeWidth,
                        radius-strokeWidth,
                        radius,
                        radius
                        );
            g2.setStroke(oldStroke);
      }
}
0
 
objectsCommented:
no sorry, the one you are adding your icons to.
0
 
eeBlueShadowAuthor Commented:
ok. HScene (of which scene is an instrance) is a class which extends Container and is drawn by the application

--------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.tv.xlet.*;
import org.havi.ui.*;
import org.dvb.ui.*;
import org.davic.media.*;
import javax.tv.service.selection.*;

public class TVBank extends Container implements Xlet {

      private static XletContext context;

      public static Color BUTTON_BORDER_COLOUR = new Color(100, 100, 215);
      public static Color BUTTON_BORDER_HILITE = new Color(255, 0, 0);
      public static Color BUTTON_BGROUND_COLOUR = new Color(255, 255, 255);
      public static Color BUTTON_BGROUND_HILITE = new Color(255, 255, 255);
      public static Color BUTTON_TEXT_COLOUR = new Color(0, 0, 0);
      public static Color BUTTON_TEXT_HILITE = new Color(0, 0, 0);

      // Main scene
      private HScene scene;

      // The initializations are always done in initXlet, not in
      // the construcor.
      public TVBank() {
            super();
      }

      public static XletContext getContext() {
            return context;
      }

      public void initXlet(XletContext ctx) throws XletStateChangeException {
            setName("TVBank");
            this.context = ctx;
            
            // Creating the scene to paint on.
            HSceneFactory factory = HSceneFactory.getInstance();
            HSceneTemplate hst = new HSceneTemplate();
            hst.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new org.havi.ui.HScreenDimension(1, 1), HSceneTemplate.REQUIRED);
            hst.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new org.havi.ui.HScreenPoint(0, 0), HSceneTemplate.REQUIRED);
                scene = factory.getBestScene(hst);
            scene.add(this);
            scene.addKeyListener((KeyListener)this);

            this.setSize(scene.getSize());
      }

      public void startXlet() throws XletStateChangeException {
            validate();

            scene.setVisible(true);
            scene.requestFocus();
            scene.show();

            setLayout(new GridLayout(1,0));
            
            ColourButtonIcon cbi1 = new ColourButtonIcon(Color.red, 30);
            cbi1.setVisible(true);
            add(cbi1);

            ColourButtonIcon cbi2 = new ColourButtonIcon(Color.green, 40);
            cbi2.setVisible(true);
            add(cbi2);
            
            ColourButtonIcon cbi3 = new ColourButtonIcon(Color.yellow, 50);
            cbi3.setVisible(true);
            add(cbi3);

            ColourButtonIcon cbi4 = new ColourButtonIcon(Color.blue, 60);
            cbi4.setVisible(true);
            add(cbi4);

            repaint();
      }

      public void paint(Graphics g) {
            super.paint(g);
      }

      public void pauseXlet() {
            scene.setVisible(false);
      }

      public void destroyXlet(boolean b) {
            if (scene != null) {
                  removeKeyListener(this);
                  scene.remove(this);
                  scene.setVisible(false);
                  HSceneFactory.getInstance().dispose(scene);
                  scene = null;
            }
            context.notifyDestroyed();                        
      }
}


0
 
objectsCommented:
try calling validate() after adding the icons.
0
 
eeBlueShadowAuthor Commented:
I wonder what happens when you get to 5,000,000 points in an area, will they have to invent a new certificate? 'Legend' or something
0
 
objectsCommented:
perhaps "Java Tragic" :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now