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

مشكلة فى قراءة صورة و التعامل مع ال Timer

بدأه mr.beshoy في 12 يناير 2010 · 25 رد · 1,363 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

سلام للجميع

---------------

فى محاولة بسيطة لإستعمال ال Class timer

كتبت كود بسيط حيث عندما يتم الضغط على Button start - الصورة التى فى المكان X_START and Y_START

تتحرك بمعدل زمنى لمدة 5 ثوانى ثم تقف بشرط عند التحرك فى الاتجاه X تتحرك بمسافة 10

انا كتبت ما توصلت له لكت بيعترض على اسم الملف javax.imageio.IIOException: Can't read input file!

أما المشكلة الثانية مش عارف هل استعمال لل clas Timer صح و لا غلط

الكود

---------

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TimerTest extends JFrame implements ActionListener {

    private JPanel panel;
    private JButton button;
    private TimerMove timerMove;
    private Timer timer;

    public TimerTest() throws IOException {
        panel = new JPanel();
        button = new JButton("Start");
        panel.add(button);
        getContentPane().add(panel);
        timerMove = new TimerMove();
        timer = new Timer();
        button.addActionListener(this);
    }

    public static void main(String[] args) throws IOException {
        TimerTest timerTest = new TimerTest();
        timerTest.setDefaultCloseOperation(TimerTest.EXIT_ON_CLOSE);
        timerTest.setSize(new Dimension(600, 300));
        timerTest.setLocationRelativeTo(null);
        timerTest.setVisible(true);
        timerTest.setExtendedState(MAXIMIZED_BOTH);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            timer.scheduleAtFixedRate(null, 2, 6);
            timerMove.moveImage(timer);
        }
    }
}

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

class TimerMove extends JPanel {

    private int X_START = 10, Y_START = 10;
    private File file;
    private Image image;
    private Timer timer;

    public TimerMove() throws IOException {
        file = new File("stick.gif");
        image = ImageIO.read(file);
        timer = new Timer();
        moveImage(timer);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, X_START + 10, Y_START + 10, this);
    }

    public void moveImage(Timer timer) {
        this.timer = timer;
        repaint();
    }
}

الكود مرفق و مع الصورة المسببة للمشكلة

شكراً

TimerTest.rar

#2

أولا : بالنسبة لمشكلة عدم قراءة الملف

استخدم الاسلوب دا

new Fie(getClass().getResource("stick.gif").getFile()) ;

للحصول علي المسار الكامل للملف في أي نظام تشغيل

وبكدا مشكلة قراءة الملف هتتحل

#######################################

ثانيا بالنسبة لعدم ظهور الصورة

اولا انت لم تقم باضافة البانل الخاص برسم الصورة ،، يعني انت عملت

TimerMove timermove = new TimerMove() ;

ولم تقم باضافتها للفريم التي ستظهر عليها ،، ولكن دي مش المشكلة بردو ، هناك مشكلة اخري

وهي عدم تحديد التاسك التي من المفروض تنفيذها

أقصد هذا

timer.scheduleAtFixedRate(null, 2, 6);

لماذا null ؟؟

يجب تحديد التاسك ،، من الكلاس TimerTask

والا ستحصل علي NullPointerException!!

########################################

نصيحه حاول استخدام

Timer الخاص بالـ swing

بدلا من الـ Util Timer

بالتوفيق :)

صــفــر

#3

شكراً على ردك

----------------------

نفذت الآتى :

 public TimerTest() throws IOException {
        panel = new JPanel();
        timerMove = new TimerMove();
        button = new JButton("Start");
        panel.add(button);
        panel.add(timerMove);
        getContentPane().add(panel);


    }

    public TimerMove() throws IOException {
        file = new File(getClass().getResource("stick.gif").getFile());
        image = ImageIO.read(file);

    }

و مع ذلك يعطينى IOEXception

------------------------------------------------

بالنسبة لتحديد ال task حقيقة لا أعرف كيق يتم ذلك

شكراً

تم تعديل هذه المشاركة بواسطة mr.beshoy في 12 يناير 2010 في 05:41

#4

جرب هذا التعديل يا بيشوي ..

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            timer.start();
        }
        else
        if( e.getSource() == timer ){
            timer.scheduleAtFixedRate(null, 2, 6);
            timerMove.moveImage(timer);
        }
    }

طبعا تحتاج للتالي :

timer.addActionLisntener(this);

تم تعديل هذه المشاركة بواسطة MKSoft في 12 يناير 2010 في 11:56

#5

أخ محمد شكرا ً لردك

لكن المشكلة تكمن فى أنه بيعطينى exception

Exception in thread "main" javax.imageio.IIOException: Can't read input file!

و هنا الملف المرفق بعد إرشاد cIs.Falcon

----------------------------------

المشكلة الان فى إيقاف ال exception حتى تظهر الصورة ثم أتطرق لموضوع ال timer

------------------------------

TimerTest.rar

#6

الصورة من نوع bmp وليس gif. لماذا تغير الextensions الخاص بالصور!!!؟؟؟

#7

