11 Replies - 438 Views - Last Post: 17 May 2014 - 09:50 AM Rate Topic: -----

#1 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Using Jslider To draw rectangle in JTextField in different colors

Posted 15 May 2014 - 04:35 PM

[color]
public class MyColorChooser extends JPanel implements ChangeListener{


private JSlider redSlider;
private JSlider greenSlider;
private JSlider blueSlider;
private JTextField txt1;
private JTextField txt2;
private JTextField txt3;
private final Color[] colorValues =
{Color.red, Color.green, Color.blue};

//no-argument constructor
public MyColorChooser()
{
super("Testing JTextField and JSlider ");
setLayout(new FlowLayout() );



//contruct text field with 10 columns
txt1 = new JTextField("red");
add(txt1);// add txt1 to JFrame

txt2 = new JTextField("green");
add(txt2);

txt3 = new JTextField("blue");
add(txt3);

// register event handlers for textFields
TextFieldHandler handler = new TextFieldHandler();
txt1.addActionListener(handler);
txt2.addActionListener(handler);
txt3.addActionListener(handler);


}
private class TextFieldHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String string = "";

//event for textField
if(event.getSource() == txt1);



//event for textField
if(event.getSource() == txt2);





//event for textField
if(event.getSource() == txt3);
}

}



}
[/code]

Do i have to make a new no argument constructor for my sliders? Im sorry im still new at programming and confused.

Is This A Good Question/Topic? 0
  • +

Replies To: Using Jslider To draw rectangle in JTextField in different colors

#2 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 15 May 2014 - 04:45 PM

public class MyColorChooser  extends JPanel implements ChangeListener{

   
    private JSlider redSlider;
    private JSlider greenSlider;
    private JSlider blueSlider;
    private JTextField txt1;
    private JTextField txt2;
    private JTextField txt3;
    private final Color[] colorValues =
    {Color.red, Color.green, Color.blue};
   
    //no-argument constructor
    public MyColorChooser()
    {
        super("Testing JTextField and JSlider ");
                setLayout(new FlowLayout() );
                
                redSlider = 
                        new JSlider(SwingConstants.HORIZONTAL,0,255,10);
                redSlider.setMajorTickSpacing(5);
                redSlider.setPaintTicks(true);
                
                
                greenSlider = 
                        new JSlider(SwingConstants.HORIZONTAL,0,255,10);
                greenSlider.setMajorTickSpacing(5);
                greenSlider.setPaintTicks(true);
                
                blueSlider = 
                        new JSlider(SwingConstants.HORIZONTAL,0,255,10);
                blueSlider.setMajorTickSpacing(5);
                blueSlider.setPaintTicks(true);
                
                //register JSlider event listener
                
                
                
                
                //contruct text field with 10 columns
                txt1 = new JTextField("red");
                add(txt1);// add txt1 to JFrame
                
                txt2 = new JTextField("green");
                add(txt2);
                
                txt3 = new JTextField("blue");
                add(txt3);
                
                // register event handlers for textFields
                TextFieldHandler handler = new TextFieldHandler();
                txt1.addActionListener(handler);
                txt2.addActionListener(handler);
                txt3.addActionListener(handler);
                
                    
                }
    private class TextFieldHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String string = "";
            
           //event for textField
            if(event.getSource() == txt1);
            
            
            
            //event for textField
            if(event.getSource() == txt2);
            
            
            
            
            
            //event for textField
            if(event.getSource() == txt3);
        }
                
    }

    
    
}


This is what i mean. Also, do i have register the event listeners for each JSlider separately.
Was This Post Helpful? 0
  • +
  • -

#3 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 16 May 2014 - 12:55 PM

public class MyColorChooser  extends JPanel implements ChangeListener{

   
    private JSlider redSlider;
    private JSlider greenSlider;
    private JSlider blueSlider;
    private JTextField txt1;
    private JTextField txt2;
    private JTextField txt3;
    private final Color[] colorValues =
    {Color.red, Color.green, Color.blue};
   
