import java.awt.*;
import java.lang.Math;
import java.awt.event.*;
import java.awt.Graphics;
import java.applet.Applet;
/*
<APPLET
CODE=draw.class
WIDTH=600
HEIGHT=200 >
</APPLET>
*/
public class draw extends Applet implements ActionListener, MouseListener,
MouseMotionListener {

Button bDraw, bLine, bOval, bRect, bRounded,bSelect1,bSelect2,bHeading;
Point dot[] = new Point[1000];
Point start, end;
int dots = 0;
boolean mouseUp = false;
boolean draw = false;
boolean line = false;
boolean oval = false;
boolean rectangle = false;
boolean rounded = false;
Font font;


public void init()
{

setLayout(null);
font= new Font("Monospaced", Font.BOLD, 24);
bHeading = new Button("MY PAINT APPLICATION");
bHeading.setSize(500,40);
bHeading.setLocation(300,40);
bHeading.setFont(font);
bHeading.setForeground(Color.white.brighter());
bHeading.setBackground(Color.blue.brighter());

setLayout(null);
bLine = new Button("Line");
bLine.setSize(100,30);
bLine.setLocation(30,100);
bLine.setForeground(Color.blue.brighter());
bLine.setFont(new Font("Arial", Font.BOLD, 16));

bOval = new Button("Circle");
bOval.setSize(100,30);
bOval.setLocation(300,100);
bOval.setForeground(Color.green.brighter());
bOval.setFont(new Font("Arial", Font.BOLD, 16));

bRect = new Button("Rectangles");
bRect.setSize(100,30);
bRect.setLocation(500,100);
bRect.setForeground(Color.red.brighter());
bRect.setFont(new Font("Arial", Font.BOLD, 16));

bRounded = new Button("Ellipse");
bRounded .setSize(100,30);
bRounded .setLocation(700,100);
bRounded.setForeground(Color.blue.brighter());
bRounded.setFont(new Font("Arial", Font.BOLD, 16));

bDraw = new Button("Polygon");
bDraw.setSize(100,30);
bDraw.setLocation(900,100);
bDraw.setForeground(Color.yellow.brighter());
bDraw.setFont(new Font("Arial", Font.BOLD, 16));

setLayout(null);
bSelect1 = new Button("SELECT LINE COLOR");
bSelect1.setSize(200,30);
bSelect1.setLocation(220,170);
bSelect1.setForeground(Color.white.brighter());
bSelect1.setBackground(Color.green.brighter());
bSelect1.setFont(new Font("Arial", Font.BOLD, 16));

bSelect2 = new Button("SELECT FILL COLOR");
bSelect2.setSize(200,30);
bSelect2.setLocation(500,170);
bSelect2.setForeground(Color.white.brighter());
bSelect2.setBackground(Color.green.brighter());
bSelect2.setFont(new Font("Arial", Font.BOLD, 16));

add(bLine);
add(bOval);
add(bRect);
add(bRounded);
add(bDraw);
add(bSelect1);
add(bSelect2);
add(bHeading);

bLine.addActionListener(this);
bOval.addActionListener(this);
bRect.addActionListener(this);
bRounded.addActionListener(this);
bDraw.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
mouseUp = false;
start = new Point(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e)
{
if(line){
end = new Point(e.getX(), e.getY());
} else {
end = new Point(Math.max(e.getX(), start.x),
Math.max(e.getY(), start.y));
start = new Point(Math.min(e.getX(), start.x),
Math.min(e.getY(), start.y));
}
mouseUp = true;
repaint();
}
public void mouseDragged(MouseEvent e)
{
if(draw){
dot[dots] = new Point(e.getX(), e.getY());
dots++;
repaint();
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void paint (Graphics g)
{
if (mouseUp) {
int width = end.x - start.x;
int height = end.y - start.y;
if(line){
g.drawLine(start.x, start.y, end.x, end.y);
}
else if(oval){
g.drawOval(start.x, start.y, width, height);
}
else if(rectangle){
g.drawRect(start.x, start.y, width, height);
}
else if(rounded){
g.drawRoundRect(start.x, start.y, width, height, 10, 10);
}
else if(draw){
for(int loop_index = 0; loop_index < dots - 1;
loop_index++){
g.drawLine(dot[loop_index].x, dot[loop_index].y,
dot[loop_index + 1].x, dot[loop_index + 1].y);
}
}
}
}
public void actionPerformed(ActionEvent e)
{
setFlagsFalse();
if(e.getSource() == bDraw)draw = true;
if(e.getSource() == bLine)line = true;
if(e.getSource() == bOval)oval = true;
if(e.getSource() == bRect)rectangle = true;
if(e.getSource() == bRounded)rounded = true;
}

void setFlagsFalse()
{
rounded = false;
line = false;
oval = false;
rectangle = false;
draw = false;
}
}
I WANT A SITUATION WHEREBY,AFTER DRAWING THE SHAPE,I SHOUL BE ABLE TO FILL IT WITH COLOUR,AND I WISH TO USE PANEL
I HOPE I WILL GET HELP AS SOON AS POSSIBLE

Hi Akeem Amure and welcome to Daniweb :)

A couple of things before we start - please don't shout (by using all capitals) and there is no need to ask for help as soon as possible as we always try to get back to you when we can. Also, please use code tags when posting chunks of source code as it makes it easier to read.

Ok, looking at your code there is a few things. The statement

import jav.awt.*;

automatically imports the class Graphics, so there is no need to import it explicitly. Also, you don't need to import anything starting with java.lang as this is the default package that gets imported automatically.

In answer to your question, the Graphics class has a series of methods beginning with fill, for example fillOval, which can be used to paint a shape with a filled colour. In order to outline the shape in a different colour, call the draw method after the fill method with a different colour.

Hope this helps :)
darkagn

Thanks so much for replying.But i think there is more to my question.
1. i want to create panels with it
2.i also need a code that will make me fill any shape i draw with any colour
Iwill be very grateful if am attended to.thank you so much again

Thanks so much for replying.But i think there is more to my question.
1. i want to create panels with it [

So have the class extend JPanel then and create a JFrame for it in the main() method. Not difficult to do at all. You should also put your painting code in paintComponent() for the JPanel, instead of paint().

2.i also need a code that will make me fill any shape i draw with any colour
Iwill be very grateful if am attended to.thank you so much again

Darkagn already told you that you can use the fillXXX() methods to draw a shape that is filled with the current color. You can have a color picker for choosing that color if you wish. If that isn't what you need to do then you need to explain your question further.

hey i run your program in command prompt by typyig javac Draw.java It compiled successfully.Now whats next steps...?? how to run applet?

  1. Akeem Amure write this five years ago, he/she probably isn't here any more
  2. You can use Google to find out how to run an applet
  3. This is not a free coding service. Nobody will "rewrite the whole code" for you, and you are insulting us by asking
  4. Daniweb is a free service to help people help each other and themselves. Do not ask anyone to do anything else for you until you have shown some effort or attempt to find answers yourself.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Paint{

    public static void main(String[] args){
            gui g = new gui();
            g.setVisible(true);

    }

}

 class gui extends JComponent implements ActionListener{
    JButton red, green, blue, clear;
    Image image;
    Graphics2D draw;
    int x, y, prevX, prevY;

    gui(){
            JFrame frame = new JFrame("Paint");
            Container content = frame.getContentPane();
            content.setLayout(new BorderLayout());
            setDoubleBuffered(false);

            JPanel panel = new JPanel();
            content.add(panel, BorderLayout.SOUTH);
            panel.setPreferredSize(new Dimension(32, 68));
            panel.setMinimumSize(new Dimension(32, 68));
            panel.setMaximumSize(new Dimension(32, 68));


            red = new JButton("Red");
            green = new JButton("Green");
            blue = new JButton("Blue");
            clear = new JButton("Clear");

            red.setPreferredSize(new Dimension(50, 16));
            green.setPreferredSize(new Dimension(50,16));
            blue.setPreferredSize(new Dimension(50, 16));

            panel.add(red);
            panel.add(green);
            panel.add(blue);
            panel.add(clear);

            panel.setVisible(true);

            red.addActionListener(this);
            green.addActionListener(this);
            blue.addActionListener(this);
            clear.addActionListener(this);

            frame.setSize(500, 500);

            frame.setVisible(true)            ;


            addMouseListener(new MouseAdapter(){
                    public void mousePressed(MouseEvent e){
                            prevX = e.getX();
                            prevY = e.getY();
                    }

            });

            addMouseMotionListener(new MouseMotionAdapter(){
                    public void mouseDragged(MouseEvent e){
                            x = e.getX();
                            y = e.getY();
                            draw.drawLine(prevX, prevY, x, y);
                            repaint();
                            prevX = x;
                            prevY = y;

                    }


            });

    }


    public void paintComponent(Graphics g){

           if(image==null){
                    image = createImage(getSize().width, getSize().height);

                    draw = (Graphics2D)image.getGraphics();

                    draw.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                    draw.setPaint(Color.white);
                    draw.fillRect(0, 0, getSize().width, getSize().height);
                    draw.setPaint(Color.black);
                    repaint();
           }

            g.drawImage(image, 0, 0, null);

    }


    public void actionPerformed(ActionEvent e) {
        if( e.getSource()==red){
            draw.setPaint(Color.red);
            repaint();
        }
        if( e.getSource()==green){
            draw.setPaint(Color.green);
            repaint();
        }
        if( e.getSource()==blue){
            draw.setPaint(Color.blue);
            repaint();
        }
        if( e.getSource()==clear){
            draw.setPaint(Color.white);
            draw.fillRect(0, 0, getSize().width, getSize().height);
            draw.setPaint(Color.black);
            repaint();
        }


    }


 }

I TRIED THIS CODE BUT THE CODE ISN'T RUNNING. PLEASE HELP ME WITH IT SOMEBODY. IT CONCERNS MY SCHOOL PROJECT GIVEN TO ME TO DO. I AM COUNTING ON YOU GUYS. THANKS

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

ps Also in the Rules:
Do ensure you own the intellectual property rights to everything that you post

This article has been dead for over six months. Start a new discussion instead.