Results 1 to 8 of 8
  1. #1
    nadeemshafi9 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default newbie drawing two circles simultaneously

    Hello guys

    I am trying to draw two circles simultaneously i cant seem to make sure im talking about the same jframe. should i be controlling the whole thing from one place ? i mean currently i am just working on a what netbeans created from the calculator tutorial.



    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * NumberAddition.java
     *
     * Created on 21-Mar-2010, 00:20:37
     */
    
    package my.numberaddition;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    
    /**
     *
     * @author Nadeem
     */
    public class NumberAddition extends javax.swing.JFrame {
    
        /** Creates new form NumberAddition */
        public NumberAddition() {
            initComponents();
        }
        public void paint(Graphics g) {
            (new Thread(new HelloRunnable(g, this))).start();  
           
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 566, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 405, Short.MAX_VALUE)
            );
    
            pack();
        }// </editor-fold>
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NumberAddition().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        // End of variables declaration
    
    }
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package my.numberaddition;
    
    /**
     *
     * @author Nadeem
     */
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    
    public class HelloRunnable implements Runnable {
        public Graphics g;
        public JFrame j;
    
        public HelloRunnable(Graphics g, JFrame j){
            this.g = g;
            this.j = j;
        }
        public void run() {
            work(this.g, this.j);
        }
    
        public void work(Graphics g, JFrame j){
            System.out.println("Hello from a thread!");
             // Dynamically calculate size information
            Dimension size = j.getSize();
            // diameter
            int d = Math.min(size.width, size.height);
            int x = (size.width - d)/2;
            int y = (size.height - d)/2;
    
            // draw circle (color already set to foreground)
            g.fillOval(x, y, d, d);
            g.setColor(Color.black);
            g.drawOval(x, y, d, d);
        }
        public static void main(String args[]) {
        }
    
    }
    thnx for any advice

  2. #2
    nadeemshafi9 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    i know that the new thread in the paint method extension halts the program at paint so the painting dosent actually happen like i want it to because i want teh new thread to paint the current jframe.

    how do you exit a thread ? or does it just return ?


    should i take the real oo approach and create a handler then create a jframe inside it then have two threads to which i pass the jframe
    Last edited by nadeemshafi9; 03-21-2010 at 07:14 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    There's so much wrong with your approach that it would be rather pointless to criticize the code method by method. You need to read up on the correct way to do custom painting, and maintain concurrency in Swing. You'll find both those topics in the Swing tutorial:
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    Last edited by DarrylBurke; 03-21-2010 at 11:39 AM.

  4. #4
    nadeemshafi9 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks darryl

  5. #5
    nadeemshafi9 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    iv looked at swing worker and i read through the swing threading section but i cant find a solution to painting two circles at once, i mean in pseudo programming at least it would be helpful if i could get some sort of direction, it seems to me that i have an idea of how but i need confirmation before i waste time.

    thanks

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

    Default

    There's no need for use of SwingWorker or any background thread in your situation. You simply need to draw the circles. Darryl's link will help you get on track.

  7. #7
    joansmithseo is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default drawing two circle simultaniously

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class TimerTest extends JPanel
    {
    public static final double 1 - ROOT_2_OVER_2 = Math.sqrt(2)/2.0;
    public static void main(String[] args)
    {
    TimerTest timerTest = new TimerTest();
    JFrame frame = new JFrame(TimerTest.class.getName());
    frame.setContentPane(timerTest);
    frame.setSize(400,400);
    frame.setDefaultCloseOperation(WindowConstants.EXI T_ON_CLOSE);
    frame.setVisible(true);
    timerTest.start();
    }

    private final Timer timer;
    private final int circleSize = 50;
    private int circle1X = 200;
    private int circle1Y = 200;
    private boolean circle1Foward = true;
    private int circle2X = 100;
    private int circle2Y = 100;
    private boolean circle2Foward = true;
    private Color circle1Color = Color.BLACK;
    private Color circle2Color = Color.BLACK;
    private int line1X1 = 0;
    private int line1Y1 = 0;
    private int line1X2;
    private int line1Y2;
    private int line2X2;
    private int line2Y2;
    public TimerTest()
    {
    timer = new Timer(100, new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    if(circle1Foward)
    {
    if(circle1X > 350)
    circle1Foward = false;
    else
    circle1X += 1;
    }
    else
    {
    if(circle1X < 50)
    circle1Foward = true;
    else
    circle1X -= 1;
    }
    if(circle1X < 150 || circle1X > 250)
    circle1Color = Color.RED;
    else
    circle1Color = Color.BLACK;
    if(circle2Foward)
    {
    if(circle2Y > 350)
    circle2Foward = false;
    else
    circle2Y += 1;
    }
    else
    {
    if(circle2Y < 50)
    circle1Foward = true;
    else
    circle2Y -= 1;
    }
    if(circle2Y < 150 || circle2Y > 250)
    circle2Color = Color.RED;
    else
    circle2Color = Color.BLACK;
    updateLines();
    repaint();
    }
    });
    timer.setInitialDelay(0);
    }
    private void updateLines()
    {
    line1X2 = (int)(circle1X + (ROOT_2_OVER_2 * circle1Width));
    line1Y2 = (int) (circle2Y + (ROOT_2_OVER_2 * circle1Height));
    line2X2 = (int) (circle2X - (ROOT_2_OVER_2 * circle2Width) + circle2Width);
    line2Y2 = (int) (circle2Y + (ROOT_2_OVER_2 * circle2Height));
    }
    protected void paintComponent(Graphics g)
    {
    g.setColor(circle1Color);
    g.drawOval(circle1X,circle1Y,circle1Width,circle1H eight);
    g.setColor(circle2Color);
    g.drawOval(circle2X,circle2Y,circle2Width,circle2H eight);
    g.setColor(Color.BLACK);
    g.drawLine(line1X1,line1Y1,line1X2,line1Y2);
    g.drawLine(line2X1,line2Y1,line2X2,line2Y2);
    }
    public void start()
    {
    timer.start();
    }
    }


    Thanks;

    how to draw
    Last edited by joansmithseo; 01-22-2011 at 07:32 PM.

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

    Default

    Anyone know what to expect from the link at the end of the post immediately above. Is it a malware or ad site? It looks like spam, but I've not had the chutzpah to click it yet...

Similar Threads

  1. Multiple persistance unit simultaneously, issue.
    By afrodom in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 03-06-2010, 05:04 PM
  2. Why my threads don't run simultaneously?
    By Gilvan Justino in forum New To Java
    Replies: 7
    Last Post: 01-16-2010, 01:43 AM
  3. Replies: 1
    Last Post: 12-21-2008, 09:06 AM
  4. Replies: 4
    Last Post: 09-13-2008, 11:07 AM
  5. Circles (Loops)
    By Zebra in forum New To Java
    Replies: 1
    Last Post: 05-29-2008, 06:38 AM

Posting Permissions

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