Results 1 to 8 of 8
- 03-21-2010, 07:04 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
thnx for any adviceJava 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[]) { } }
- 03-21-2010, 07:10 AM #2
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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 jframeLast edited by nadeemshafi9; 03-21-2010 at 07:14 AM.
- 03-21-2010, 11:30 AM #3
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)
dbLast edited by DarrylBurke; 03-21-2010 at 11:39 AM.
- 03-22-2010, 03:48 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
thanks darryl
- 03-22-2010, 05:11 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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
-
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.
- 01-09-2011, 10:36 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
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 drawLast edited by joansmithseo; 01-22-2011 at 07:32 PM.
-
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
-
Multiple persistance unit simultaneously, issue.
By afrodom in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 03-06-2010, 05:04 PM -
Why my threads don't run simultaneously?
By Gilvan Justino in forum New To JavaReplies: 7Last Post: 01-16-2010, 01:43 AM -
Enabling clients to simultaneously download a file
By DannyZB in forum NetworkingReplies: 1Last Post: 12-21-2008, 09:06 AM -
Beginner Java graphics - filling concentric circles with color
By GenkiSudo in forum New To JavaReplies: 4Last Post: 09-13-2008, 11:07 AM -
Circles (Loops)
By Zebra in forum New To JavaReplies: 1Last Post: 05-29-2008, 06:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks