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

تحريك الاشكال في الجافا

بدأه agent99 في 21 ديسمبر 2011 · 3 رد · 989 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

انا عندي كود لتحريك دائرة و اريد التقليل من سرعتها بحيث ان سرعتها كبيرة و كذلك هناك اجسام لا اريد ان اعيد رسمها ما عدا الدائرة

كلاس الدائرة

 public class Circle {
     public int centerX, centerY, radius;
    public Color color=Color.BLUE;


  public Circle(int x, int y, int r) {
     centerX=x; centerY=y; radius=r;
    }





     public void move(int xAmount, int yAmount)
    {
    centerX = centerX + xAmount;
    centerY = centerY + yAmount;
    }

    public void draw(Graphics g)
    {

    // Translates circle's center to rectangle's origin for drawing.

    g.fillOval(centerX , centerY, radius ,radius);

    }
      public void move()
    {
     int i=0,j=0;
        i++; j++;
    move(i,10);

    }

}

كلاس التحريك

 public class search extends Frame {
int a=400;
int b=a/2;
int c=a/6;

Circle cir=new Circle(150, 150, 20);
public void drawRec(Graphics g)

    {

        Graphics2D g2 = (Graphics2D) g;
	Rectangle r = new Rectangle(125,125,a,b);
g2.draw(r);
g2.draw(new Line2D.Double((a/6.0+125),125,(a/6+125),b+125));
g2.draw(new Line2D.Double((2*a/6.0+125),125,(2*a/6+125),b+125));
g2.draw(new Line2D.Double((3*a/6.0+125),125,(3*a/6+125),b+125));
g2.draw(new Line2D.Double((4*a/6.0+125),125,(4*a/6+125),b+125));
g2.draw(new Line2D.Double((5*a/6.0+125),125,(5*a/6+125),b+125));

g2.draw(new Line2D.Double(125,b/3+125,a+125,b/3+125));
g2.draw(new Line2D.Double(125,2*b/3+125,a+125,2*b/3+125));


}


  public void paint(Graphics g)
    {
     drawRec(g);
     g.setColor(Color.blue);
        cir.draw(g);
    up(1);

    }

public void foward(int amountX)
    {

cir.centerX=(cir.centerX + amountX);


    repaint();

}

public void back(int x)
    {
    cir.centerX=(cir.centerX -x);

repaint();
}

public void up(int y)
    {
cir.centerY= (cir.centerY -y);
repaint();
}

public void down(int y)
    {
cir.centerY= (cir.centerY +y);
repaint();

}
}

ماذا افعل حتى لا اعيد رسم drawRec() في كل مرة اعيد رسم الدائرة ؟؟

ارجو المساعدة

#2

السلام عليكم

لتقليل السرعة استخدم ال Thread

و لرسم الدائرة من جديد ارسم مستطيل شفاف حول ال نافذة ثم ارسم فوقه الدائرة

هذا class يرسم دائرة

انا افضل ان تقوم بلرسم على JPanel افضل من JFrame

هذا الكلاس

import java.awt.Dimension;

import javax.swing.JFrame;


public class Fenetre extends JFrame{

        private Panneau pan = new Panneau();

        public Fenetre(){

                this.setTitle("Animation");
                this.setSize(300, 300);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setLocationRelativeTo(null);
                this.setContentPane(pan);
                this.setVisible(true);

                go();
        }

و هذا code ال Panel

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Panneau extends JPanel {

        private int posX = -50;
        private int posY = -50;

 public void paintComponent(Graphics g){
                //On décide d'une couleur de fond pour notre
rectangle
                g.setColor(Color.white);
                //On dessine celui-ci afin qu'il prenne tout la
surface
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                //On redéfinit une couleur pour notre rond
                g.setColor(Color.red);
                //On le dessine aux coordonnées souhaitées
                g.fillOval(posX, posY, 50, 50);         
        }

        public int getPosX() {
                return posX;
        }

        public void setPosX(int posX) {
                this.posX = posX;
        }

        public int getPosY() {
                return posY;
        }

        public void setPosY(int posY) {
                this.posY = posY;
        }

}

و هذه go

private void go(){

                for(;;)
                {
                        int x = pan.getPosX(), y = pan.getPosY();
                        x++;
                        y++;
                        pan.setPosX(x);
                        pan.setPosY(y);
                        pan.repaint();  
                        try {
                                Thread.sleep(10);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }

                        //Si nos coordonnées arrivent aux bords de
notre composant
                        //On réinitialise
                        if(x == pan.getWidth() || y ==
pan.getHeight())
                        {
 pan.setPosX(-50);
                                pan.setPosY(-50);
                        }

                }

        }

تم تعديل هذه المشاركة بواسطة sahraoui17 في 21 ديسمبر 2011 في 20:43

#3

شكرا جزيلا لك

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