Intermediate Java Tutorial – 25 – Finishing the Oval Slider Program

///Intermediate Java Tutorial – 25 – Finishing the Oval Slider Program

Intermediate Java Tutorial – 25 – Finishing the Oval Slider Program

FavoriteLoadingAdd to favorites

Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –

source

Java468x60
By |2020-02-29T02:01:13+00:00February 29th, 2020|Java Video Tutorials|35 Comments

35 Comments

  1. rameen alikhani February 29, 2020 at 2:01 am - Reply

    the comedian bucky!

  2. Pheonixeye_1 February 29, 2020 at 2:01 am - Reply

    He is intelligent and most importantly he is funny,

  3. Ameer Abdallah February 29, 2020 at 2:01 am - Reply

    There is a great way for you guys to learn exactly how to manipulate sliders most effectively.

    Down below the code for making the slider have 2 separate values is posted. I also created a sync width and height checkbox which, when selected, will make the width and height of the slider equal to each other. I suggest trying it on your own before looking at my code and use my code for guidance. This may not be the best way to do it but in my opinion, it makes your mind wander outside of the box and allows you to think of ways to integrate your code into a real world program.

    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.awt.*;

    public class Window extends JFrame {

    private JSlider sliderWidth;
    private JSlider sliderHeight;
    private DrawOval myPanel;
    private JCheckBox sync;

    public Window() {
    super("Oval Slider");

    //Oval creation
    myPanel = new DrawOval();
    myPanel.setBackground(Color.ORANGE);
    add(myPanel);

    //JCheckBox
    sync = new JCheckBox("Sync Width and Height");
    add(sync, BorderLayout.EAST);

    //Slider width initialization
    sliderWidth = new JSlider(SwingConstants.HORIZONTAL, 0, 850, 10);
    sliderWidth.setMajorTickSpacing(10);
    sliderWidth.setPaintTicks(true);
    add(sliderWidth, BorderLayout.SOUTH);

    //Slider width change listener
    sliderWidth.addChangeListener(
    new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
    //if the checkbox is selected make sliderHeight position = sliderWidth position
    if(sync.isSelected()) {
    sliderHeight.setValue(sliderWidth.getValue());
    myPanel.setWidth(sliderWidth.getValue());

    }else myPanel.setWidth(sliderWidth.getValue());
    }
    }
    );

    //Slider height initialization
    sliderHeight = new JSlider(SwingConstants.VERTICAL, 0, 850, 10);
    sliderHeight.setMajorTickSpacing(10);
    sliderHeight.setPaintTicks(true);
    add(sliderHeight, BorderLayout.WEST);

    //Slider height change listener
    sliderHeight.addChangeListener(
    new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
    //if the checkbox is selected make sliderWidth position = sliderHeight position
    if(sync.isSelected()) {
    sliderWidth.setValue(sliderHeight.getValue());
    myPanel.setHeight(sliderHeight.getValue());

    }else myPanel.setHeight(sliderHeight.getValue());

    }
    }
    );

    }

    }

  4. Souhel Ayoub February 29, 2020 at 2:01 am - Reply

    Can anybody tell me what is wrong with this Slider? I still have error next to the line < new ChangeListener()>.
    package eng;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javafx.beans.value.ChangeListener;
    import javax.swing.JFrame;
    import javax.swing.JSlider;
    import javax.swing.SwingConstants;
    import javax.swing.event.ChangeEvent;

    public class TheWindow extends JFrame{

    private JSlider slider;
    private DrawOval myPanel;

    public TheWindow(){
    super("the Title");
    myPanel=new DrawOval();
    myPanel.setBackground(Color.orange);
    slider=new JSlider(SwingConstants.HORIZONTAL,0,200,10);
    slider.setMajorTickSpacing(10);
    slider.setBackground(Color.yellow);
    slider.setPaintTicks(true);

    slider.addChangeListener(
    new ChangeListener(){
    /*anynomous ….is not abstract and does not override abstract method changed(ObservableValue, Object,Object)in Changelistener.. … can't be converted to javax.swing.event.ChangeListener ….. */
    public void stateChanged(ChangeEvent e){
    myPanel.setD(slider.getValue());
    }
    }
    );
    add(slider, BorderLayout.SOUTH);
    add(myPanel, BorderLayout.CENTER);
    }
    }

  5. Vaishnav Garodia February 29, 2020 at 2:01 am - Reply

    Bucky you are a expert in so many languages that I think Microsoft would really accept you as their lead programmer

  6. Patrascu Lucian February 29, 2020 at 2:01 am - Reply

    This oval is a circle. Also, if you use g.fillOval(10, 10, 200, 100); you just draw an eclipse, which is awesome especially if you're using eclipse

  7. Roshan Zaid February 29, 2020 at 2:01 am - Reply

    All what i get is a grey box as the output. neither the oval nor the slider. bucky roberts! i care of your piece of cake. need a solution soon.

  8. Tom Spencer February 29, 2020 at 2:01 am - Reply

    FYI for anyone who is interested:-

    g.fillOval(110-(d/2), 110-(d/2), d, d);

    put that in paintCompnent in the drawOval class in place of the original g.fillOval line and it will draw the circle in the center of the screen so it won't grow from the corner, but instead expand out from the center.

  9. Tom Spencer February 29, 2020 at 2:01 am - Reply

    /Finishes slider program /Sits there sliding the slider back and forth very fast /Feels like hees being sucked into a black hole /Realises Bucky is a James Bond villain who is trying to destroy us all!!!!!!

  10. WB February 29, 2020 at 2:01 am - Reply

    yeah, the paintComponent() was paint() for me.

  11. Vivek Choudhary February 29, 2020 at 2:01 am - Reply

    .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ,isn't working it is showing error(The method setDefaultCloseOperation(int) is undefined for the type animationscontinue)

  12. hello February 29, 2020 at 2:01 am - Reply

    did any made a game or any app using knowledge upto this tutorial ……..if yes then PLEASE reply ….because that will help everyone to come up with something new and MOST important Interest will persist

  13. kadinscido February 29, 2020 at 2:01 am - Reply

    So lemme get this straight….there's a method, inside a method, inside an argument? (ChangeListener)

  14. meriem karoun February 29, 2020 at 2:01 am - Reply

    If you have problem with the oval not showing, it s in the method PaintComponent, make sure you spell it right!

  15. Kidane Yosief February 29, 2020 at 2:01 am - Reply

    Why could not I see my circle in the Panel please?

  16. vljzlj February 29, 2020 at 2:01 am - Reply

    if you press ctrl + Shift + o in eclipse it imports every thing your program needs to run correctly , just press that after u finished coding 🙂 so u dont need to mess with ' ah i cant remember what I need to import'

  17. bibek shakya February 29, 2020 at 2:01 am - Reply

    programmer +comedian

  18. Jinhua Chen February 29, 2020 at 2:01 am - Reply

    TheWindow.java:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class TheWindow extends JFrame{

        private JSlider slider;
        private DrawOval myPanel;

        public TheWindow() {
            super("The title");

            myPanel = new DrawOval();
            myPanel.setBackground(Color.ORANGE);

            slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
            slider.setMajorTickSpacing(10);
            slider.setPaintTicks(true);
            slider.addChangeListener(
                    new ChangeListener() {
                       @Override
                        public void stateChanged(ChangeEvent e) {
                            myPanel.setD(slider.getValue());
                        }
                    }
            );

            add(slider, BorderLayout.SOUTH);
            add(myPanel, BorderLayout.CENTER);

        }

    }

    DrawOval.java:

    import java.awt.*;
    import javax.swing.*;

    public class DrawOval extends JPanel{

        private int d = 10;

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillOval(10, 10, d, d);
        }

        public void setD(int newD) {
            d = (newD >=0 ? newD : 10);
            repaint();
        }

        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public Dimension getMinimumSize() {
            return getPreferredSize();
        }
    }

  19. The Humanity February 29, 2020 at 2:01 am - Reply

    the paintComponent() method doesn't register as the JPanel built in method. For me, the paint() method worked with super.paint(g) instead of paintComponent

  20. Bryan Westerveld February 29, 2020 at 2:01 am - Reply

    My circle doesn't want to become bigger no matter how much I slide.

  21. Suman Gurung February 29, 2020 at 2:01 am - Reply

    I checked and re checked. did exactly like in the video and when i execute it 
    it only shows window with slider Heeeelp !! WTF is happening

  22. Shivashriganesh Mahato February 29, 2020 at 2:01 am - Reply

    Have a fun time in Microsoft!

  23. Kristiyan Gostev February 29, 2020 at 2:01 am - Reply

    If you are linux user (I use GNOME), add
    myPanel.repaint();
     after
    add(slider, BorderLayout.SOUTH);
    add(myPanel, BorderLayout.CENTER);
    or the circle will not show.

  24. DINESHKUMAR PHALWADIYA February 29, 2020 at 2:01 am - Reply

    thanks for your clean and understandable tutorials

  25. Tan Yi Syuen February 29, 2020 at 2:01 am - Reply

    how to change the colour of oval ?

  26. pta2002 February 29, 2020 at 2:01 am - Reply

    Bucky thanks for all your tuts! They are great 🙂

  27. Pathfinder February 29, 2020 at 2:01 am - Reply

    Hey, Bucky! you're unbelievable! Thanks for all your great tutorials, I love them and they are helping a lot. As someone already said before, you' re doing the world a great service. I can only confirm that. Greetings from Europe!

  28. Jan February 29, 2020 at 2:01 am - Reply

    Could anyone help me out. I wrote out the code exactly as Bucky instructs, but I when I compile and run it, I get a white window with a thin horizontal orange bar at the bottom. Damn, what's going on?? I'm just wandering if it's a java issue with graphics. I've seen other tutorials use graphics2D instead.

  29. Max Parker February 29, 2020 at 2:01 am - Reply

    I have no errors in my code but it just says built in 0 sec. in the console and never opens. what am i missing?

  30. Stephen Garner February 29, 2020 at 2:01 am - Reply

    Thanks dude! You're doing the world a great service! Hope you're still making productive video tutorials. Hope the fish is doing well. 

  31. Allan Nouri February 29, 2020 at 2:01 am - Reply

    Maybe the turds behind Skype should listen to Bucky's lecture on setting the default close operation. Fucking HIDE_ON_CLOSE-bastards.

  32. Quand9 February 29, 2020 at 2:01 am - Reply

    <3 thanks man, i think i finally understand how JFrames interact with JPanels and how to draw things because of you 🙂

  33. XIAO February 29, 2020 at 2:01 am - Reply

    developers uses engines that makes everything 10x faster.. but ok

  34. aaron priester February 29, 2020 at 2:01 am - Reply

    If anyone actually wants this "program" properly just add a little repaint(); right after the panel.setD(slider.getValue()); in the stateChanged() method. 🙂

  35. John Kuchta February 29, 2020 at 2:01 am - Reply

    simple, awesome and educational. thanks Bucky.

Leave A Comment

*