Hey guys,
I have this assignment due soon and need some help with it. I don't want the solution handed to me, but I also don't want some extremely vague answers either. I want to learn from this.
Basically, what I am making is a simple applet, that has 3 buttons across in the NORTH section of a BorderLayout, and a Panel in the CENTER section.
The buttons are as follows:
- Paint Brush
- Line Tool
- Rectangle Tool
It is a paint program as you have guessed. Now, I have gotten it to work only so far. I can click on one of the buttons, and then I can use the tool described, and then when I click on another different button, I can use that tool, but the drawing already there goes. Like a repaint or something.
Here is the code I have so far, I know what the problem is, I just don't know how to get around it. The problem is that I am creating instances of the tools class, and then adding it to the panel after I remove the others. It doesn't work otherwise.
import java.applet.Applet;
import java.awt.*;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JavaDraw extends Applet implements ActionListener {
private Button paintBrush;
private Button lineTool;
private Button rectangleTool;
private BorderLayout layout = new BorderLayout(); // Declaring the layout of the app
private Panel buttonsMenu = new Panel(); // Creat a new panel for the buttons
private Panel drawingPanel = new Panel();
PaintBrush pb = new PaintBrush();
LineTool lt = new LineTool();
Rectangle rec = new Rectangle();
public void init() {
this.setSize(800, 500); // Setting the size of the window
drawingPanel.setSize(800, 480);
setLayout(layout); // Setting the layout
this.paintBrush = new Button("Paint Brush"); // Creating a button for the 'paint brush'
this.lineTool = new Button("Line Tool"); // Creating a button for the 'line tool'
this.rectangleTool = new Button("Rectangle Tool"); // Creating a button for the 'rectangle tool'
buttonsMenu.setLayout(new FlowLayout(FlowLayout.LEFT)); // Align the buttons to the left
buttonsMenu.setBackground(new Color(221324)); // Set the background color to diferenciate it from the drawing canvas
this.paintBrush.addActionListener(this); //
this.lineTool.addActionListener(this); // Creating actionListeners on the buttons
this.rectangleTool.addActionListener(this); //
buttonsMenu.add(paintBrush); //
buttonsMenu.add(lineTool); // Adding the buttons to the buttonsMenu panel
buttonsMenu.add(rectangleTool); //
this.add("North", buttonsMenu); // Add the buttonsMenu panel to the 'North' position in the main layout
this.add("Center", drawingPanel);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Paint Brush")){
pb.setSize(800,480);
drawingPanel.remove(lt);
drawingPanel.remove(rec);
drawingPanel.add(pb);
} else if (e.getActionCommand().equals("Line Tool")){
lt.setSize(800,480);
drawingPanel.remove(pb);
drawingPanel.remove(rec);
drawingPanel.add(lt);
} else if (e.getActionCommand().equals("Rectangle Tool")){
rec.setSize(800,480);
drawingPanel.remove(lt);
drawingPanel.remove(pb);
drawingPanel.add(rec);
}
}
}
class PaintBrush extends Applet {
int xpoint;
int ypoint;
public boolean mouseDown(Event e, int x, int y){
xpoint = x;
ypoint = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y){
Graphics g = getGraphics();
g.drawLine(xpoint, ypoint, x, y);
xpoint = x;
ypoint = y;
return false;
}
}
class LineTool extends Applet {
int xpoint;
int ypoint;
public boolean mouseDown(Event e, int x, int y){
xpoint = x;
ypoint = y;
return true;
}
public boolean mouseUp(Event e, int x, int y){
Graphics g = getGraphics();
g.drawLine(xpoint, ypoint, x, y);
xpoint = x;
ypoint = y;
return false;
}
}
class Rectangle extends Applet {
int xpoint;
int ypoint;
int temp;
public boolean mouseDown(Event e, int x, int y){
xpoint = x;
ypoint = y;
return true;
}
/*
public boolean mouseDrag(Event e, int x, int y){
Graphics g = getGraphics();
x = x - xpoint;
y = y - ypoint;
g.drawRect(xpoint, ypoint, x, y);
this.repaint();
return false;
}
*/
public boolean mouseUp(Event e, int x, int y){
Graphics g = getGraphics();
x = x - xpoint;
y = y - ypoint;
g.drawRect(xpoint, ypoint,
x, y);
return false;
}
}
Any help is MUCH appreciated.
Thanks a lot,
Colm