    //no-argument constructor
    public MyColorChooser()
    {
        super();
                setLayout(new FlowLayout() );
        RectPanel myPanel = new RectPanel();
        myPanel.setBackground(Color.WHITE);
                
                redSlider = 
                        new JSlider(SwingConstants.HORIZONTAL,0,255,10);
                redSlider.setMajorTickSpacing(5);
                redSlider.setPaintTicks(true);
                redSlider.add(this);
                
                
                greenSlider = 
                        new JSlider(SwingConstants.HORIZONTAL,0,255,10);
                greenSlider.setMajorTickSpacing(5);
                greenSlider.setPaintTicks(true);
                greenSlider.add(this);
                
                blueSlider = 
                        new JSlider(SwingConstants.HORIZONTAL,0,255,10);
                blueSlider.setMajorTickSpacing(5);
                blueSlider.setPaintTicks(true);
                blueSlider.add(this);
                
                //register JSlider event listener
                
                
                
                
                //contruct text field with 10 columns
                txt1 = new JTextField("red");
                add(txt1);// add txt1 to JFrame
                
                txt2 = new JTextField("green");
                add(txt2);
                
                txt3 = new JTextField("blue");
                add(txt3);
                
                // register event handlers for textFields
                TextFieldHandler handler = new TextFieldHandler();
                txt1.addActionListener(handler);
                txt2.addActionListener(handler);
                txt3.addActionListener(handler);
                
                    
                }

    void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   
    private class TextFieldHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String string = "";
            
           //event for textField
            if(event.getSource() == txt1);
            
            
            
            //event for textField
            if(event.getSource() == txt2);
            
            
            
            
            
            //event for textField
            if(event.getSource() == txt3);
        }
                
    }


	@Override
	public void stateChanged(ChangeEvent arg0) {
            
		
		
	}

    
    
}

public class RectPanel extends JPanel{
    
    private int x = 10;
    private int y = 10;
    private int width = 500;
    private int height = 900;
    
    //draw an rectangle 
    public void paintComponet( Graphics g)
    {
        super.paintComponent(g);
        g.fill3DRect(x, y, width, height, true);
    }
    
    
    public void setX( int newX)
    {
        // if x invalid, default to 10
        x = (newX  >= 0 ? newX : 10);
        repaint();
    }
    
    public void setY( int newY)
    {
        // if x invalid, default to 10
        y = (newY  >= 0 ? newY : 10);
        repaint();
    }
    // used by layout manager to size
    public Dimension getPreferredSize()
    {
        return new Dimension(500, 900);
    }
    
    public Dimension getMinimumSize()
    {
        return getPreferredSize();
    }
}

public class MyColorChooserTest {
    public static void main(String[] args)
    {
        MyColorChooser myColorChooser = new MyColorChooser();
        myColorChooser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myColorChooser.setSize(500, 900);
        myColorChooser.setVisible(true);
        
        
    }
}





Im suppose to create a subclass of Jpanel and create 3 JSliders and 3 JTextFields. The Jsliders are suppose to draw a rectangle in different colors and have the vaule of the Jslider displayed on textfield.
Was This Post Helpful? 0
  • +
  • -

#4 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 16 May 2014 - 01:12 PM

Can you explain the logic and what i'm doing wrong please? I really want to become a more proficient programming.
Was This Post Helpful? -1
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 16 May 2014 - 01:23 PM

Please - let's not bump your topic every fifteen minutes. It's still the first day, and still at the top of the page for the forum.

Also - do not create duplicate topics.. merging.
Was This Post Helpful? 1
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 725
  • View blog
  • Posts: 3,161
  • Joined: 03-December 12

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 16 May 2014 - 01:32 PM

Let's start with what don't you understand?
Was This Post Helpful? 1
  • +
  • -