أولا : هى فى الاول خالص كانت bmp لكن لما عملت(IOEXCEPTION>> can not read file) غيرتها ل gif

لكن ايه المشكلة فى هذا

؟؟

تم تعديل هذه المشاركة بواسطة mr.beshoy في 12 يناير 2010 في 15:07

#8

رجعتها مرة أخرى ل bmp و نفس الخطأ

شكراً

#9

مممممممم هي الصورة ما بتظهرش ،، لكن مش بيعطي اكسيبنش خاص بقراءة الملف معايا

المشكلة دلوقتي ان انت ضيفت الـ timermove panel في الـ panel1

ضيف الـ timermove panel في الـ Frame

يعني اعمل كدا

add(timermove, BorderLayout.CENTER) ;

,والـ panel 1

add(panel1, BorderLayout.NORTH);

عشان بس تفصل ما بين البانل اللي هترسم فيها والبانل اللي فيها الزرار

############################################

أما عن مشكلة قراءة المصورة ،، ضع السطر الخاص بقراءة الملف بين

try / catch block

وفي الـ catch اكتب

ex.printStackTrace();

عشان تشوف الاكسيبشن بالتحديد

صــفــر

#10

شكراً لتعبك معايا

------------------

اولاً انا كنت عامل كده

public TimerMove() throws IOException {        file = new File(getClass().getResource("stick.gif").getFile());       
 image = ImageIO.read(file);         }

هى نفسها لو حذفت throws IOException

و كتبت ال try..catch

طبقت ما قولته و مع ذلك يعطينى exception بيقول can not read file

انا مش عارف العيب فين بصراحة فى الصورة - قد تكون تالفة - و لا فى الكود عندى

ولو الصورة تالفة ياريت لو حد معاه نفس الصورة يرفقها هنا لان بصراحة أن مصمم الصورة على برنامج الرسام لانى لم أجدها (( انا عاوز الصور الخاصة بلعبة tetries لو حد عرف يجيبها يكون مشكور جداً )) ممكن تشوفوها من هنا

http://www.freetetris.org/

شكراً جزيلاً

#11

طيب جرب أي صورة تانيه كدا ،، وشوف هل بردو هيبقي فيه مشكلة ولا لا

ولو لا ،، يبقي الصورة تالفه زي ما انت بتقول

صــفــر

#12

جربت صورة تانى و نفس الخطأ

ياريت لو حد يعرف الخطأ فين يقول

شكراً

#13
/*
copy the image to the root directory of the project, don't put it inside the
source folder.

TimerTask can't be null.

change this:
timer.scheduleAtFixedRate(null, 2, 6);
to
timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            // Task here ...
        }, 2, 6);

eventhough i don't know why you have 2 and 6 coz these numbers are in 
milliseconds. 1000 ms = 1 second
*/
#14

تقصد يعنى مضعش الصورة فى ال src و أضعها فى ال folder الاٍساسى

عملت كده و مع ذلك نفسى الخطاً

أنت اشتغلت معاك

شكرا

تم تعديل هذه المشاركة بواسطة mr.beshoy في 12 يناير 2010 في 23:37

#15

لأول مرة الصورة لا تظهر و تدول المشكلة معى بسببها ...

كل هذا ولم أدخل فى الى المشكةل الأساسية و هى التعامل مع ال timer

جربت أكثر من طريقة لتفادى ال IOexception و لكن دون جدوى

#16

جربت تنفيذ المشروع ولم أحصل على IOException

#17

مش ممكن ...ده انا معذبنى هذه الخطاً

و الصورة ظهرت عندك ؟؟؟

ياريت لو ظهرت ترفق الكود معلش

شكرا

يا عصام

#18

لأا ممكن اومال انا بقول ايه من امبارح

البروجيكت شغال معايا من غير اكسيبشن خاص بقراءة الملف !!

طيب في حل جربه كدا ،، حاول اخذ المسار الكامل للصورة من الويندوز وضعه في الـكونستركتور الخاص بالكلاس File

وجرب ..

صــفــر

#19

مش عارف ايه السبب بصراحة

مش اول مرة اقرأ صورة

ال IOexception على السطر هذا

   image = ImageIO.read(file);

--------------------------------------

لو تسمح أرفق الكود الى شغال معاك اشوفه

شكرا

تم تعديل هذه المشاركة بواسطة mr.beshoy في 13 يناير 2010 في 01:15

#20

يا إخوة جربت استعمال ImageIcon بدل ال Image

طبعاً فى الحالة دى لا يظهر exception خاص بقراءة الصورة

و مع ذلك الصورة لا تظهر

الكود

------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TimerTest extends JFrame {

    private JPanel panel;
    private JButton button;
    private TimerMove timerMove;

    public TimerTest() {
        panel = new JPanel(new BorderLayout());
        timerMove = new TimerMove();
        timerMove.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        button = new JButton("Start");
        panel.add(button, BorderLayout.NORTH);
        panel.add(timerMove,BorderLayout.SOUTH);
        getContentPane().add(panel);
    }

    public static void main(String[] args) throws IOException {
        TimerTest timerTest = new TimerTest();
        timerTest.setDefaultCloseOperation(TimerTest.EXIT_ON_CLOSE);
        timerTest.setSize(new Dimension(600, 300));
        timerTest.setLocationRelativeTo(null);
        timerTest.setVisible(true);
        timerTest.setExtendedState(MAXIMIZED_BOTH);
    }
}

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

