هذا الكود تم كتابه التعليقات عليه بواسطة طلاب من هندسة شبرا
نسأل الله العظيم ان يعلمنا ما ينفعنا وينفعنا بما علمنا انه ولى ذلك والقادر عليه
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // for actionPerformed(ActionEvent e)
import java.util.*; // why
import java.io.*;
/**
Copyright: Copyright © 2003
*
Company: attel
* @author unascribed
* @version 1423
*/
//********** class shape start here *********//
class Shape implements Serializable
{
private Point dim1,dim2; // why private = to be constant
private Color c;
void setdata(Point dim1,Point dim2,Color c) // our mrthod setdata to store the data whitch i need to draw any shape
{
this.dim1 = dim1; // what is this
this.dim2 = dim2; // what is this
this.c = c; // what is this
}
Point getdim1() // method to get the dim of the first point
{
return dim1; // return the value in the var. dim1
}
Point getdim2() // method to get the dim. of the second point
{
return dim2; // return it in the dim2 var.
}
Color getColor() // also to return the color of drawing
{
return c; // store it in the c var.
}
void calcdiff() // method to calculate the diff. between the two point & store the diff in the second point to eforce that the second point must be the biggest one so dont draw in the reverse direction
{
// to calculate the w&h &draw
dim2.x = dim2.x - dim1.x;
dim2.y = dim2.y - dim1.y;
}
void draw(Graphics g) // abstract method draw
{
}
} // the end of the Sahpe class
//***********************************************************************************
// ****** the recangle class start here *********//
class Rect extends Shape // dont forgot the extends
{
void draw (Graphics g) // overridden
{
g.setColor(getColor()); // call our getcolor method whitch retrun c
g.drawRect(getdim1().x,getdim1().y,getdim2().x,getdim2().y); // call our extends methods
}
} // the end of the rect class
// ****** the Circle class start here *********//
class Circle extends Shape
{
void draw (Graphics g)
{
g.setColor(getColor());
g.drawOval(getdim1().x,getdim1().y,getdim2().x,getdim2().y);
}
}// the end of the circle class
// ****** the Line class start here *********//
class Line extends Shape
{
void draw (Graphics g)
{
g.setColor(getColor());
g.drawLine(getdim1().x,getdim1().y,getdim2().x,getdim2().y);
}
}// the end of the line class
//***********************************************************************************
// ******** Our main Class ****** i wrirte the code here ***** //
public class AlaaPaint extends JFrame implements ActionListener,MouseListener,MouseMotionListener{
Point pold,pnew;
int flag ; // flag to set the diff. shapes
JPanel p1 = new JPanel();
JMenuBar mb = new JMenuBar() ;
JMenu m1 = new JMenu("File");
JMenu m2 = new JMenu("Edit");
JMenu m3 = new JMenu("View");
JMenu m4 = new JMenu("Color");
JMenu m5 = new JMenu("Img");
JMenu m6= new JMenu ("Help");
JMenuItem mi1 = new JMenuItem("New");
JMenuItem mi2 = new JMenuItem("Open");
JMenuItem mi3 = new JMenuItem("Save");
JMenuItem mi4 = new JMenuItem("SaveAs");
JMenuItem mi5 = new JMenuItem("Exit");
JMenuItem mi6 = new JMenuItem("Undo");
JMenuItem mi7 = new JMenuItem("Redo");
JMenuItem mi8 = new JMenuItem("Copy");
JMenuItem mi9 = new JMenuItem("Paste");
JMenuItem mi10 = new JMenuItem("Cut");
JMenuItem mi11 = new JMenuItem("SelectAll");
JMenuItem mi12 = new JMenuItem("Zoom");
JMenuItem mi13 = new JMenuItem("Rotate");
JMenuItem mi14 = new JMenuItem("ClearImage");
JMenuItem mi15 = new JMenuItem("Editcolor");
JMenuItem mi16 = new JMenuItem("AboutPaint");
// note the folleing 2 lines
Shape sp1,sp2; // sp1 to draw $ sp2 to store in vector
Vector v = new Vector(); // to repaint &serlization (srote the shapes)
// contanier to the 3 Buttons
JButton b1 = new JButton("Rectangle");
JButton b2 = new JButton("Circle");
JButton b3 = new JButton("Line");
JButton b4 = new JButton("Exit");
public AlaaPaint()
{
Container c = getContentPane(); // for swing component
p1.setLayout(new GridLayout(4,1)); // to include the 4 Buttons
b1.addActionListener(this); // add the actions to the Buttons
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
c.addMouseListener(this);
c.addMouseMotionListener(this); // why
// why
//add the Buttons to the Panel
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
// Buttons properties
// 1. Background Color
b1.setBackground(Color.blue);
b2.setBackground(Color.blue);
b3.setBackground(Color.blue);
b4.setBackground(Color.blue);
// 2. Foreground Color
b1.setForeground(Color.white);
b2.setForeground(Color.white);
b3.setForeground(Color.white);
b4.setForeground(Color.white);
// Font
/*
b1.setFont(Font.BOLD);
b2.setFont(Font.BOLD);
b3.setFont(Font.BOLD);
b4.setFont(Font.BOLD);
*/
// add the menus to the MenuBar
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
mb.add(m5);
mb.add(m6);
// add the MenuItem to the Menus
// File menu
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m1.addSeparator();
m1.add(mi5);
// Edit menu
m2.add(mi6);
m2.add(mi7);
m2.add(mi8);
m2.add(mi9);
m2.add(mi10);
m2.addSeparator();
m2.add(mi11);
// view menu
m3.add(mi12);
// imge menu
m4.add(mi13);
m4.addSeparator();
m4.add(mi14);
// Color menu
m5.add(mi15);
// help menu
m6.add(mi16);
c.add(p1,"East"); // add the panel to the container
c.add(mb,BorderLayout.NORTH); // adding the MenuBar to the container
setSize(500,500);
setVisible(true);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} // the end of the constructor
public void paint(Graphics g)
{
super.paint(g);
// System.out.print("allah"); //
for(int i=0; i {
sp2=(Shape)v.get(i);
sp2.draw(g);
}
switch(flag)
{
case 1:
sp1 = new Rect();
sp1.setdata(pold,pnew,Color.blue);
sp1.calcdiff();
sp1.draw(g);
//System.out.println(sp1.getdim1());
//System.out.println(sp1.getdim2());
break;
case 2:
sp1 = new Circle();
sp1.setdata(pold,pnew,Color.blue);
sp1.calcdiff();
sp1.draw(g);
System.out.println(sp1.getdim1());
System.out.println(sp1.getdim2());
break;
case 3:
sp1 = new Line();
sp1.setdata(pold,pnew,Color.blue);
// sp1.calcdiff(); // i dont need the caldiff method to draw the line
sp1.draw(g);
break;
}
}// the end of paint method
//
//
//
//
//
//
//
public void actionPerformed(ActionEvent e)
{int x;
String s1 = e.getActionCommand(); // take the text on the Button & put in String
if(e.getSource()==mi1)
{
v.clear();
repaint();
}
if(e.getSource()==mi3)
{
JFileChooser chooser=new JFileChooser("c:");
x=chooser.showSaveDialog(this);
if(x==JFileChooser.APPROVE_OPTION)
{
File f=chooser.getSelectedFile();
try
{
FileOutputStream os=new FileOutputStream(f);
ObjectOutputStream obstream= new ObjectOutputStream(os);
for(int i=0;i {
obstream.writeObject(v.get(i));
}
}
catch(IOException e34)
{ }
}
}
if(e.getSource()==mi2)
{
JFileChooser chooser=new JFileChooser("c://");
x=chooser.showOpenDialog(this);
if(x==JFileChooser.APPROVE_OPTION)
{
File f=chooser.getSelectedFile();
try
{
FileInputStream os=new FileInputStream(f);
ObjectInputStream obstream= new ObjectInputStream(os);
Shape s3;
while((s3=(Shape)obstream.readObject())!=null)
{
v.addElement(s3);
}
repaint();
}
catch(Exception e34)
{ }
}
}
if(s1.equals("Rectangle"))
flag = 1;
else
if(s1.equals("Circle"))
flag = 2;
else
if(s1.equals("Line"))
flag = 3;
else
flag = 4;
} // end of the actionPerformed method
// the diff. method ofthe MouseListener events
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
// by experience to make the drwing in the positive side
if(e.getModifiers()== e.BUTTON1_MASK) // to paint by the left mouse only
{e.translatePoint(4,20);
pold= e.getPoint();
}
}
public void mouseReleased(MouseEvent e)
{
v.addElement(sp1);
}
public void mouseClicked(MouseEvent e)
{
}
// the diff. methods of the MouseMotionListener
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON1_MASK)
{ e.translatePoint(4,20);
pnew = e.getPoint();
repaint();
}
}
public static void main(String[] args) {
/* AlaaPaint alaaPaint1 =*/ new AlaaPaint();
}
private void jbInit() throws Exception {
this.getContentPane().setBackground(new Color(255, 255, 184)); // set the color of the contanier
}
}
package mypaintbrush;
