Results 1 to 8 of 8
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default blinking line timer confusion

    can Somebody explain why the line is blinking in this code:

    Java Code:
    package game1;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Game1 extends JFrame implements ActionListener{
        private final int WIDTH = 800, HEIGHT = 600;
        private javax.swing.Timer timer = new javax.swing.Timer(10, this);
        public JPanel container;
    
        public Game1() {
            timer.setInitialDelay(1500);
            container = new JPanel();
            this.add(container);
            setVisible(true);
            setSize(WIDTH, HEIGHT);
            setTitle("Game1");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setResizable(false);
            timer.start();
        }
         
         public void actionPerformed(ActionEvent e){
             Graphics g = container.getGraphics();
             g.setColor(Color.red);
             int i = 0;
             if (i  < 5) {
                    g.drawLine(50, 100, 30, 20);
                    repaint();
                 }
             else
                 timer.stop();
         }
        
        public static void main(String[] args) {
            Game1 game = new Game1();
        }
    }
    And why nothing happens when I put this:

    Java Code:
    g.drawLine(50, 100, 30, 20);
                    repaint();
    in a for loop and remove the else?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: blinking line timer confusion

    That code could be improved in several ways. For instance, you should not get a component's Graphics object by calling getGraphics() on it. Also you rarely want to draw directly into a top window's paint method but instead will want to draw inside of the paintComponent method of a JPanel or similar component that derives from JComponent. You'll want to read the drawing with Swing tutorials that are part of the standard Swing tutorials for more.

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: blinking line timer confusion

    Quote Originally Posted by Fubarable View Post
    That code could be improved in several ways. For instance, you should not get a component's Graphics object by calling getGraphics() on it. Also you rarely want to draw directly into a top window's paint method but instead will want to draw inside of the paintComponent method of a JPanel or similar component that derives from JComponent. You'll want to read the drawing with Swing tutorials that are part of the standard Swing tutorials for more.
    Thank You.

    If somebody could give me a good paint component method and how to call it that would be cool. I have tried to use it in several projects and free time applications but I usually end up fudging it in some way. I used insets for one and some other stuff but when I try to add multiple things like timers and shapes and so on I don't exactly know how to create a correct paint component method.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: blinking line timer confusion

    Quote Originally Posted by jwl View Post
    If somebody could give me a good paint component method and how to call it that would be cool. I have tried to use it in several projects and free time applications but I usually end up fudging it in some way. I used insets for one and some other stuff but when I try to add multiple things like timers and shapes and so on I don't exactly know how to create a correct paint component method.
    You will find several good examples of using this method in Swing graphics if you search this forum. Also check out the tutorial on Performing Custom Painting with Swing.

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: blinking line timer confusion

    Java Code:
    package test;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import javax.swing.*;
    
    public class Test extends JPanel implements ActionListener{
        
        public void Timer() {
            Timer time = new Timer(50, this);
            time.start();
        }
        
        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Point2D.Double from = new Point2D.Double(10, 40); 
            Point2D.Double to = new Point2D.Double(30, 40);
            g2d.setColor(Color.MAGENTA);
            Line2D.Double lineSegment = new Line2D.Double(from, to); 
            Point2D.Double from1 = new Point2D.Double(10, 40);
            Point2D.Double to1 = new Point2D.Double(10, 60);
            Line2D.Double lineS1 = new Line2D.Double(from1, to1);
            g2d.draw(lineSegment);
            Point2D.Double from2 = new Point2D.Double(10, 60);
            Point2D.Double to2 = new Point2D.Double(40, 60);
            Line2D.Double lineS2 = new Line2D.Double(from2, to2);
            g2d.draw(lineS2);
            g2d.draw(lineS1);
            Point2D.Double from3 = new Point2D.Double(30, 40);
            Point2D.Double to3 = new Point2D.Double(50, 20);
            Line2D.Double lineS3 = new Line2D.Double(from3, to3);
            g2d.draw(lineS3);
            Point2D.Double from4 = new Point2D.Double(50, 20);
            Point2D.Double to4 = new Point2D.Double(90, 20);
            Line2D.Double lineS4 = new Line2D.Double(from4, to4);
            g2d.draw(lineS4);
            Point2D.Double from5 = new Point2D.Double(90, 20);
            Point2D.Double to5 = new Point2D.Double(110, 40);
            Line2D.Double lineS5 = new Line2D.Double(from5, to5);
            g2d.draw(lineS5);
            Point2D.Double from6 = new Point2D.Double(110, 40);
            Point2D.Double to6 = new Point2D.Double(140, 40);
            Line2D.Double lineS6 = new Line2D.Double(from6, to6);
            g2d.draw(lineS6);
            Point2D.Double from7 = new Point2D.Double(60, 60);
            Point2D.Double to7 = new Point2D.Double(90, 60);
            Line2D.Double lineS7 = new Line2D.Double(from7, to7);
            g2d.draw(lineS7);
            Point2D.Double from8 = new Point2D.Double(110, 60);
            Point2D.Double to8 = new Point2D.Double(140, 60);
            Line2D.Double lineS8 = new Line2D.Double(from8, to8);
            g2d.draw(lineS8);
            Point2D.Double from9 = new Point2D.Double(140, 40);
            Point2D.Double to9 = new Point2D.Double(140, 60);
            Line2D.Double lineS9 = new Line2D.Double(from9, to9);
            g2d.draw(lineS9);
            g2d.setColor(Color.BLACK);
            Ellipse2D.Double ellipse = new Ellipse2D.Double(40, 50, 20, 20);
            g2d.draw(ellipse);
            Ellipse2D.Double ellipse1 = new Ellipse2D.Double(90, 50, 20, 20);
            g2d.draw(ellipse1);
         }    
        
        public void paintComponent(Graphics g) {
           
        }
        
        public void actionPerformed(ActionEvent e){
           
        }
        
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(800, 600);
            frame.setTitle("Title");
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            Test component = new Test();
            frame.add(component);
        }
    }
    Just in case anyone wants to give me some pointers.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: blinking line timer confusion

    You're using the wrong method, paint. Please check my recommendations above.

  7. #7
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: blinking line timer confusion

    Ok, I have fixed the paint method now I want to draw a line under the car that moves up and down so it looks like the car is moving. I have a sleep timer but that just covers the screen until the time is up and then paints the 2d images.

    Java Code:
    package javaapplication40;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class JavaApplication40 {
    
        
        public static void main(String[] args) {
           SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? " +
                    SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("Animation");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(250, 200); //overridden by pack()
            Timer timer = new Timer(1000, new MyPanel()); 
            f.add(new MyPanel());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            timer.start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }
            timer.stop();
        }
    }
    
    class MyPanel extends JPanel implements ActionListener{
        public javax.swing.Timer timer = new javax.swing.Timer(50, this);
    
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.GREEN));
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800,600);
        }
        
        public void actionPerformed(ActionEvent e)
        {
           // I want to draw a line that moves up and down using a swing timer
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);       
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawString("text example",350,300);
            Point2D.Double from = new Point2D.Double(10, 40); 
            Point2D.Double to = new Point2D.Double(30, 40);
            g2d.setColor(Color.MAGENTA);
            Line2D.Double lineSegment = new Line2D.Double(from, to); // created line
            Point2D.Double from1 = new Point2D.Double(10, 40);
            Point2D.Double to1 = new Point2D.Double(10, 60);
            Line2D.Double lineS1 = new Line2D.Double(from1, to1);
            g2d.draw(lineSegment);
            Point2D.Double from2 = new Point2D.Double(10, 60);
            Point2D.Double to2 = new Point2D.Double(40, 60);
            Line2D.Double lineS2 = new Line2D.Double(from2, to2);
            g2d.draw(lineS2);
            g2d.draw(lineS1);
            Point2D.Double from3 = new Point2D.Double(30, 40);
            Point2D.Double to3 = new Point2D.Double(50, 20);
            Line2D.Double lineS3 = new Line2D.Double(from3, to3);
            g2d.draw(lineS3);
            Point2D.Double from4 = new Point2D.Double(50, 20);
            Point2D.Double to4 = new Point2D.Double(90, 20);
            Line2D.Double lineS4 = new Line2D.Double(from4, to4);
            g2d.draw(lineS4);
            Point2D.Double from5 = new Point2D.Double(90, 20);
            Point2D.Double to5 = new Point2D.Double(110, 40);
            Line2D.Double lineS5 = new Line2D.Double(from5, to5);
            g2d.draw(lineS5);
            Point2D.Double from6 = new Point2D.Double(110, 40);
            Point2D.Double to6 = new Point2D.Double(140, 40);
            Line2D.Double lineS6 = new Line2D.Double(from6, to6);
            g2d.draw(lineS6);
            Point2D.Double from7 = new Point2D.Double(60, 60);
            Point2D.Double to7 = new Point2D.Double(90, 60);
            Line2D.Double lineS7 = new Line2D.Double(from7, to7);
            g2d.draw(lineS7);
            Point2D.Double from8 = new Point2D.Double(110, 60);
            Point2D.Double to8 = new Point2D.Double(140, 60);
            Line2D.Double lineS8 = new Line2D.Double(from8, to8);
            g2d.draw(lineS8);
            Point2D.Double from9 = new Point2D.Double(140, 40);
            Point2D.Double to9 = new Point2D.Double(140, 60);
            Line2D.Double lineS9 = new Line2D.Double(from9, to9);
            g2d.draw(lineS9);
            g2d.setColor(Color.BLACK);
            Ellipse2D.Double ellipse = new Ellipse2D.Double(40, 50, 20, 20);
            g2d.draw(ellipse);
            Ellipse2D.Double ellipse1 = new Ellipse2D.Double(90, 50, 20, 20);
            g2d.draw(ellipse1);
        }  
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: blinking line timer confusion

    You're still calling Thread.sleep on the event thread. Again, never do that. I also strongly urge you not to have your GUI components implement the listener interfaces such as ActionListener. As it is in your code above you're creating two MyPanels, one for the listener which can cause big problems. Use an anonymous inner ActionListener instead for your Timer. As for what code to put in the listener's actionPerformed, change the value of some variable that is used in the paintComponent method so that your line changes position, and then call repaint on the drawing GUI.

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Simple Questions- Stop JPanel Blinking
    By MrFish in forum AWT / Swing
    Replies: 7
    Last Post: 04-10-2011, 06:23 PM
  3. Confusion in line
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 10:46 PM
  4. Blinking stars
    By jholtt23 in forum New To Java
    Replies: 0
    Last Post: 02-19-2009, 05:38 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 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
  •