Results 1 to 12 of 12
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default To make slider resize rectangle and to move pictures

    I have made a slider on DesktopPane and it should influence my drawings on JPanel, everything set up on JApplet. I hope i didn't explained very confusing. :rolleyes:

    My drawing consists of rectangle which have images on it's left and right side. When I move slider, rectangle should be shrinking and images moving with it to continue being right on it's left and right edge.

    Do you have any idea, link or hint how to make this?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Start with a design. Break the project up into small steps/units. Solve each step with a smalll simple program. Merge the small units into the large program as you get them working.

  3. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I think I have already broken it making JPanel and DesktopPane. DesktopPane has only sliders on it, while JPanel has the images and rectangle which are affected by slider. I'm trying to find the way to make influence on rectangle and images while dragging slider. :o

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Have you looked at the Tutorial How to use Sliders
    Last edited by Norm; 05-20-2010 at 05:06 PM.

  5. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Yes, I saw that, but I don't know where to write this code for stateChanged method since I have separated applet with slider definition and separate JPanel with this picture and rectangle, so I don't know how to make my variables seen on both places, because I need to change state on slider which will influence pictures and rectangle. So complicated...

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    To have a variable in scope (be seen) in two methods in a class, move the variable up to the class level.

    If you post your code for further questions be sure to put it in code tags to keep its formatting. [ code ] & [/ code ] without the spaces

  7. #7
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    There is no problem with variables in methods, but with variables on JApplet and JPanel... I have been trying to fix things:

    I made one JPanel which consists of rectangle and pictures on it's edges and I made a sliders on main panel (actually on JApplet). How to make rectangle and images affected with slider when my code in JPanel doesn't recognize variables for slider (which are in JApplet)? Here is the code:

    Java Code:
    package org.me.pack;
    
    import javax.swing.JApplet;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    import javax.swing.Timer;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedInputStream;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class NewJApplet extends JApplet {
     private javax.swing.JLabel brrutera;
        private javax.swing.JPanel glavnipanel;
        private javax.swing.JSlider jSlider1;
        private javax.swing.JDesktopPane panel1;
        private javax.swing.JLabel plus1;
        private javax.swing.JLabel plus2;
        private javax.swing.JLabel protok;
        private javax.swing.JSlider slider1;
        private javax.swing.JSlider slider2;
        private javax.swing.JSlider slider3;
        private javax.swing.JSlider slider4;
        private javax.swing.JLabel tl;
        private javax.swing.JTextField tp;
        private javax.swing.JTextField tq;
        private javax.swing.JTextField tt;
        private javax.swing.JLabel udaljenost;
        private javax.swing.JLabel velicina;
    
        static final int FPS_MIN = 0;
        static final int FPS_MAX = 20;
        static final int FPS_INIT = 10;
    
        public void init() {
    
            panel1 = new javax.swing.JDesktopPane();
            jSlider1 = new javax.swing.JSlider();
            slider1 = new javax.swing.JSlider();
            velicina = new javax.swing.JLabel();
            udaljenost = new javax.swing.JLabel();
            slider2 = new javax.swing.JSlider();
            brrutera = new javax.swing.JLabel();
            slider3 = new javax.swing.JSlider();
            protok = new javax.swing.JLabel();
            slider4 = new javax.swing.JSlider();
            tl = new javax.swing.JLabel();
            plus1 = new javax.swing.JLabel();
            plus2 = new javax.swing.JLabel();
            tt = new javax.swing.JTextField();
            tp = new javax.swing.JTextField();
            tq = new javax.swing.JTextField();
            glavnipanel = new org.me.pack.MajaPanel();
    
            panel1.setBackground(new java.awt.Color(255, 255, 255));
            panel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
            jSlider1.setBounds(-200, -70, 200, 70);
            panel1.add(jSlider1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            slider1.setBackground(new java.awt.Color(255, 255, 255));
            slider1.setPaintLabels(true);
            slider1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            slider1.setBounds(190, 10, 200, 25);
            slider1.setValue(50);
                slider1.addChangeListener(new MyChangeAction());
    
            
            panel1.add(slider1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            velicina.setText("Odaberite veličinu fajla (u MB):");
            velicina.setBounds(10, 20, 180, 14);
            panel1.add(velicina, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            udaljenost.setText("Udaljenost (u km)");
            udaljenost.setBounds(10, 50, 170, 14);
            panel1.add(udaljenost, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            slider2.setBackground(new java.awt.Color(255, 255, 255));
            slider2.setBounds(190, 40, 200, 25);
            panel1.add(slider2, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            brrutera.setText("Broj rutera:");
            brrutera.setBounds(10, 80, 170, 14);
            panel1.add(brrutera, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            slider3.setBackground(new java.awt.Color(255, 255, 255));
            slider3.setBounds(190, 70, 200, 25);
            panel1.add(slider3, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            protok.setText("Protok (u MB/s):");
            protok.setBounds(10, 110, 170, 14);
            panel1.add(protok, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            slider4.setBackground(new java.awt.Color(255, 255, 255));
            slider4.setBounds(190, 100, 200, 25);
            panel1.add(slider4, javax.swing.JLayeredPane.DEFAULT_LAYER);
    
            tl.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            tl.setText("Tl =");
    
            plus1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            plus1.setText("+");
    
            plus2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            plus2.setText("+");
    
            tt.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            tt.setText("Tt");
            tt.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                }
            });
    
            tp.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            tp.setText("Tp");
            tp.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                }
            });
    
            tq.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            tq.setText("Tq");
            tq.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                }
            });
    
            glavnipanel.setBackground(new java.awt.Color(255, 255, 255));
            glavnipanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    
            javax.swing.GroupLayout glavnipanelLayout = new javax.swing.GroupLayout(glavnipanel);
            glavnipanel.setLayout(glavnipanelLayout);
            glavnipanelLayout.setHorizontalGroup(
                glavnipanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 563, Short.MAX_VALUE)
            );
            glavnipanelLayout.setVerticalGroup(
                glavnipanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 212, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(glavnipanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(30, 30, 30)
                            .addComponent(tl)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(plus1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(plus2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(glavnipanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tl)
                            .addComponent(plus1)
                            .addComponent(tt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(plus2)
                            .addComponent(tq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap())
            );
    
            // TODO start asynchronous download of heavy resources
        }
    public class MyChangeAction implements ChangeListener{
        public void stateChanged(ChangeEvent ce){
          int value = slider1.getValue();
    
    }}}
    Java Code:
    package org.me.pack;
    import java.awt.Color;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.net.URL;
    import java.awt.Image;
    import java.awt.Graphics;
        public class MajaPanel extends JPanel
    {
      public Image backgroundImage;
    
    
      public MajaPanel()
      {
                    setOpaque(true);
          try
          {
             this.backgroundImage = ImageIO.read(new URL("http://www.ajmalbeig.addr.com/pgc/images/icons/icon_computer.GIF"));
          }
          catch(Exception e)
          {
             throw new RuntimeException(e);
          }
      }
    
      protected void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         g.drawImage(backgroundImage,30,70,null);
         g.drawImage(backgroundImage,580,70,null);
         g.setColor(Color.blue);
         g.fillRect(50, 50, 10, 10);
                    g.fillRect(90, 100,500, 14);
    repaint();
    }
    
    
    
     }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

  9. #9
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I know and I'm sorry, but I desperately need help with this problem...

  10. #10
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    It's nice of you to apologise, for future reference, if you do post the same question on multiple forums, simply link the other thread in your first post, this will avoid any resentment from the other forum-goers, and avoid time wasting if someone replies to your question, when it was already answered in the other thread.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  11. #11
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Ok, I'll do it. I even always tell solution to everybody if I find it :rolleyes:

    Greetings!

  12. #12
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I have solved part of a problem. To make rectangle resizing while moving slider I should only add this in JPanel:
    Java Code:
     protected void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         g.drawImage(backgroundImage,30,70,null);
         g.setColor(Color.blue);
         g.fillRect(50, 50, 10, 10);
         value=NewJApplet.slider1.getValue();
         g.fillRect(90, 100,value * 5, 14);
         g.drawImage(backgroundImage,580,70,null);
         repaint();
    }
    The variables value and slider1 should be declared as static int.

    I also want to make image move left-right while moving slider. How to do that? I tried exactly the same as with rectangle (to write value * 5 instead of 580 in drawImage, but it doesn't work). Any idea...?

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 10:51 PM
  2. To make image move
    By mneskovic in forum New To Java
    Replies: 4
    Last Post: 05-08-2010, 09:23 PM
  3. how to zoom using slider
    By kumar_gemi in forum New To Java
    Replies: 0
    Last Post: 07-11-2009, 11:06 PM
  4. How to use Slider in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:05 PM
  5. How do I make My ball to move randomly?
    By whdbstjr90 in forum New To Java
    Replies: 4
    Last Post: 12-31-2007, 06:32 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
  •