Results 1 to 3 of 3
- 10-18-2007, 08:29 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 13
- Rep Power
- 0
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!!!!!
- 10-19-2007, 12:49 AM #2
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; } }
- 10-19-2007, 02:50 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
Display program
By nhlfan in forum New To JavaReplies: 2Last Post: 11-22-2007, 12:00 AM -
Swing program to display JVM information
By satya007 in forum AWT / SwingReplies: 3Last Post: 11-13-2007, 09:59 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks