View Single Post
  #2 (permalink)  
Old 04-04-2009, 07:55 PM
hardwired's Avatar
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Suggestions:
1 — FlowLayout is not a good choice for your Draw component. Inspect the bounds (r) of Draw after it is rendered (code below). Better to use the center section of a BorderLayout.
Code:
        public void paintComponent( Graphics g)
        {
            super.paintComponent( g);
            Graphics2D g2 = (Graphics2D)g;
            Rectangle r = getBounds();
            System.out.printf("width = %d  height = %d " +
                              "bounds = [%d, %d, %d, %d]%n",
                               getWidth(), getHeight(),
                               r.x, r.y, r.width, r.height);
            g2.setPaint(Color.magenta);
            g2.draw(r);
            g2.setPaint(Color.black);
            x1 = 0; x2 = 0; y1 = 0; y2 = 0;

            while( x1 < 1000)
            {
                x2 = x1 + 1;
                y1 = a * x1 * x1 + b * x1;
                y2 = a * x2 * x2 + b * x2;
                if(x1 == 200) {
                    System.out.printf("x1 = %d  y1 = %d%n", x1, y1);
                    g2.setPaint(Color.red);
                    g2.fill(new Ellipse2D.Double(x1-1.5, y1-1.5, 4, 4));
                    g2.setPaint(Color.green.darker());
                    g2.fill(new Ellipse2D.Double(200 + x1, 200 - y1, 4, 4));
                    g2.setPaint(Color.blue);
                    g2.drawLine(200, 200, x1, y1);
                    g2.setPaint(Color.black);
                }
                g.drawLine( 200 + x1, 200 - y1,200 + x2, 200 - y2 );
                g.drawLine( 200 - x1, 200 - y1,200 - x2, 200 - y2 );
                x1 = x2;
            }
        }
2 — Mount the sliders in a panel and add it to the south/last section of the BorderLayout.
3 — Code above gives some ideas about how to explore/investigate what is going on in your graphic component.
4 — For dynamic graphics, viz, fluid resizing behavior, consider using the width and height of the component for drawing, eg (pseudo code),
Code:
        protected void paintComponent( Graphics g)
        {
            super.paintComponent( g);
            Graphics2D g2 = (Graphics2D)g;
            int w = getWidth();
            int h = getHeight();
            // center of this component:
            int cx = w/2;
            int cy = h/2;
            g2.fill(new Ellipse2D.Double(cx-1.5, cy-1.5, 4, 4));
Reply With Quote