?
Solved

awt layout help

Posted on 2005-03-15
10
Medium Priority
?
246 Views
Last Modified: 2010-03-31
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
Comment
Question by:eeBlueShadow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 13547846
Or alternatively, some knowledge of how components draw themselves at different locations (I guess that's something to do with the Graphics object)
0
 

Expert Comment

by:PhilAllen
ID: 13547990
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
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 13549486
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 92

Expert Comment

by:objects
ID: 13549813
can you post the complete code for that class
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 13549930
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
 
LVL 92

Expert Comment

by:objects
ID: 13549954
no sorry, the one you are adding your icons to.
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 13550021
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13550462
try calling validate() after adding the icons.
0
 
LVL 10

Author Comment

by:eeBlueShadow
ID: 13550659
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
 
LVL 92

Expert Comment

by:objects
ID: 13550715
perhaps "Java Tragic" :)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question