Results 1 to 3 of 3
  1. #1
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

    Default My program doesnt display anything

    Hi i've got a problem with my code it doesnt display picture like or anything for that matter, it just gives me black window.

    Java Code:
    package igra;
    import java.lang.*;
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.image.*;
    import java.net.*;
    import java.awt.event.*;
    /**
     *
     * @author Bojevnik
     */
    public class GlavnoOkno extends JFrame implements {
        final int SCREENWIDTH =700;
        final int SCREENHEIGHT = 700;
        boolean RESIZABLE=false;
        JPanel panela;
        BufferedImage backBuffer;
        Graphics2D g2d;
        /** Creates a new instance of GlavnoOkno */
        public GlavnoOkno() {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(SCREENWIDTH,SCREENHEIGHT);
            this.setResizable(RESIZABLE);
            panela = new Vsebina();
            this.setLayout(null);
            panela.setBounds(0,0,SCREENWIDTH,SCREENHEIGHT);
            this.add(panela);       
        }
       private class Vsebina extends JPanel implements Runnable,KeyListener,MouseListener {
           Image ozadje;
           Toolkit tk;
           public void update(Graphics g){
               //g2d.drawImage(ozadje,0,0,SCREENWIDTH-1,SCREENHEIGHT-1,this);
               g2d.setColor(Color.RED);
               g2d.drawLine(0,0,500,500);
               paint(g);
           }
           public void paint(Graphics g){
               g.drawImage(backBuffer,0,0,this);
           }
           Vsebina(){
               backBuffer = new BufferedImage(SCREENWIDTH, SCREENHEIGHT,BufferedImage.TYPE_INT_RGB);
               g2d = backBuffer.createGraphics();
               tk = Toolkit.getDefaultToolkit();
               ozadje = tk.getImage(getFURL("img/testOzadje.jpg"));
               this.addMouseListener(this);
               this.addKeyListener(this);
               this.setFocusable(true);
                Thread s = new Thread(this);
                if(s!=null) s.start();
           }
           public void run(){         
               int a=0;
               while(a<10){
                    repaint();
                   a++;
                    try{
                   Thread.sleep(1000);
               }
               catch(Exception ex){
                   System.exit(-1);
               }
                   System.out.println("dela");}
              
           }
           public URL getFURL(String url){
               URL tempURL=null;
               File temp;
               try{    
               temp = new File(url);
               tempURL = temp.toURL();
               if (tempURL==null) throw new Exception("sss");
               }
               catch(Exception ex){
                   System.err.print("NAPAKA pri branju slike");
                   System.exit(-1);
               }
               return tempURL;
           }
           public void keyReleased(KeyEvent ke){}
           public void keyPressed(KeyEvent ke){if (ke.getKeyCode()==27) System.exit(1);}
           public void keyTyped(KeyEvent ke){}
           public void mouseExited(MouseEvent me){}
           public void mouseEntered(MouseEvent me){}
           public void mouseClicked(MouseEvent me){}
           public void mouseReleased(MouseEvent me){}
           public void mousePressed(MouseEvent me){System.exit(0);}
       }   
        
    }
    I HATE SMURFS!!!!!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Components with a "J" prefix are Swing vis–a–vis AWT. In Swing we don't override the update method for graphics; its use is restricted to AWT graphics/drawing.
    The preferred/recommended way to arrange for keyboard input to Swing components is with key binding. Using a KeyListener is okay but can cause focus–related problems.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class GO extends JFrame {
        final int SCREENWIDTH = 700;
        final int SCREENHEIGHT = 700;
        boolean RESIZABLE=false;
    
        public GO() {
            super("click, click");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(SCREENWIDTH,SCREENHEIGHT);
            this.setResizable(RESIZABLE);
            JPanel panela = new Vsebina(SCREENWIDTH, SCREENHEIGHT);
            this.setLayout(null);
            panela.setBounds(0,0,SCREENWIDTH,SCREENHEIGHT);
            this.add(panela);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new GO();
        }
    }
    
    class Vsebina extends JPanel implements Runnable {
        BufferedImage backBuffer;
        BufferedImage ozadje;
        Thread thread;
        boolean running = false;
        int x = 100;
        int y = 100;
        double theta = 0;
        double thetaInc = Math.toRadians(5);
    
        Vsebina(int w, int h){
            backBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = backBuffer.createGraphics();
            g2d.setBackground(Color.pink);
            g2d.clearRect(0,0,w,h);
            g2d.setPaint(Color.red);
            int dia = Math.min(w,h)/3;
            g2d.fillOval(w/2-dia/2, h/2-dia/2, dia, dia);
            g2d.dispose();
            ozadje = loadImage(//"img/testOzadje.jpg");
                               "images/Bird.gif");
            registerKey();
            this.addMouseListener(ml);
        }
    
        protected void paintComponent(Graphics g){
            g.drawImage(backBuffer,0,0,this);
            g.drawImage(ozadje, x, y, this);
        }
    
        public void run() {
            while(running) {
                theta += thetaInc;
                int w = getWidth();
                int h = getHeight();
                double dia = Math.min(w, h)/4.0;
                x = (int)(w/2 + dia*Math.cos(theta)) - ozadje.getWidth()/2;
                y = (int)(h/2 + dia*Math.sin(theta)) - ozadje.getHeight()/2;
                repaint();
                try {
                    Thread.sleep(50);
                } catch(InterruptedException ex) {
                    stop();
                }
            }
            System.out.println("dela");
        }
    
        private BufferedImage loadImage(String path){
            BufferedImage image = null;
            try{    
                File temp = new File(path);
                URL tempURL = temp.toURL();
                if (tempURL==null) throw new Exception("sss");
                image = ImageIO.read(temp);  // or, read(file);
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            }catch(Exception ex){
                System.err.print("NAPAKA pri branju slike");
                System.exit(-1);
            }
            return image;
        }
    
        private void registerKey() {
            int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
            getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
            getActionMap().put("ESCAPE", new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
    
        private MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if(e.getClickCount() == 2) {
                    theta = 0;
                } else {
                    if(running)
                        stop();
                    else
                        start();
                }
            }
        };
    
        private void start() {
            if(!running) {
                running = true;
                thread = new Thread(this);
                thread.setPriority(Thread.NORM_PRIORITY);
                thread.start();
            }
        }
    
        private void stop() {
            running = false;
            if(thread != null)
                thread.interrupt();
            thread = null;
        }
    }

  3. #3
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  3. Display program
    By nhlfan in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 01:00 AM
  4. Swing program to display JVM information
    By satya007 in forum AWT / Swing
    Replies: 3
    Last Post: 11-13-2007, 10:59 AM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •