How do you display more than one image in the JFrame window in real time?
For the purposes of my project, I'm trying to simulate a phyllotaxis pattern by creating multiple circles in real time using the formulas given.
So recently, I've decided to try out GUI programming in Java using JFrame and swing, and I've hit a wall trying to figure out how to get everything running properly. My idea was to slowly print out circle after circle with their x and y coordinates being calculated from the "r = cos/sin(theta)" formulas documented in the phyllotaxis instructions. Unfortunately, while the x and y values are constantly changing, only one circle is printed. Is there something I am missing?
package gExample;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class GraphicsExample extends Canvas implements ActionListener {
private final static int HEIGHT = 600;
private final static int WIDTH = 600;
private int n = 0;
private int x, y;
Timer t = new Timer(20, this);
public static void main(String args) {
JFrame frame = new JFrame();
GraphicsExample canvas = new GraphicsExample();
canvas.setSize(WIDTH, HEIGHT);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.black);
}
public void paint(Graphics g){
Random rand = new Random();
Color col = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(col);
/*each time paint() is called, I expect a new circle to be printed in the
x and y position that was updated by actionPerformed(), but only one inital circle is created. */
g.fillOval(x, y, 8, 8);
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int c = 9;
double r = c * Math.sqrt(n);
double angle = n * 137.5;
//here, every time the method is called, the x and y values are updated,
//which will be used to fill in a new circle
int x = (int) (r * Math.cos(angle * (Math.PI / 180) )) + (WIDTH / 2);
int y = (int) (r * Math.sin(angle * (Math.PI / 180) )) + (HEIGHT / 2);
//when the program is running, this line of code is executed multiple times.
System.out.println("x: " + x + " y: " + y);
n++;
}
}
java swing jframe
add a comment |
For the purposes of my project, I'm trying to simulate a phyllotaxis pattern by creating multiple circles in real time using the formulas given.
So recently, I've decided to try out GUI programming in Java using JFrame and swing, and I've hit a wall trying to figure out how to get everything running properly. My idea was to slowly print out circle after circle with their x and y coordinates being calculated from the "r = cos/sin(theta)" formulas documented in the phyllotaxis instructions. Unfortunately, while the x and y values are constantly changing, only one circle is printed. Is there something I am missing?
package gExample;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class GraphicsExample extends Canvas implements ActionListener {
private final static int HEIGHT = 600;
private final static int WIDTH = 600;
private int n = 0;
private int x, y;
Timer t = new Timer(20, this);
public static void main(String args) {
JFrame frame = new JFrame();
GraphicsExample canvas = new GraphicsExample();
canvas.setSize(WIDTH, HEIGHT);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.black);
}
public void paint(Graphics g){
Random rand = new Random();
Color col = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(col);
/*each time paint() is called, I expect a new circle to be printed in the
x and y position that was updated by actionPerformed(), but only one inital circle is created. */
g.fillOval(x, y, 8, 8);
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int c = 9;
double r = c * Math.sqrt(n);
double angle = n * 137.5;
//here, every time the method is called, the x and y values are updated,
//which will be used to fill in a new circle
int x = (int) (r * Math.cos(angle * (Math.PI / 180) )) + (WIDTH / 2);
int y = (int) (r * Math.sin(angle * (Math.PI / 180) )) + (HEIGHT / 2);
//when the program is running, this line of code is executed multiple times.
System.out.println("x: " + x + " y: " + y);
n++;
}
}
java swing jframe
1
The custom painting in a Swing or AWT component is not 'persistent'. To get the effect you want, either a) retain a list of the earlier co-ords (etc.) then iterate the entire list and paint each, for every time the display must update, or.. b) write the changes to a buffered image displayed in a label, then repaint the label when the image changes.
– Andrew Thompson
Jan 2 at 2:52
1
BTW - 1)t.start();
That is not something that should appear in apaint(Graphics)
method. 2) Using AWT components for custom painting is mostly unnecessary. I'd use aJPanel
instead. 3) Whether using Swing (paintComponent(Graphics)
) or AWT (paint(Graphics)
), always call thesuper
method. Doing so guarantees that previous drawings are erased. It often occurs anyway, as you've observed, but best not leave it to chance.
– Andrew Thompson
Jan 2 at 2:56
Option A makes complete sense, but option B is a little bit vague. Can you elaborate a bit more what a label and buffered image is? Also where should t.start() go?
– Darien Miller
Jan 2 at 4:19
AJLabel
can display anImageIcon
which contains aBufferedImage
. Here is an example? "Also where should t.start() go?" The paint method is called whenever the JRE determines it's needed, and may be called many times. The constructor in guaranteed to be called exactly once, so that's where I'd put it. But better to use a SwingTimer
(see earlier eg)
– Andrew Thompson
Jan 2 at 5:29
add a comment |
For the purposes of my project, I'm trying to simulate a phyllotaxis pattern by creating multiple circles in real time using the formulas given.
So recently, I've decided to try out GUI programming in Java using JFrame and swing, and I've hit a wall trying to figure out how to get everything running properly. My idea was to slowly print out circle after circle with their x and y coordinates being calculated from the "r = cos/sin(theta)" formulas documented in the phyllotaxis instructions. Unfortunately, while the x and y values are constantly changing, only one circle is printed. Is there something I am missing?
package gExample;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class GraphicsExample extends Canvas implements ActionListener {
private final static int HEIGHT = 600;
private final static int WIDTH = 600;
private int n = 0;
private int x, y;
Timer t = new Timer(20, this);
public static void main(String args) {
JFrame frame = new JFrame();
GraphicsExample canvas = new GraphicsExample();
canvas.setSize(WIDTH, HEIGHT);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.black);
}
public void paint(Graphics g){
Random rand = new Random();
Color col = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(col);
/*each time paint() is called, I expect a new circle to be printed in the
x and y position that was updated by actionPerformed(), but only one inital circle is created. */
g.fillOval(x, y, 8, 8);
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int c = 9;
double r = c * Math.sqrt(n);
double angle = n * 137.5;
//here, every time the method is called, the x and y values are updated,
//which will be used to fill in a new circle
int x = (int) (r * Math.cos(angle * (Math.PI / 180) )) + (WIDTH / 2);
int y = (int) (r * Math.sin(angle * (Math.PI / 180) )) + (HEIGHT / 2);
//when the program is running, this line of code is executed multiple times.
System.out.println("x: " + x + " y: " + y);
n++;
}
}
java swing jframe
For the purposes of my project, I'm trying to simulate a phyllotaxis pattern by creating multiple circles in real time using the formulas given.
So recently, I've decided to try out GUI programming in Java using JFrame and swing, and I've hit a wall trying to figure out how to get everything running properly. My idea was to slowly print out circle after circle with their x and y coordinates being calculated from the "r = cos/sin(theta)" formulas documented in the phyllotaxis instructions. Unfortunately, while the x and y values are constantly changing, only one circle is printed. Is there something I am missing?
package gExample;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class GraphicsExample extends Canvas implements ActionListener {
private final static int HEIGHT = 600;
private final static int WIDTH = 600;
private int n = 0;
private int x, y;
Timer t = new Timer(20, this);
public static void main(String args) {
JFrame frame = new JFrame();
GraphicsExample canvas = new GraphicsExample();
canvas.setSize(WIDTH, HEIGHT);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.black);
}
public void paint(Graphics g){
Random rand = new Random();
Color col = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g.setColor(col);
/*each time paint() is called, I expect a new circle to be printed in the
x and y position that was updated by actionPerformed(), but only one inital circle is created. */
g.fillOval(x, y, 8, 8);
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int c = 9;
double r = c * Math.sqrt(n);
double angle = n * 137.5;
//here, every time the method is called, the x and y values are updated,
//which will be used to fill in a new circle
int x = (int) (r * Math.cos(angle * (Math.PI / 180) )) + (WIDTH / 2);
int y = (int) (r * Math.sin(angle * (Math.PI / 180) )) + (HEIGHT / 2);
//when the program is running, this line of code is executed multiple times.
System.out.println("x: " + x + " y: " + y);
n++;
}
}
java swing jframe
java swing jframe
asked Jan 2 at 2:38
Darien MillerDarien Miller
194
194
1
The custom painting in a Swing or AWT component is not 'persistent'. To get the effect you want, either a) retain a list of the earlier co-ords (etc.) then iterate the entire list and paint each, for every time the display must update, or.. b) write the changes to a buffered image displayed in a label, then repaint the label when the image changes.
– Andrew Thompson
Jan 2 at 2:52
1
BTW - 1)t.start();
That is not something that should appear in apaint(Graphics)
method. 2) Using AWT components for custom painting is mostly unnecessary. I'd use aJPanel
instead. 3) Whether using Swing (paintComponent(Graphics)
) or AWT (paint(Graphics)
), always call thesuper
method. Doing so guarantees that previous drawings are erased. It often occurs anyway, as you've observed, but best not leave it to chance.
– Andrew Thompson
Jan 2 at 2:56
Option A makes complete sense, but option B is a little bit vague. Can you elaborate a bit more what a label and buffered image is? Also where should t.start() go?
– Darien Miller
Jan 2 at 4:19
AJLabel
can display anImageIcon
which contains aBufferedImage
. Here is an example? "Also where should t.start() go?" The paint method is called whenever the JRE determines it's needed, and may be called many times. The constructor in guaranteed to be called exactly once, so that's where I'd put it. But better to use a SwingTimer
(see earlier eg)
– Andrew Thompson
Jan 2 at 5:29
add a comment |
1
The custom painting in a Swing or AWT component is not 'persistent'. To get the effect you want, either a) retain a list of the earlier co-ords (etc.) then iterate the entire list and paint each, for every time the display must update, or.. b) write the changes to a buffered image displayed in a label, then repaint the label when the image changes.
– Andrew Thompson
Jan 2 at 2:52
1
BTW - 1)t.start();
That is not something that should appear in apaint(Graphics)
method. 2) Using AWT components for custom painting is mostly unnecessary. I'd use aJPanel
instead. 3) Whether using Swing (paintComponent(Graphics)
) or AWT (paint(Graphics)
), always call thesuper
method. Doing so guarantees that previous drawings are erased. It often occurs anyway, as you've observed, but best not leave it to chance.
– Andrew Thompson
Jan 2 at 2:56
Option A makes complete sense, but option B is a little bit vague. Can you elaborate a bit more what a label and buffered image is? Also where should t.start() go?
– Darien Miller
Jan 2 at 4:19
AJLabel
can display anImageIcon
which contains aBufferedImage
. Here is an example? "Also where should t.start() go?" The paint method is called whenever the JRE determines it's needed, and may be called many times. The constructor in guaranteed to be called exactly once, so that's where I'd put it. But better to use a SwingTimer
(see earlier eg)
– Andrew Thompson
Jan 2 at 5:29
1
1
The custom painting in a Swing or AWT component is not 'persistent'. To get the effect you want, either a) retain a list of the earlier co-ords (etc.) then iterate the entire list and paint each, for every time the display must update, or.. b) write the changes to a buffered image displayed in a label, then repaint the label when the image changes.
– Andrew Thompson
Jan 2 at 2:52
The custom painting in a Swing or AWT component is not 'persistent'. To get the effect you want, either a) retain a list of the earlier co-ords (etc.) then iterate the entire list and paint each, for every time the display must update, or.. b) write the changes to a buffered image displayed in a label, then repaint the label when the image changes.
– Andrew Thompson
Jan 2 at 2:52
1
1
BTW - 1)
t.start();
That is not something that should appear in a paint(Graphics)
method. 2) Using AWT components for custom painting is mostly unnecessary. I'd use a JPanel
instead. 3) Whether using Swing (paintComponent(Graphics)
) or AWT (paint(Graphics)
), always call the super
method. Doing so guarantees that previous drawings are erased. It often occurs anyway, as you've observed, but best not leave it to chance.– Andrew Thompson
Jan 2 at 2:56
BTW - 1)
t.start();
That is not something that should appear in a paint(Graphics)
method. 2) Using AWT components for custom painting is mostly unnecessary. I'd use a JPanel
instead. 3) Whether using Swing (paintComponent(Graphics)
) or AWT (paint(Graphics)
), always call the super
method. Doing so guarantees that previous drawings are erased. It often occurs anyway, as you've observed, but best not leave it to chance.– Andrew Thompson
Jan 2 at 2:56
Option A makes complete sense, but option B is a little bit vague. Can you elaborate a bit more what a label and buffered image is? Also where should t.start() go?
– Darien Miller
Jan 2 at 4:19
Option A makes complete sense, but option B is a little bit vague. Can you elaborate a bit more what a label and buffered image is? Also where should t.start() go?
– Darien Miller
Jan 2 at 4:19
A
JLabel
can display an ImageIcon
which contains a BufferedImage
. Here is an example? "Also where should t.start() go?" The paint method is called whenever the JRE determines it's needed, and may be called many times. The constructor in guaranteed to be called exactly once, so that's where I'd put it. But better to use a Swing Timer
(see earlier eg)– Andrew Thompson
Jan 2 at 5:29
A
JLabel
can display an ImageIcon
which contains a BufferedImage
. Here is an example? "Also where should t.start() go?" The paint method is called whenever the JRE determines it's needed, and may be called many times. The constructor in guaranteed to be called exactly once, so that's where I'd put it. But better to use a Swing Timer
(see earlier eg)– Andrew Thompson
Jan 2 at 5:29
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000619%2fhow-do-you-display-more-than-one-image-in-the-jframe-window-in-real-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000619%2fhow-do-you-display-more-than-one-image-in-the-jframe-window-in-real-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The custom painting in a Swing or AWT component is not 'persistent'. To get the effect you want, either a) retain a list of the earlier co-ords (etc.) then iterate the entire list and paint each, for every time the display must update, or.. b) write the changes to a buffered image displayed in a label, then repaint the label when the image changes.
– Andrew Thompson
Jan 2 at 2:52
1
BTW - 1)
t.start();
That is not something that should appear in apaint(Graphics)
method. 2) Using AWT components for custom painting is mostly unnecessary. I'd use aJPanel
instead. 3) Whether using Swing (paintComponent(Graphics)
) or AWT (paint(Graphics)
), always call thesuper
method. Doing so guarantees that previous drawings are erased. It often occurs anyway, as you've observed, but best not leave it to chance.– Andrew Thompson
Jan 2 at 2:56
Option A makes complete sense, but option B is a little bit vague. Can you elaborate a bit more what a label and buffered image is? Also where should t.start() go?
– Darien Miller
Jan 2 at 4:19
A
JLabel
can display anImageIcon
which contains aBufferedImage
. Here is an example? "Also where should t.start() go?" The paint method is called whenever the JRE determines it's needed, and may be called many times. The constructor in guaranteed to be called exactly once, so that's where I'd put it. But better to use a SwingTimer
(see earlier eg)– Andrew Thompson
Jan 2 at 5:29