bwhelanuk
asked on
Updating canvas through textfield
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.HORIZO NTAL,0,0,1 ,255);
Scrollbar sb2 = new Scrollbar(Scrollbar.HORIZO NTAL,0,0,1 ,255);
Scrollbar sb3 = new Scrollbar(Scrollbar.HORIZO NTAL,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(J Frame.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.CENT ER);
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(thi s);
row4.add(txt2);
txt2.addActionListener(thi s);
row4.add(txt3);
txt3.addActionListener(thi s);
con.add(row4);
}
//This method gets the value from the scrollbar & repaints the colour onto the canvas
public void adjustmentValueChanged(Adj ustmentEve nt 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).getVal ue();
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();
}
}
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.HORIZO
Scrollbar sb2 = new Scrollbar(Scrollbar.HORIZO
Scrollbar sb3 = new Scrollbar(Scrollbar.HORIZO
//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(J
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.CENT
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(
row2.add(sb1);
sb2.addAdjustmentListener(
row2.add(sb2);
sb3.addAdjustmentListener(
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(thi
row4.add(txt2);
txt2.addActionListener(thi
row4.add(txt3);
txt3.addActionListener(thi
con.add(row4);
}
//This method gets the value from the scrollbar & repaints the colour onto the canvas
public void adjustmentValueChanged(Adj
// 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
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).getVal
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
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();
}
}
ASKER
Have just tried that, when I enter in a value and hit enter, the scollbar moves, but te color doesn,t update
Works for me with my correction
ASKER
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
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
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
>> 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
Of course you'll have to make noticeable changes - a few notches here and there won't make any visible difference
ASKER
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
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
ASKER
Can u paste your code so I can compare, my eyes are going googly looking at it :-(
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.HORIZO NTAL, 0, 0, 1, 255);
Scrollbar sb2 = new Scrollbar(Scrollbar.HORIZO NTAL, 0, 0, 1, 255);
Scrollbar sb3 = new Scrollbar(Scrollbar.HORIZO NTAL, 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(J Frame.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.CENT ER);
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(thi s);
row4.add(txt2);
txt2.addActionListener(thi s);
row4.add(txt3);
txt3.addActionListener(thi s);
con.add(row4);
}
// This method gets the value from the scrollbar & repaints the colour onto
// the canvas
public void adjustmentValueChanged(Adj ustmentEve nt 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(ActionEven t e) {
int r = sb1.getValue();
int g = sb2.getValue();
int b = sb3.getValue();
sb1.setValue(Integer.parse Int(txt1.g etText())) ;
sb2.setValue(Integer.parse Int(txt2.g etText())) ;
sb3.setValue(Integer.parse Int(txt3.g etText())) ;
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();
}
}
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.HORIZO
Scrollbar sb2 = new Scrollbar(Scrollbar.HORIZO
Scrollbar sb3 = new Scrollbar(Scrollbar.HORIZO
// 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(J
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.CENT
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(
row2.add(sb1);
sb2.addAdjustmentListener(
row2.add(sb2);
sb3.addAdjustmentListener(
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(thi
row4.add(txt2);
txt2.addActionListener(thi
row4.add(txt3);
txt3.addActionListener(thi
con.add(row4);
}
// This method gets the value from the scrollbar & repaints the colour onto
// the canvas
public void adjustmentValueChanged(Adj
// 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
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(ActionEven
int r = sb1.getValue();
int g = sb2.getValue();
int b = sb3.getValue();
sb1.setValue(Integer.parse
sb2.setValue(Integer.parse
sb3.setValue(Integer.parse
myCanvas.setBackground(new
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();
}
}
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(from the directory you put it in)
should be
public void paint(Graphics g)
in your canvas