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

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

Well I have a probable solution. Instead of using different applets in the drawing panel and then removing and adding one you can combine all of them in one applet(or you can paint in the parent applet only) and use flags to check and paint accordingly. This is the code

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();
    boolean brushflag=false,lineflag=false,rectangleflag=false;
	
	Rectangle rec = new Rectangle();
    int xpoint;
	int ypoint;

  	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")){

  			brushflag=true;
            lineflag=false;
            rectangleflag=false;

  		} else if (e.getActionCommand().equals("Line Tool")){

  			brushflag=false;
            lineflag=true;
            rectangleflag=false;

  		} else if (e.getActionCommand().equals("Rectangle Tool")){

  			brushflag=false;
            lineflag=false;
            rectangleflag=true;

  		}
  	}
    public boolean mouseDown(Event e, int x, int y){
        xpoint = x;
	    ypoint = y;
        return true;
    }
    public boolean mouseDrag(Event e, int x, int y){
	    if(brushflag)
        {
            
            Graphics g = drawingPanel.getGraphics();
            g.drawLine(xpoint, ypoint, x, y);
            xpoint = x;
            ypoint = y;
        }
		return false;
	  }
    public boolean mouseUp(Event e, int x, int y){
        if(lineflag)
        {
            Graphics g = drawingPanel.getGraphics();
            g.drawLine(xpoint, ypoint, x, y);
            xpoint = x;
            ypoint = y;
        }
        else if(rectangleflag)
        {
            Graphics g = drawingPanel.getGraphics();

            x = x - xpoint;
            y = y - ypoint;

            g.drawRect(xpoint, ypoint,
                    x, y);
        }
		return false;
	  }
}

One thing more. While drawing rectangle you are considering height and width as cuurent x-previous x and current y-previous y. The problem is that this quantity can be -ve due to which you are getting filled rectangles when these quantities are negative. I think so you can modify your algo accordingly for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.