class TimerMove extends JPanel {

    private int X_START = 10, Y_START = 200;
    private ImageIcon imageIcon;

    public TimerMove() {
        imageIcon = new ImageIcon(getClass().getResource("stick.bmp"));

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(imageIcon.getImage(), X_START, Y_START, this);
    }

}

شكراً

الكود مرفق حتى تستطيعون استعمال الصورة

TimerTest.rar

تم تعديل هذه المشاركة بواسطة mr.beshoy في 13 يناير 2010 في 12:06

#21

كل المشكله انك مسوي البانل اللي بتطبع عليه في الNorth أضبطه على Center وبتنضبط أمورك

هذا تعديل بسيط


/**
*
* @author beshoy
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TimerTest extends JFrame {

private JPanel panel;
private JButton button;
private TimerMove timerMove;

public TimerTest() {
panel = new JPanel(new BorderLayout());
timerMove = new TimerMove();
timerMove.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
button = new JButton("Start");
panel.add(button, BorderLayout.NORTH);
panel.add(timerMove,BorderLayout.CENTER);

getContentPane().add(panel);
setDefaultCloseOperation(TimerTest.EXIT_ON_CLOSE);
setSize(new Dimension(600, 300));
setLocationRelativeTo(null);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
}

public static void main(String[] args) throws IOException {
TimerTest timerTest = new TimerTest();

}
}

تقبل مروري


HeartOutputStream myHeart;
OutputStream myHeartOutStream;

myHeartOutStream = MAllaw.getHeart().getOutputStream();
myHeart = new HeartOutputStream(myHeartOutStream);

while(myHeart.isStillBeating())
myHeart.write("I ♥ Java");

#22

طبقت كما قلت و المشكلة قائمة

لجئت لتلك الطريقة

و ايضا لم تظهر

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestTimer extends JFrame {

    private JPanel panel;
    private TimerMove timerMove;


    public TestTimer() {
        panel = new JPanel(null);
        panel.setBounds(10, 100, 200, 200);
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        timerMove = new TimerMove();
        timerMove.setBounds(10, 100, 500, 500);
        timerMove.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        panel.add(timerMove);
        getContentPane().add(panel);
    }

    public static void main(String[] args) {
        TestTimer testTimer = new TestTimer();
        testTimer.setDefaultCloseOperation(TestTimer.EXIT_ON_CLOSE);
        testTimer.setSize(new Dimension(600, 300));
        testTimer.setLocationRelativeTo(null);
        testTimer.setVisible(true);
        testTimer.setExtendedState(MAXIMIZED_BOTH);
    }
}

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

class TimerMove extends JPanel {

    private ImageIcon imageIcon;
    private int x = 20, y = 100;

    public TimerMove() {
        imageIcon = new ImageIcon(getClass().getResource("stick.jpg"));
    }

    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        g.drawImage(imageIcon.getImage(), x, y, this);
    }
}

أتنمى تساعدونى فى المشكلة هذه

----------

الكود المرفق

TesTimer.rar

#23
public void paintComponent(Graphics g) {
 	super.paintComponents(g);
 	g.drawImage(imageIcon.getImage(), x, y, this);
}

أزل ال"s"

تم تعديل هذه المشاركة بواسطة herch في 13 يناير 2010 في 16:20

#24

تمام اشتغلت و ظهرت الصورة و ده معناه ان الصورة شغالة و سليمة .. (( لم الأحظ الS )) خطا عفوا منى

لكن المشكلة الكبرى عندما استبدال استعمال ال ImageIcon ب Image

فى ال Image يتم فعل مثل هذا :

---------------------------------

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

class TimerMove extends JPanel {

    //   private ImageIcon imageIcon;
    private Image image;
    private int x = 20, y = 100;
    private File file;

    public TimerMove(){

        file = new File(getClass().getResource("stick.jpg").getFile());
        try {
            image = ImageIO.read(file);
            //     imageIcon = new ImageIcon(getClass().getResource("stick.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(TimerMove.class.getName()).log(Level.SEVERE, null, ex);
        }
        //     imageIcon = new ImageIcon(getClass().getResource("stick.jpg"));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawImage(image, x, y, this);
    }
}

اعتقد الطريقة سليمة 100 فى المية

ليش بيعترض و يعطينى IOexception (( اثبتنا فى المكود السابق ان الصورة سليمة و موضوعة فى المسار السليم ))

شكرا

#25

العجيب انه اشتغل معي 100% وماطلعلي اي مشاكل بعدما عدلت اللي قلتلك عليه !


HeartOutputStream myHeart;
OutputStream myHeartOutStream;

myHeartOutStream = MAllaw.getHeart().getOutputStream();
myHeart = new HeartOutputStream(myHeartOutStream);

while(myHeart.isStillBeating())
myHeart.write("I ♥ Java");

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