الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

برنامج الرسام -بدائي-

بدأه Mina Fouad في 3 أبريل 2011 · 3 رد · 1,578 مشاهدة · في JavaME
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم,

انا مبتدأ في ال j2me واريد ان انفذ برنامج الرسام والذي يقوم برسم مستطيل او دائرة - حسب اختيار المستخدم - ثم يعود ﻷختيار شكل اخر. ولكن المشكلة انه عند اختيار الشكل الجديد يختفي القديم

هذ محاولتي

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author mina
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //                      
    private Command exitCommand;
    private Form form;
    private StringItem stringItem;
    //                    
    private Command draw;
    private Command clear;
    private List listOfShapes;
    private Command drawRect; // to be deleted
    private Command drawCircle; // to be deleted
    private Command nextToAttr; /* next to select the attributes; */


    /**
 	* The HelloMIDlet constructor.
 	*/
    public HelloMIDlet() {
    }

    //                   	
    //                 	

    //                                       	
    /**
 	* Initilizes the application.
 	* It is called only once when the MIDlet is started. The method is called before the startMIDlet method.
 	*/
    private void initialize() {                                     	
        // write pre-initialize user code here

        // write post-initialize user code here
    }                            
    //                          

    //                                        
    /**
 	* Performs an action assigned to the Mobile Device - MIDlet Started point.
 	*/
    public void startMIDlet() {                                      
        // write pre-action user code here
        switchDisplayable(null, getForm());                                        
        // write post-action user code here
    }                         	
    //                       	

    //                                     	
    /**
 	* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
 	*/
    public void resumeMIDlet() {                                   	
        // write pre-action user code here

        // write post-action user code here
    }                              
    //                            

    //                                              
    /**
 	* Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable.
 	* @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately
 	* @param nextDisplayable the Displayable to be set
 	*/
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {                                            
        // write pre-switch user code here
        Display display = getDisplay();                                           	
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }                                         	
        // write post-switch user code here
    }                               	
    //                             	

    //                                             	
    /**
 	* Called by a system to indicated that a command has been invoked on a particular displayable.
 	* @param command the Command that was invoked
 	* @param displayable the Displayable where the command was invoked
 	*/
    public void commandAction(Command command, Displayable displayable) {                                           	
        // write pre-action user code here
        if (displayable == form) {                                       	
            if (command == exitCommand) {                                     	
                // write pre-action user code here
                exitMIDlet();                                       	
                // write post-action user code here
            }                                                  
        }                                                
        // write post-action user code here
        if (command == draw){
            switchDisplayable(null, getListOfShapes());
        }
        if (command == listOfShapes.SELECT_COMMAND){

      Circle c =new Circle();
                  c.circleX=40;
                  c.circleY=40;
                  c.radius=50;
                  c.rectX=45;
                  c.rectY=46;
                  c.width=30;
                  c.height=30;
c.shape=listOfShapes.getSelectedIndex();
                    switchDisplayable(null, c);
        }
    }                           	
    //                         	

    //                               	
    /**
 	* Returns an initiliazed instance of exitCommand component.
 	* @return the initialized component instance
 	*/
    public Command getExitCommand() {
        if (exitCommand == null) {                             	
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);                               	
            // write post-init user code here
        }                     	
        return exitCommand;
    }
    //                   	
/**
 * Draw Command 
 * @return
 */
    public Command getDrawCommand(){
    if(draw == null){
        draw= new Command("Draw", Command.ITEM, 0);
    }
    return draw;
}
   /**
    * Clear command
    * @return
    */
    public Command getClearCommand(){
        if ( clear == null){
            clear = new Command("Claer Canvas", Command.ITEM, 1);

        }
        return clear;
    }

    public Command getDrawCircleCommand(){
        if(drawCircle==null){
            drawCircle = new Command("draw circle", Command.ITEM, 0);
        }
        return drawCircle;
    }

    public Command getDrawRectCommand(){
        if(drawRect == null){
            drawRect = new Command("draw rect", Command.ITEM, 0);
        }
        return drawRect;
    }
    public Command getNextToAttr(){
        if(nextToAttr==null){
            nextToAttr= new Command("Next", Command.ITEM, 0);
        }
        return nextToAttr;
    }
    //                               	
    /**
 	* Returns an initiliazed instance of form component.
 	* @return the initialized component instance
 	*/
    public Form getForm() {
        if (form == null) {                             	
            // write pre-init user code here
            form = new Form("Welcome", new Item[] { getStringItem() });                                    
            form.addCommand(getExitCommand());
            form.setCommandListener(this);                                  
            form.addCommand(getDrawCommand());
            form.addCommand(getClearCommand());

            form.setCommandListener(this);
        }                     	
        return form;
    }
    //                   	
