Results 1 to 3 of 3
  1. #1
    weddy69 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default swing mixed with awt graphics

    HI all,

    First i am limited in placing code Ill do my best to explain what is missing. Thanks

    I am mixing the graphic interface similar to Goole maps, currently I am working on a pan zoom feature that zooms in on multiple map images. I do not know what the graphics are at this time I have been using gif and jpg. I have placed my graphics on the JFrame layer
    and used LayeredPanes to paste the JSlider.Vertical bar. This is where i get stumped... with out the main method except for testing, how would i put the JSlider on a floating pane where it is not stuck on the frame layer I messed my LayeredPane up some how.

    this is code I have been working with to get functionality.
    Java Code:
     public class MapScale extends JPanel {
        BufferedImage image;
        double scale = 1.0;
    
        public MapScale(BufferedImage image) {
            this.image = image;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            double x = (getWidth() - scale*image.getWidth())/2;
            double y = (getHeight() - scale*image.getHeight())/2;
            AffineTransform at = AffineTransform.getTranslateInstance(x,y);
            at.scale(scale, scale);
    
            g2.drawRenderedImage(image, at);
        }
    
        public Dimension getPreferredSize() {
            int w = (int)(scale*image.getWidth());
            int h = (int)(scale*image.getHeight());
            return new Dimension(w, h);
        }
    
        private JSlider getSlider() {
    
    
    
            int min = 1, max = 41, inc = 5;
            final JSlider slider = new JSlider(JSlider.VERTICAL,min, max, 20);
            slider.setMajorTickSpacing(5);
            slider.setMinorTickSpacing(1);
            slider.setPaintTicks(true);
            slider.setSnapToTicks(true);
            slider.setLabelTable(getLabelTable(min, max, inc));
            slider.setPaintLabels(true);
            slider.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    int value = slider.getValue();
                    System.out.println(value + "this is the value of the value slider ");
                    // I want a more mag in the zoom
                    scale = (value+4)/5.0;
                    revalidate();
                    repaint();
                }
            });
            
    
            return slider;
        }
    
        private Hashtable getLabelTable(int min, int max, int inc) {
            Hashtable<Integer,JLabel> table = new Hashtable<Integer,JLabel>();
            for(int j = min; j <= max; j += inc) {
                String s = String.format("%.2f", (j+4)/20.0);
                System.out.println(s +"  this is the  format");
                table.put(Integer.valueOf(j), new JLabel(s));
            }
            return table;
        }
    
        public static void main(String[] args) throws IOException {
            String path = "images/WBD_world-map.jpg";
            BufferedImage image = ImageIO.read(new File(path));
            MapScale test = new MapScale(image);
            test.add(test.getSlider());// this adds to the map
    
            JLayeredPane lPane = new JLayeredPane();
            lPane.setPreferredSize(new Dimension(100,300));
            lPane.setLocation(10, 10);
            lPane.setBorder(BorderFactory.createEmptyBorder());
            Container cp = new Container();
            cp.add(test.getSlider());
            lPane.add(cp);
            
            lPane.setVisible(true);
            
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
            f.getContentPane().add(new JScrollPane(test));
    
    //        f.getContentPane().add(test.getSlider());
    //        f.getContentPane().add(test.getSlider(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
        }
    Thanks in advance for clues, tips, and direction...

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    without the main method
    how would i put the JSlider on a floating pane


    If I understand the situation, your application will be a single Component that displays a map in a JScrollPane. If so, you may be able to solve the problem by having the JScrollPane be the single Component.

    where it is not stuck on the frame layer

    This suggests that the Component will have two parts, the JSlider and the JScrollPane. You can combine these into a single JPanel. Set its LayoutManager to a BorderLayout and insert the map in BorderLayout.CENTER and the slider in BorderLayout.LEADING, or the like.

    Here's an (untested) sketch:
    Java Code:
    Component createMapViewer () {
        MapScale test = new MapScale(image);
        JScrollPane scroll = new JScrollPane(test);
        JSlider slider  = ...;
        JPanel viewer = new JPanel();
        viewer.setLayout(new BorderLayout());
        viewer.add(slider, BorderLayout.LEADING);
        viewer.add(scroll, BorderLayout.CENTER;
        return viewer;
     }
    The value to insert as the Component is the value returned by createMapViewer.

    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default

    > how would i put the JSlider on a floating pane
    You could try adding it to a JToolBar or to a non-modal JDialog.

    db

Similar Threads

  1. Help me with graphics
    By 7oclock in forum New To Java
    Replies: 12
    Last Post: 04-04-2009, 11:20 PM
  2. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 11:48 AM
  3. Mixed language project?
    By toadaly in forum NetBeans
    Replies: 5
    Last Post: 03-11-2009, 06:29 AM
  4. SWT Graphics Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:28 PM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •