Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2007, 09:29 PM
Member
 
Join Date: Aug 2007
Posts: 13
Bojevnik is on a distinguished road
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.

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!!!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-19-2007, 01:49 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
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.
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; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-19-2007, 03:50 PM
Member
 
Join Date: Aug 2007
Posts: 13
Bojevnik is on a distinguished road
Thank you.
__________________
I HATE SMURFS!!!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
Display program nhlfan New To Java 2 11-22-2007 01:00 AM
Swing program to display JVM information satya007 AWT / Swing 3 11-13-2007 10:59 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +3. The time now is 01:16 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org