[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Updating canvas through textfield

Posted on 2005-04-17
15
Medium Priority
?
252 Views
Last Modified: 2010-03-31
In this program, I need to enter the values for the color into the text box and the color should change to the required value when I hit the enter key.  
Any help will be greatly appreciated ;-)

  //This application displays a chosen colour on a canvas
  //The user can change the colour by adjusting any of the 3 scrollbars
//package Colourchooser;
 //import methods
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.awt.Graphics.*;
   import java.util.*;

  public class ColourChooser extends JFrame implements AdjustmentListener, ActionListener{

    //set up the canvas object which will be displayed on a panel called row1
   // the colour will be displayed on this canvas
    JPanel row1 = new JPanel();

   
    //setting up the individual scrollbars and adding them to a panel called row2
    JPanel row2 = new JPanel();  
    Scrollbar sb1 = new Scrollbar(Scrollbar.HORIZONTAL,0,0,1,255);
    Scrollbar sb2 = new Scrollbar(Scrollbar.HORIZONTAL,0,0,1,255);
    Scrollbar sb3 = new Scrollbar(Scrollbar.HORIZONTAL,0,0,1,255);
   
    //setting up the labels and putting then in a panel called row3    
    JPanel row3 = new JPanel();
    JLabel rd = new JLabel("      Red");
    JLabel grn = new JLabel("     Green");
    JLabel bl = new JLabel("     Blue");

    //setting up the textfields that will display the individual colour values and then adding
   // them to a pnel called row4
    JPanel row4 = new JPanel();
    JTextField txt1 = new JTextField(10);
    JTextField txt2 = new JTextField(10);
    JTextField txt3 = new JTextField(10);

    DrawCanvas myCanvas = new DrawCanvas();    
   
   //setting up the container to hold the components (which are in there own panels)
   //and setting up the layout manager (grid layout)
      public ColourChooser() {
      super("Brendan Whelan");
      setSize(350,100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      GridLayout layout = new GridLayout(1,5);
      Container con = getContentPane();
      con.setLayout(layout);
      con.setVisible(true);

  //add the canvas to panel row1 and set the layout for the panel (flow layout)
  //set the size of the canvas to 65 pixels by 65 pixels
  FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);
  row1.setLayout(layout1);
  row1.add(myCanvas);
  con.add(row1);

  //set up the layout manager for the scrollbars (gridlayout) 1 column wide and 3 rows high
  //add the action listeners to the scrollbars
  GridLayout layout2 = new GridLayout (3,1);
      row2.setLayout(layout2);
      sb1.addAdjustmentListener(this);
      row2.add(sb1);
        sb2.addAdjustmentListener(this);        
      row2.add(sb2);
      sb3.addAdjustmentListener(this);    
      row2.add(sb3);
        con.add(row2);

  //set up the layout manager for the labels and add them to them to the container
  GridLayout layout3 = new GridLayout (3,1);
      row3.setLayout(layout3);
      row3.add(rd);
      row3.add(grn);
      row3.add(bl);
        con.add(row3);

  //set up the layout manager for the textfields and add them to the container
  GridLayout layout4 = new GridLayout (3,1);
      row4.setLayout(layout4);
      row4.add(txt1);
      txt1.addActionListener(this);
      row4.add(txt2);
      txt2.addActionListener(this);
      row4.add(txt3);
      txt3.addActionListener(this);
      con.add(row4);

  }
 
 //This method gets the value from the scrollbar & repaints the colour onto the canvas
 public void adjustmentValueChanged(AdjustmentEvent e) {
        // Gets the value from the selected scrollbar
        int r = sb1.getValue();
        int g = sb2.getValue();
        int b = sb3.getValue();
        // Sets the colour on the canvas
      myCanvas.setBackground(new Color(r,g,b));
      myCanvas.repaint();
      //This finds out which of the 3 scrolbars has been selected
      //and adjusts the value accordingly
      Object source = e.getSource();
      int i = ((Scrollbar)source).getValue();
      if (source == sb1) {
      txt1.setText("" + i);
  }
      else if (source == sb2) {
      txt2.setText("" + i);
  }
      else if (source == sb3) {
      txt3.setText("" + i);
  }
      }


  public void actionPerformed (ActionEvent e) {
      
      int r = sb1.getValue();
      int g = sb2.getValue();
      int b = sb3.getValue();
        sb1.setValue( Integer.parseInt( txt1.getText()) );
        sb2.setValue( Integer.parseInt( txt2.getText()) );
        sb3.setValue( Integer.parseInt( txt3.getText()) );
      myCanvas.setBackground(new Color(r,g,b));
      myCanvas.repaint();
  }
 



 public static void main (String[]args) {

      ColourChooser choose = new ColourChooser();
      choose.setVisible(true);

  }

}

