Results 1 to 2 of 2

Thread: Program is blinking

  1. #1
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default Program is blinking

    Could someone make this work? I am guessing it is a double buffering issue, over swamped with programs from the kids trying to out source a little lol

    Do it for meeeeee

    Code:
    package yesdg;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    //!! import squarequest.sprites.*;
    import java.awt.event.*;
    import java.util.EnumMap;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    
    
    
    
    public class metoo extends JPanel implements ActionListener{ 
    	
    	
    
        private static final int ANIMATION_DELAY = 15;
       private final int HEIGHT = 400;
       private final int WIDTH = 600;
       private Square square;
       private EnumMap<Direction, Boolean> dirMap = new EnumMap<>(Direction.class);
       private Map<Integer, Direction> keyToDir = new HashMap<>();
       // !! private Circle circle;
       private Timer animationTimer;
       
       
    
      
       
       
       
       private static final int BALL_SIZE2 = 25, SPEED1 = 15;
       
       int width = getWidth();
    
       int height = getHeight();
       
       private int xPos1, yPos1;
       
       private int xSpeed1, ySpeed1;
    
       public metoo() {
    	   
    	   
    	   
          for (Direction dir : Direction.values()) {
             dirMap.put(dir, Boolean.FALSE);
          }
          keyToDir.put(KeyEvent.VK_W, Direction.UP);
          keyToDir.put(KeyEvent.VK_S, Direction.DOWN);
          keyToDir.put(KeyEvent.VK_A, Direction.LEFT);
          keyToDir.put(KeyEvent.VK_D, Direction.RIGHT);
          
          
          
          if(keyboard_check(KeyEvent.VK_A) && xPos1-width>=0){ // if left is pressed AND x-xvel (or a number, how ever far you want it to go) isn't outside the room
        		 xPos1-=width; // move to the new spot
        	}
        	if(keyboard_check(KeyEvent.VK_D) && xPos1+width>=(600)){ // if right is pressed AND x+xvel (or a number, how ever far you want it to go) isn't outside the room
        		  xPos1+=width; // move to the new spot
        	 }
        	if(keyboard_check(KeyEvent.VK_W) && yPos1-height>=0){ // if up is pressed AND y-yvel (or a number, how ever far you want it to go) isn't outside the room
        		  yPos1-=height; // move to the new spot
        	 }
        	 if(keyboard_check(KeyEvent.VK_S) && yPos1+height>=(400)){ // if down is pressed AND y+yvel (or a number, how ever far you want it to go) isn't outside the room
        		   yPos1+=height; // move to the new spot
        	  }
          
          
          
          // !! addKeyListener(new DirectionListener());
          setKeyBindings();
          setBackground(Color.white);
          setPreferredSize(new Dimension(WIDTH, HEIGHT));
          setFocusable(true);
          square = new Square();
          animationTimer = new Timer(ANIMATION_DELAY, new AnimationListener());
          animationTimer.start();
       }
          
          
    
       private boolean keyboard_check(int vkA) {
    	// TODO Auto-generated method stub
    	return false;
    }
    
       class OptimizedDoubleBufferedCanvas extends Canvas {
    	    
    	    public void update(Graphics g) {
    		Graphics offgc;
    		Image offscreen = null;
    		Rectangle box = g.getClipRect();
    
    		// create the offscreen buffer and associated Graphics
    		offscreen = createImage(box.width, box.height);
    		offgc = offscreen.getGraphics();
    		// clear the exposed area
    		offgc.setColor(getBackground());
    		offgc.fillRect(0, 0, box.width, box.height);
    		offgc.setColor(getForeground());
    		// do normal redraw
    		offgc.translate(-box.x, -box.y);
    		paint(offgc);
    		// transfer offscreen to window
    		g.drawImage(offscreen, box.x, box.y, this);
    	    }
    	}
    
    
    private void setKeyBindings() {
          int condition = WHEN_IN_FOCUSED_WINDOW;
          final InputMap inputMap = getInputMap(condition);
          final ActionMap actionMap = getActionMap();
          boolean[] keyPressed = { true, false };
          for (Integer keyCode : keyToDir.keySet()) {
             Direction dir = keyToDir.get(keyCode);
             for (boolean onKeyPress : keyPressed) {
                boolean onKeyRelease = !onKeyPress; // to make it clear how bindings work
                KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0,
                      onKeyRelease);
                Object key = keyStroke.toString();
                inputMap.put(keyStroke, key);
                actionMap.put(key, new KeyBindingsAction(dir, onKeyPress));
             }
          }
       }
    
       public void paintComponent(Graphics g) {
          //super.paintComponent(g);
          square.display(g);
       }
    
    
    
       private class AnimationListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent evt) {
             boolean repaint = false;
             for (Direction dir : Direction.values()) {
                if (dirMap.get(dir)) {
                   square.move(dir);
                   repaint = true;
                }
             }
             if (repaint) {
                repaint();
             }
          }
       }
    
       private class KeyBindingsAction extends AbstractAction {
          /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private Direction dir;
          boolean pressed;
    
          public KeyBindingsAction(Direction dir, boolean pressed) {
             this.dir = dir;
             this.pressed = pressed;
          }
    
          @Override
          public void actionPerformed(ActionEvent evt) {
             dirMap.put(dir, pressed);
          }
       }
    
       private static void createAndShowGUI() {
          metoo gamePanel = new metoo();
          JFrame frame = new JFrame("GamePanel");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(gamePanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
          gamePanel.requestFocusInWindow();
          
          
          frame.add(new IndeProj());
          
          class IndeProj extends JPanel implements ActionListener{
        	  
        	  private static final int ANIMATION_DELAY = 15;
        	   private final int HEIGHT = 400;
        	   private final int WIDTH = 600;
        	   private Square square;
        	   private EnumMap<Direction, Boolean> dirMap = new EnumMap<>(Direction.class);
        	   private Map<Integer, Direction> keyToDir = new HashMap<>();
        	   // !! private Circle circle;
        	   private Timer animationTimer;
        	   private int xPos1, yPos1;
        	   private int xSpeed1, ySpeed1;
        	   private static final int BALL_SIZE2 = 25, SPEED1 = 15;
        	   int width = getWidth();
        	   int height = getHeight();
          	private static final int BALL_SIZE = 25, SPEED = 4;
          	
           //public void IndeProj(int xPos1, int yPos1, int xSpeed1, int ySpeed1){
           
        	   IndeProj() {
        	   
        	   for (int i=0; i<10; i++){
        		   
    
        	    	Random r = new Random();
        	        
        	    	xPos1 = r.nextInt(300);
    
        	        yPos1 = r.nextInt(300);
    
        	        // determine speed direction
    
        	        xSpeed1 = SPEED;
    
        	        ySpeed1 = -SPEED;
    
        	        // a timer called every 1/10 second
    
        	        Timer timer = new Timer(100, this);
    
        	        timer.start();
        }
           }
    
    
        @Override
        public void actionPerformed(ActionEvent e) {
        	// TODO Auto-generated method stub
        	 // get screen size
    
            int width = getWidth();
    
            int height = getHeight();
    
            // update positions
    
            xPos1 += xSpeed1;
    
            yPos1 += ySpeed1;
    
            // test if we go out the screen on the X axis
    
            if(xPos1 < 0) {
    
                xPos1 = 0;
    
                xSpeed1 = SPEED;
    
            }
    
            else if(xPos1 > width - BALL_SIZE) {
    
                xPos1 = width - BALL_SIZE;
    
                xSpeed1 = -SPEED;
    
            }
    
            // test if we go out the screen on the Y axis
    
            if(yPos1 < 0) {
    
                yPos1 = 0;
    
                ySpeed1 = SPEED;
    
            }
    
            else if(yPos1 > height - BALL_SIZE) {
    
                yPos1 = height - BALL_SIZE;
    
                ySpeed1 = -SPEED;
    
            }
    
            // ask to redraw the ball
    
            repaint();
    
        }
    
    
        	  public void paintComponent(Graphics g) {
    
        	        super.paintComponent(g);
    
        	        g.fillOval(xPos1, yPos1, BALL_SIZE, BALL_SIZE);
    
        	    }
    
        	 
    
        	    /*public void main(String[] args) {
    
        	        // a JFrame to test the whole thing
    
        	        JFrame frame = new JFrame("Bouncing ball");
    
        	        frame.setSize(500, 500);
    
        	        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
        	        // add the bouncing panel to the JFrame
    
        	        frame.add(new IndeProj());
    
        	        frame.setVisible(true);
        	    }*/
    
    
    
    
        public int getWidth() {
        	// TODO Auto-generated method stub
        	return 0;
        	
        }
    
        public int getHeight() {
        	// TODO Auto-generated method stub
        	return 0;
        }
        }
        }
    
    
       
    
       public static void main(String[] args) {
    	   
    	// a JFrame to test the whole thing
    
           //JFrame frame = new JFrame("Bouncing ball");
    
           //frame.setSize(500, 500);
    
           //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
           // add the bouncing panel to the JFrame
    
           //frame.add(new IndeProj());
           //Component componentObtainingGraphicsFrom = null;
    	//frame.add(componentObtainingGraphicsFrom, new Square());
           //frame.setVisible(true);
    	   
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGUI();
             }
          });
       }
    
    
    
    @Override
    public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	
    }
    }
    
    enum Direction {
       UP(0, -9), DOWN(0, 9), LEFT(-9, 0), RIGHT(9, 0);
       private int incrX;
       private int incrY;
    
       private Direction(int incrX, int incrY) {
          this.incrX = incrX;
          this.incrY = incrY;
       }
    
       public int getIncrX() {
          return incrX;
       }
    
       public int getIncrY() {
          return incrY;
       }
    }
    
    
    
    class Square {
       private int x = 300;
       private int y = 200;
       private int w = 45;
       private int h = w;
       private int step = 1;
       private Color color = Color.red;
       private Color fillColor = new Color(255, 150, 150);
       private Stroke stroke = new BasicStroke(3f, BasicStroke.CAP_ROUND,
             BasicStroke.JOIN_ROUND);
    
       public void display(Graphics g) {
          Graphics2D g2d = (Graphics2D) g.create();
          g2d.setColor(fillColor);
          g2d.fillRect(x, y, w, h);
          g2d.setStroke(stroke);
          g2d.setColor(color);
          g2d.drawRect(x, y, w, h);
          g2d.dispose();
       }
    
       public void setStep(int step) {
          this.step = step;
       }
    
       public void move(Direction dir) {
          x += step * dir.getIncrX();
          y += step * dir.getIncrY();
          
          
          
          
       }
       
       //////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////
       /////////////////////////////////////////////////////////////////////////////////
    	///////////////////////////////////////////////////////////////////////////////
       
       
       
       
       
    	  public class IndeProj extends JPanel implements ActionListener{
    	  
    	  private static final int ANIMATION_DELAY = 15;
    	   private final int HEIGHT = 400;
    	   private final int WIDTH = 600;
    	   private Square square;
    	   private EnumMap<Direction, Boolean> dirMap = new EnumMap<>(Direction.class);
    	   private Map<Integer, Direction> keyToDir = new HashMap<>();
    	   // !! private Circle circle;
    	   private Timer animationTimer;
    	   private int xPos1, yPos1;
    	   private int xSpeed1, ySpeed1;
    	   private static final int BALL_SIZE2 = 25, SPEED1 = 15;
    	   int width = getWidth();
    	   int height = getHeight();
      	private static final int BALL_SIZE = 25, SPEED = 4;
      	
       //public void IndeProj(int xPos1, int yPos1, int xSpeed1, int ySpeed1){
       
    	   IndeProj() {
    	   
    	   for (int i=0; i<10; i++){
    		   
    
    	    	Random r = new Random();
    	        
    	    	xPos1 = r.nextInt(300);
    
    	        yPos1 = r.nextInt(300);
    
    	        // determine speed direction
    
    	        xSpeed1 = SPEED;
    
    	        ySpeed1 = -SPEED;
    
    	        // a timer called every 1/10 second
    
    	        Timer timer = new Timer(100, this);
    
    	        timer.start();
    }
       }
    
    
    @Override
    public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
    	 // get screen size
    
        int width = getWidth();
    
        int height = getHeight();
    
        // update positions
    
        xPos1 += xSpeed1;
    
        yPos1 += ySpeed1;
    
        // test if we go out the screen on the X axis
    
        if(xPos1 < 0) {
    
            xPos1 = 0;
    
            xSpeed1 = SPEED;
    
        }
    
        else if(xPos1 > width - BALL_SIZE) {
    
            xPos1 = width - BALL_SIZE;
    
            xSpeed1 = -SPEED;
    
        }
    
        // test if we go out the screen on the Y axis
    
        if(yPos1 < 0) {
    
            yPos1 = 0;
    
            ySpeed1 = SPEED;
    
        }
    
        else if(yPos1 > height - BALL_SIZE) {
    
            yPos1 = height - BALL_SIZE;
    
            ySpeed1 = -SPEED;
    
        }
    
        // ask to redraw the ball
    
        repaint();
    
    }
    
    
    	  public void paintComponent(Graphics g) {
    
    	        super.paintComponent(g);
    
    	        g.fillOval(xPos1, yPos1, BALL_SIZE, BALL_SIZE);
    
    	    }
    
    	 
    
    	    /*public void main(String[] args) {
    
    	        // a JFrame to test the whole thing
    
    	        JFrame frame = new JFrame("Bouncing ball");
    
    	        frame.setSize(500, 500);
    
    	        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    	        // add the bouncing panel to the JFrame
    
    	        frame.add(new IndeProj());
    
    	        frame.setVisible(true);
    	    }*/
    
    
    
    
    public int getWidth() {
    	// TODO Auto-generated method stub
    	return 0;
    	
    }
    
    public int getHeight() {
    	// TODO Auto-generated method stub
    	return 0;
    }
    }
    }




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  2. #2
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Give us a stacktrace if you have

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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