View Single Post
  #2 (permalink)  
Old 10-19-2007, 02:49 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
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; } }
Reply With Quote