*****************************************************************
 // package Colourchooser;


import java.awt.* ;


public class DrawCanvas extends Canvas




{
     // Variable declarations
       int red,green,blue;
       // int redValue, greenValue, blueValue;





     public DrawCanvas()
     {
         // set the size of the canvas
              setSize(65,65);
     }

     public void paintComponent(Graphics g)

  {
         // set the colour of the canvas
           g.setColor(getBackground());
           g.fillRect(0, 0, getWidth(), getHeight());

 }

     public void setCanvasColor(int red, int green, int blue)
     {
        // "repaint" the canvas(this will call the paint method)
                 setBackground(new Color(red, green, blue));
                 repaint();
     }

}


0
Comment
Question by:bwhelanuk
  • 8
  • 5
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802211
>>public void paintComponent(Graphics g)

should be

public void paint(Graphics g)

in your canvas
0
 

Author Comment

by:bwhelanuk
ID: 13802255
Have just tried that, when I enter in a value and hit enter, the scollbar moves, but te color doesn,t update
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802273
Works for me with my correction
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:bwhelanuk
ID: 13802311
Nah, the color doesn't change at all, even tho the scrollbar does.  I have tried it on both JCreator, and from the comand line on my laptop
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802326
Well it's got to really - it does for me ;-)  This is what i did

1. Copy-pasted the code as ColourChooser.java
2. Made the change i mentioned
3. Got rid of the import in DrawCanvas and removed 'public' in 'public class'
4. Compiled and ran it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802333
>> Nah, the color doesn't change at all

Of course you'll have to make noticeable changes - a few notches here and there won't make any visible difference
0
 

Author Comment

by:bwhelanuk
ID: 13802369
The color changes ok when I move the scrollbar, thats not the problem, I want it to also change if the user enters the number directly into the text field and hits enter. All that happens now if I enter, say 200 into teh textfield and hit enter, teh scrollbar moves ok but the color doesn't change at all
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802377
In that case, all i can think is that you're not running the code you posted with my change as i can change the colour fine by typing and hitting enter
0
 

Author Comment