public List getListOfShapes (){

    if(listOfShapes== null){
        listOfShapes= new List("Select Drawing Command", List.IMPLICIT);
        listOfShapes .append("Rectangle", null);
        listOfShapes.append( "Circle", null);
        listOfShapes.addCommand(getNextToAttr());

        listOfShapes.setCommandListener(this);
    }
    return listOfShapes;
}
//                               	
/**
 * Returns an initiliazed instance of stringItem component.
 * @return the initialized component instance
 */
public StringItem getStringItem() {
    if (stringItem == null) {                             	
            // write pre-init user code here
        stringItem = new StringItem("Hello", "Hello, World!");                               	
            // write post-init user code here
    }                     	
    return stringItem;
}
//                   	

    /**
 	* Returns a display instance.
 	* @return the display instance.
 	*/
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
 	* Exits MIDlet.
 	*/
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
 	* Called when MIDlet is started.
 	* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
 	*/
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
 	* Called when MIDlet is paused.
 	*/
    public void pauseApp() {
        midletPaused = true;
    }

    /**
 	* Called to signal the MIDlet to terminate.
 	* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
 	*/
    public void destroyApp(boolean unconditional) {
    }

class Shape extends Canvas{

        protected void paint(Graphics g) {

        }

}

    class Rect extends Shape{

        /* attributes of the rectaagle */
        int x ,y;  // of the upper left point
        int width, height;


        private void draw(Graphics g){
            g.drawRect(x, y, width, height);

        }

        protected void paint(Graphics g) {

            draw(g);

        }



    }

    class Circle extends Shape implements CommandListener{
        /* attributes of the circle */
        int circleX, circleY ; // of the center
        int radius;
       	int shape ; // an integer indictes the shape is a rect or circle;
int rectX,  rectY;
int height, width;
Command cmd;
boolean isFirstTime ;
public Circle(){
    cmd = new Command("back", Command.ITEM, 0);
    addCommand(cmd);
    isFirstTime= true;
    setCommandListener(this);
    repaint();
}
        private void drawCircle(Graphics g){
            g.drawArc(circleX, circleY, radius, radius, 0, 360);

        }
        private void drawRect (Graphics g){
            g.drawRect(rectX, rectY, width, height);
        }

        protected void paint(Graphics g) {
   	// 	throw new UnsupportedOperationException("Not supported yet.");

    g.setColor(0);

   	if(shape == 1){
            drawCircle(g);
   	}
if(shape == 0)

{
    drawRect(g);
            }



  // repaint();
        }

        public void commandAction(Command c, Displayable d) {
if(c == cmd){
    getDisplay().setCurrent(getListOfShapes());

    //repaint();

}
        }
    }
}

للعلم لقد قمت بنفس المشروع باستخدام ال swing وتمكنت من التغلب ع هذه المشكلة باستخدام Array list تحوي جميع الاشكال وتقوم باعادة رسمها

ولكني اظن ان هناك طريقة اكثر كفاءة ف ال j2me

شكراً مقدماً

تم تعديل هذه المشاركة بواسطة Mina Fouad في 3 أبريل 2011 في 02:02

1

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#2

هذا البرنامج كامل ارجو من الخبراء تقييمه مع العلم انه اول تجاربي مع ال j2me

SimplePaint.zip

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#3

تم تعديل البرنامج واضافة الكثير من ال validations

اما بالنسبة لأجابة عن سؤالي - ان كان هناك طريقة اكثر كفاءة للاحتفاظ بكل الاشكال- فقد وجدت الاجابة وهي باختصار استخدام GameCanvas وليس Canvas والذي يمتاز بالحفاظ ع الاشكال المرسومة بخلاف ال Canvas.

SimplePaint.zip

1

--

Mina Fouad

Computer & Systems Engineering Dpt.

Faculty of Engineering

Alexandria University

#4

يمكنك الرسم على صورة Image حيث ستبقى كل تعديلاتك ثم رسم الصورة على Canvas

Ahmed Rageh

مواضيع مشابهة