|
-
Jun 1st, 2006, 12:02 AM
#1
Thread Starter
Hyperactive Member
[SOLVED] Change the color by button.
I am currently reading learn java in 21 days and I am trying to combine two examples from the book to create an applet that lets you draw on the page. I put into it the buttons example that lets you change colors of the background, but what I am trying to do is instead of changing the background, change the color to draw with.
I tried making it so that the if statements would change the color of the graphics object g but I believe since it is in a different method, paint, that it just won't work this way. I then also tried to keep it changing the foreground color in hopes it would change the color used to paint with, but still no go.
If someone has the time, could you please explain what needs to happen in order for the color during paint to change to what button is pressed?
Everything looks like its good up until the point where the color needs to change after the button is pressed. Or so I believe.
VB Code:
import java.awt.*;
public class DrawingApplet extends java.applet.Applet {
final int MAXSPOTS = 100350;
int xspots[] = new int[MAXSPOTS];
int yspots[] = new int[MAXSPOTS];
int currspots = 0;
public void init() {
setBackground(Color.white);
add(new Button("Red"));
add(new Button("Blue"));
add(new Button("Green"));
add(new Button("White"));
add(new Button("Black"));
}
public boolean mouseDown(Event evt, int x, int y) {
if (currspots < MAXSPOTS) {
addspot(x,y);
return true;
}
else {
System.out.println("Too many spots.");
return false;
}
}
public boolean mouseDrag(Event evt, int x, int y) {
addspot(x,y);
return true;
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button) {
changeColor((String)arg);
return true;
} else return false;
}
void addspot(int x,int y) {
xspots[currspots] = x;
yspots[currspots] = y;
currspots++;
repaint();
}
public void paint(Graphics g) {
g.setColor(Color.blue);
for (int i = 0; i < currspots; i++) {
g.fillOval(xspots[i] - 5, yspots[i] - 5, 10, 10);
}
}
void changeColor(String bname) {
if (bname.equals("Red")) setForeground(Color.red);
else if (bname.equals("Blue")) setForeground(Color.blue);
else if (bname.equals("Green")) setForeground(Color.green);
else if (bname.equals("White")) setForeground(Color.white);
else setForeground(Color.black);
repaint();
}
}
Last edited by gjon; Jun 1st, 2006 at 11:52 PM.
-
Jun 1st, 2006, 04:21 AM
#2
Re: Change the color by button.
Code:
import java.awt.* ;
import java.awt.event.* ;
public class DrawingApplet extends java.applet.Applet implements ActionListener,
MouseListener, MouseMotionListener
{
private final int MAXSPOTS = 100350 ;
private int xspots[] = new int[MAXSPOTS] ;
private int yspots[] = new int[MAXSPOTS] ;
private int currspots = 0 ;
private Color drawingColor = Color.BLUE ;
private Button[] btn = new Button[5] ;
public void init () {
setBackground(Color.white) ;
btn[0] = new Button("Red") ;
btn[1] = new Button("Blue") ;
btn[2] = new Button("Green") ;
btn[3] = new Button("White") ;
btn[4] = new Button("Black") ;
for (int i = 0 ; i < btn.length ; i++) {
btn[i].addActionListener(this) ;
add(btn[i]) ;
}
addMouseListener(this) ;
addMouseMotionListener(this) ;
}
private void addspot (int x, int y) {
xspots[currspots] = x ;
yspots[currspots] = y ;
currspots++ ;
repaint() ;
}
public void paint (Graphics g) {
g.setColor(drawingColor) ;
for (int i = 0 ; i < currspots ; i++) {
g.fillOval(xspots[i] - 5, yspots[i] - 5, 10, 10) ;
}
}
private void changeColor (String bname) {
if (bname.equals("Red")) {
drawingColor = Color.red ;
}
else if (bname.equals("Blue")) {
drawingColor = Color.blue ;
}
else if (bname.equals("Green")) {
drawingColor = Color.green ;
}
else if (bname.equals("White")) {
drawingColor = Color.white ;
}
else {
drawingColor = Color.black ;
}
repaint() ;
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() instanceof Button) {
changeColor(((Button) e.getSource()).getLabel()) ;
}
}
public void mouseDragged (MouseEvent e) {
addspot(e.getX(), e.getY()) ;
}
public void mousePressed (MouseEvent e) {
if (currspots < MAXSPOTS) {
addspot(e.getX(), e.getY()) ;
}
else {
System.out.println("Too many spots.") ;
}
}
public void mouseReleased (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseMoved (MouseEvent e) {}
public void mouseClicked (MouseEvent e) {}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 1st, 2006, 04:23 AM
#3
Re: Change the color by button.
I had to change in your code, because you are using so many deprecated methods...
The thing I had to change to let your code work the way you want it to is "Creating the drawingColor object and using it in the paint method"
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 1st, 2006, 12:40 PM
#4
Thread Starter
Hyperactive Member
Re: Change the color by button.
private Color drawingColor = Color.BLUE ;
That is awesome how that can be changed through colorchange method.
The array of buttons is really a nice touch too!
I am trying to understand what you have shown me with these listeners.
I think I read that the mouseevents listed at the bottom are there only because they have to be.
So I think what happens with addMouseListener(this) ;
addMouseMotionListener(this) ; is that the methods:
public void actionPerformed (ActionEvent e)
public void mouseDragged (MouseEvent e)
public void mousePressed (MouseEvent e)
are activated without having to call them because they are listening for these actions in this object and respond to them.
Thank you so much for showing me this. When you say deprecated, I think that this may mean the book is older than I thought it was. I am half way through it, I think I will finish it and then pick up a newer book. What I am really interested in is a java server and client applet. It is really an effort for me to be patient and learn as much as I can before I get to that section.
If I misunderstood anything, please let me know, thanks again.
-
Jun 1st, 2006, 04:16 PM
#5
Re: Change the color by button.
I suggest you try Java API specification for further information.
And it'd be better if you learn java 1.5 since it's the most recent "Alpha" edition.
Last edited by ComputerJy; Jun 1st, 2006 at 05:21 PM.
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 1st, 2006, 04:59 PM
#6
Thread Starter
Hyperactive Member
Re: Change the color by button.
Thanks for the link. I was wondering after looking through the list. I see that they all provide a defenition. But is there a way to see an example of how each specification is used?
Like for instance, under java.net, socket(), just reading the defenition doesn't really tell me a lot about how to go about using it, well, i guess it should if I had the experience, but unfortunately that is what i need; how else am i to get experience? I just wonder if there is a site that you can input the class/method/api reference into and get a sample of how to use it. Or maybe the best way is to plug in an idea into a search engine/javasun search and see what comes up?
-
Jun 1st, 2006, 05:24 PM
#7
Re: Change the color by button.
actually sun provides some good code samples. take a look at this page, it's very good for beginners
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 1st, 2006, 07:43 PM
#8
Thread Starter
Hyperactive Member
Re: Change the color by button.
Awesome! Thank you very much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|