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

    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) {
            Graphics2D g2 = (Graphics2D)g;
            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.setLabelTable(getLabelTable(min, max, inc));
            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;
            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 = 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);
            Container cp = new Container();
            JFrame f = new JFrame();
            f.getContentPane().add(new JScrollPane(test));
    //        f.getContentPane().add(test.getSlider());
    //        f.getContentPane().add(test.getSlider(), "Last");
    Thanks in advance for clues, tips, and direction...

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


    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 Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


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


Similar Threads

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