#7 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 16 May 2014 - 04:56 PM

View Postmodi123_1, on 16 May 2014 - 01:23 PM, said:

Please - let's not bump your topic every fifteen minutes. It's still the first day, and still at the top of the page for the forum.

Also - do not create duplicate topics.. merging.

I'm sorry i don't know how to delete the posts.

View Postastonecipher, on 16 May 2014 - 01:32 PM, said:

Let's start with what don't you understand?

I'm not sure how to set the action events for the sliders to draw the rectangle.
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 725
  • View blog
  • Posts: 3,161
  • Joined: 03-December 12

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 17 May 2014 - 01:54 AM

Have you looked at how java's a tionListener guide?
Was This Post Helpful? 1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,726
  • Joined: 20-September 08

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 17 May 2014 - 05:26 AM

That's going a little messy. Compile run and study the following. Note your FlowLayout is fragile: resizing the window will put the components out of whack. You need to study layout managers such as the BoxLayout

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;
import javax.swing.event.*;


public class RectangleDrawer extends JFrame {
    private Color activeColor;
    private RectanglePanel rectanglePanel;

    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
	    cp.setLayout(new GridLayout(2, 1));
            cp.add(new MyColorChooser());
	    rectanglePanel = new RectanglePanel();
	    cp.add(rectanglePanel);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        RectangleDrawer f = new RectangleDrawer();
                        f.setGui();
                        f.setSize(220, 400);
                        f.setVisible(true);
                    }
                });
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    class RectanglePanel extends JPanel {
	@Override
	public void paintComponent(Graphics g) {
	    Graphics2D g2d = (Graphics2D)g;
	    g2d.setStroke(new BasicStroke(5));
	    g2d.setPaint(activeColor);
	    int w = getWidth();
	    int h = getHeight();
	    g2d.draw(new Rectangle2D.Double(5, 5, w - 10, h - 10));
	}
    }

    class MyColorChooser extends JPanel implements ChangeListener {
        private JSlider redSlider;
        private JSlider greenSlider;
        private JSlider blueSlider;
        private JTextField txt1;
        private JTextField txt2;
        private JTextField txt3;
        private final Color[] colorValues = { Color.red, Color.green, Color.blue };

        //no-argument constructor
        public MyColorChooser() {
            super();
            setLayout(new FlowLayout());

            //RectPanel myPanel = new RectPanel();
            //myPanel.setBackground(Color.WHITE);
            redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 10);
            redSlider.setMajorTickSpacing(5);
            redSlider.setPaintTicks(true);
            redSlider.addChangeListener(this);
            add(redSlider);

            greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 10);
            greenSlider.setMajorTickSpacing(5);
            greenSlider.setPaintTicks(true);
            greenSlider.addChangeListener(this);
            add(greenSlider);

            blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 10);
            blueSlider.setMajorTickSpacing(5);
            blueSlider.setPaintTicks(true);
            blueSlider.addChangeListener(this);
            add(blueSlider);

            //register JSlider event listener

            //contruct text field with 10 columns
            txt1 = new JTextField(" red");
            add(txt1); // add txt1 to JFrame

            txt2 = new JTextField("green");
            add(txt2);

            txt3 = new JTextField("blue");
            add(txt3);

        }

        @Override
        public void stateChanged(ChangeEvent arg0) {
            if (arg0.getSource() == redSlider) {
                txt1.setText(Integer.toString(redSlider.getValue()));
            }
	    else if (arg0.getSource() == greenSlider) {
                txt2.setText(Integer.toString(greenSlider.getValue()));
            }
	    else if (arg0.getSource() == blueSlider) {
                txt3.setText(Integer.toString(blueSlider.getValue()));
            }
	    activeColor = new Color(redSlider.getValue(),greenSlider.getValue(),blueSlider.getValue());
	    rectanglePanel.repaint();
        }

    }
}


Was This Post Helpful? 1
  • +
  • -

#10 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 17 May 2014 - 07:07 AM

View Postastonecipher, on 17 May 2014 - 01:54 AM, said:

I did read up on it but it's still relatively new to me. Thanks
Was This Post Helpful? 0
  • +
  • -

#11 jrab504  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 08-May 14

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 17 May 2014 - 08:01 AM

View Postg00se, on 17 May 2014 - 05:26 AM, said:

That's going a little messy. Compile run and study the following. Note your FlowLayout is fragile: resizing the window will put the components out of whack. You need to study layout managers such as the BoxLayout

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;
import javax.swing.event.*;


public class RectangleDrawer extends JFrame {
    private Color activeColor;
    private RectanglePanel rectanglePanel;

    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
	    cp.setLayout(new GridLayout(2, 1));
            cp.add(new MyColorChooser());
	    rectanglePanel = new RectanglePanel();
	    cp.add(rectanglePanel);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        RectangleDrawer f = new RectangleDrawer();
                        f.setGui();
                        f.setSize(220, 400);
                        f.setVisible(true);
                    }
                });
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    class RectanglePanel extends JPanel {
	@Override
	public void paintComponent(Graphics g) {
	    Graphics2D g2d = (Graphics2D)g;
	    g2d.setStroke(new BasicStroke(5));
	    g2d.setPaint(activeColor);
	    int w = getWidth();
	    int h = getHeight();
	    g2d.draw(new Rectangle2D.Double(5, 5, w - 10, h - 10));
	}
    }

    class MyColorChooser extends JPanel implements ChangeListener {
        private JSlider redSlider;
        private JSlider greenSlider;
        private JSlider blueSlider;
        private JTextField txt1;
        private JTextField txt2;
        private JTextField txt3;
        private final Color[] colorValues = { Color.red, Color.green, Color.blue };

        //no-argument constructor
        public MyColorChooser() {
            super();
            setLayout(new FlowLayout());

            //RectPanel myPanel = new RectPanel();
            //myPanel.setBackground(Color.WHITE);
            redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 10);
            redSlider.setMajorTickSpacing(5);
            redSlider.setPaintTicks(true);
            redSlider.addChangeListener(this);
            add(redSlider);

            greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 10);
            greenSlider.setMajorTickSpacing(5);
            greenSlider.setPaintTicks(true);
            greenSlider.addChangeListener(this);
            add(greenSlider);

            blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 10);
            blueSlider.setMajorTickSpacing(5);
            blueSlider.setPaintTicks(true);
            blueSlider.addChangeListener(this);
            add(blueSlider);

            //register JSlider event listener

            //contruct text field with 10 columns
            txt1 = new JTextField(" red");
            add(txt1); // add txt1 to JFrame

            txt2 = new JTextField("green");
            add(txt2);

            txt3 = new JTextField("blue");
            add(txt3);

        }

        @Override
        public void stateChanged(ChangeEvent arg0) {
            if (arg0.getSource() == redSlider) {
                txt1.setText(Integer.toString(redSlider.getValue()));
            }
	    else if (arg0.getSource() == greenSlider) {
                txt2.setText(Integer.toString(greenSlider.getValue()));
            }
	    else if (arg0.getSource() == blueSlider) {
                txt3.setText(Integer.toString(blueSlider.getValue()));
            }
	    activeColor = new Color(redSlider.getValue(),greenSlider.getValue(),blueSlider.getValue());
	    rectanglePanel.repaint();
        }

    }
}


With the stateChanged method, can you explain that to me please? Also, how do you know how to do that. Does it come with experience or is their a way i can look up information that will help me when i get stuck? Like if i get stuck with a method or an argument, will looking up the API help me?
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,726
  • Joined: 20-September 08

Re: Using Jslider To draw rectangle in JTextField in different colors

Posted 17 May 2014 - 09:50 AM

Read the api docs
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1