by:bwhelanuk
ID: 13802415
Can u paste your code so I can compare, my eyes are going googly looking at it :-(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802424
Sure. Save it elsewhere as ColourChooser.java:

SNIP=========================================================================


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

public class ColourChooser extends JFrame implements AdjustmentListener,
            ActionListener {

      // set up the canvas object which will be displayed on a panel called row1
      // the colour will be displayed on this canvas
      JPanel row1 = new JPanel();

      // setting up the individual scrollbars and adding them to a panel called row2
      JPanel row2 = new JPanel();

      Scrollbar sb1 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 1, 255);

      Scrollbar sb2 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 1, 255);

      Scrollbar sb3 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 1, 255);

      // setting up the labels and putting then in a panel called row3
      JPanel row3 = new JPanel();

      JLabel rd = new JLabel("      Red");

      JLabel grn = new JLabel("     Green");

      JLabel bl = new JLabel("     Blue");

      // setting up the textfields that will display the individual colour values
      // and then adding
      // them to a pnel called row4
      JPanel row4 = new JPanel();

      JTextField txt1 = new JTextField(10);

      JTextField txt2 = new JTextField(10);

      JTextField txt3 = new JTextField(10);

      DrawCanvas myCanvas = new DrawCanvas();

      // setting up the container to hold the components (which are in there own
      // panels)
      // and setting up the layout manager (grid layout)
      public ColourChooser() {
            super("Brendan Whelan");
            setSize(350, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GridLayout layout = new GridLayout(1, 5);
            Container con = getContentPane();
            con.setLayout(layout);
            con.setVisible(true);

            // add the canvas to panel row1 and set the layout for the panel (flow
            // layout)
            // set the size of the canvas to 65 pixels by 65 pixels
            FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);
            row1.setLayout(layout1);
            row1.add(myCanvas);
            con.add(row1);

            // set up the layout manager for the scrollbars (gridlayout) 1 column wide
            // and 3 rows high
            // add the action listeners to the scrollbars
            GridLayout layout2 = new GridLayout(3, 1);
            row2.setLayout(layout2);
            sb1.addAdjustmentListener(this);
            row2.add(sb1);
            sb2.addAdjustmentListener(this);
            row2.add(sb2);
            sb3.addAdjustmentListener(this);
            row2.add(sb3);
            con.add(row2);

            // set up the layout manager for the labels and add them to them to the
            // container
            GridLayout layout3 = new GridLayout(3, 1);
            row3.setLayout(layout3);
            row3.add(rd);
            row3.add(grn);
            row3.add(bl);
            con.add(row3);

            // set up the layout manager for the textfields and add them to the
            // container
            GridLayout layout4 = new GridLayout(3, 1);
            row4.setLayout(layout4);
            row4.add(txt1);
            txt1.addActionListener(this);
            row4.add(txt2);
            txt2.addActionListener(this);
            row4.add(txt3);
            txt3.addActionListener(this);
            con.add(row4);

      }

      // This method gets the value from the scrollbar & repaints the colour onto
      // the canvas
      public void adjustmentValueChanged(AdjustmentEvent e) {
            // Gets the value from the selected scrollbar
            int r = sb1.getValue();
            int g = sb2.getValue();
            int b = sb3.getValue();
            // Sets the colour on the canvas
            myCanvas.setBackground(new Color(r, g, b));
            myCanvas.repaint();
            // This finds out which of the 3 scrolbars has been selected
            // and adjusts the value accordingly
            Object source = e.getSource();
            int i = ((Scrollbar) source).getValue();
            if (source == sb1) {
                  txt1.setText("" + i);
            } else if (source == sb2) {
                  txt2.setText("" + i);
            } else if (source == sb3) {
                  txt3.setText("" + i);
            }
      }

      public void actionPerformed(ActionEvent e) {

            int r = sb1.getValue();
            int g = sb2.getValue();
            int b = sb3.getValue();
            sb1.setValue(Integer.parseInt(txt1.getText()));
            sb2.setValue(Integer.parseInt(txt2.getText()));
            sb3.setValue(Integer.parseInt(txt3.getText()));
            myCanvas.setBackground(new Color(r, g, b));
            myCanvas.repaint();
      }

      public static void main(String[] args) {

            ColourChooser choose = new ColourChooser();
            choose.setVisible(true);

      }

}

class DrawCanvas extends Canvas

{
      // Variable declarations
      int red, green, blue;

      // int redValue, greenValue, blueValue;

      public DrawCanvas() {
            // set the size of the canvas
            setSize(65, 65);
      }

      public void paint(Graphics g) {
            // set the colour of the canvas
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());

      }

      public void setCanvasColor(int red, int green, int blue) {
            // "repaint" the canvas(this will call the paint method)
            setBackground(new Color(red, green, blue));
            repaint();
      }

}
0
 

Author Comment

by:bwhelanuk
ID: 13802444
Still not working, even with your code.  I have to nudge the scrollbar a notch and then the canvas displays the correct color, but not when I hit the enter button
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 13802509
Did you put it into a new directory? If so, then do the following:


set classpath=
javac ColourChooser.java
java ColourChooser
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13802514
(from the directory you put it in)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question