Java - Graphics only repaint within the initial window
up vote
0
down vote
favorite
I'm trying to draw a trajectory for a "firework" launcher that I made, but I am running into a problem where my trajectory only repaints within the bounds of the initial window size. When I resize the window, everything repaints setting the bottom left corner to the origin, but my trajectory gets erased eventually. I have attached some images of what I mean. I think it might have to do with the for loop that my trajectory is painted with in my main() but I'm not sure how to fix that. Image examples attached. https://i.imgur.com/ymcSE7N.png
package splode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
public class Fuse extends JComponent {
private static final long serialVersionUID = 1L;
double x;
double y;
static JFrame frame = new JFrame("Fireworst");
int key;
public Fuse(int v, int a, double t, int key) {
frame.setSize(1000, 548);
this.x = v*Math.cos(Math.toRadians(a))*t;
this.y = v*Math.sin(Math.toRadians(a))*t-.5*9.8*(Math.pow(t, 2));
this.key = key;
}
public void paintComponent(Graphics g) {
if (key == 0) {
g.setColor(Color.RED);
g.fillOval((int)x, frame.getHeight()-(int)y-50, 2, 2);
}
else if (key == 1) {
for(int j=0; j<=340; j+=20) {
g.setColor(Color.RED);
g.fillArc((int)x-75, frame.getHeight()-(int)y-100, 150, 100, j+10, 3);
g.fillArc((int)x-50, frame.getHeight()-(int) y-125, 100, 150, j, 3);
}
}
}
public static void main(String args) {
for (double i=0; i<=25; i+=.1) {
Fuse canvas = new Fuse(100, 45, i, 10);
frame.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
frame.setVisible(true);
frame.getContentPane().setBackground( Color.BLACK );
Fuse fuse = new Fuse(100, 45, 7, 2);
frame.add(fuse);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(fuse.x);
System.out.println(fuse.y);
}
}
java graphics jframe jcomponent
add a comment |
up vote
0
down vote
favorite
I'm trying to draw a trajectory for a "firework" launcher that I made, but I am running into a problem where my trajectory only repaints within the bounds of the initial window size. When I resize the window, everything repaints setting the bottom left corner to the origin, but my trajectory gets erased eventually. I have attached some images of what I mean. I think it might have to do with the for loop that my trajectory is painted with in my main() but I'm not sure how to fix that. Image examples attached. https://i.imgur.com/ymcSE7N.png
package splode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
public class Fuse extends JComponent {
private static final long serialVersionUID = 1L;
double x;
double y;
static JFrame frame = new JFrame("Fireworst");
int key;
public Fuse(int v, int a, double t, int key) {
frame.setSize(1000, 548);
this.x = v*Math.cos(Math.toRadians(a))*t;
this.y = v*Math.sin(Math.toRadians(a))*t-.5*9.8*(Math.pow(t, 2));
this.key = key;
}
public void paintComponent(Graphics g) {
if (key == 0) {
g.setColor(Color.RED);
g.fillOval((int)x, frame.getHeight()-(int)y-50, 2, 2);
}
else if (key == 1) {
for(int j=0; j<=340; j+=20) {
g.setColor(Color.RED);
g.fillArc((int)x-75, frame.getHeight()-(int)y-100, 150, 100, j+10, 3);
g.fillArc((int)x-50, frame.getHeight()-(int) y-125, 100, 150, j, 3);
}
}
}
public static void main(String args) {
for (double i=0; i<=25; i+=.1) {
Fuse canvas = new Fuse(100, 45, i, 10);
frame.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
frame.setVisible(true);
frame.getContentPane().setBackground( Color.BLACK );
Fuse fuse = new Fuse(100, 45, 7, 2);
frame.add(fuse);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(fuse.x);
System.out.println(fuse.y);
}
}
java graphics jframe jcomponent
Any suggestion at all would be fine, I really cant figure out what to change
– mander39
Nov 11 at 4:57
@mander9 i'm not sure why you are using lot of jcomponents.however the issue is when you are resizing the jframe your jcomponents doesnt resize.so some of the design get cut off.when you are adding jcomponents to a jframe you should give it a perfect layout.to understand this add a color border to your jcomponents -setBorder
.then you can see component doesnt resize as you expect.add one jcomponent and set border layout to your frame and add one jcomponent to border center.you will see it's resizing correctly.may be you want to rethink why do you need 25 jcomponents
– Madhawa Priyashantha
Nov 11 at 5:16
1
Thanks for the help, I managed to fix it by generating coordinates in some arraylists instead of using the jcomponents over and over to generate the coordinates
– mander39
Nov 11 at 6:36
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to draw a trajectory for a "firework" launcher that I made, but I am running into a problem where my trajectory only repaints within the bounds of the initial window size. When I resize the window, everything repaints setting the bottom left corner to the origin, but my trajectory gets erased eventually. I have attached some images of what I mean. I think it might have to do with the for loop that my trajectory is painted with in my main() but I'm not sure how to fix that. Image examples attached. https://i.imgur.com/ymcSE7N.png
package splode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
public class Fuse extends JComponent {
private static final long serialVersionUID = 1L;
double x;
double y;
static JFrame frame = new JFrame("Fireworst");
int key;
public Fuse(int v, int a, double t, int key) {
frame.setSize(1000, 548);
this.x = v*Math.cos(Math.toRadians(a))*t;
this.y = v*Math.sin(Math.toRadians(a))*t-.5*9.8*(Math.pow(t, 2));
this.key = key;
}
public void paintComponent(Graphics g) {
if (key == 0) {
g.setColor(Color.RED);
g.fillOval((int)x, frame.getHeight()-(int)y-50, 2, 2);
}
else if (key == 1) {
for(int j=0; j<=340; j+=20) {
g.setColor(Color.RED);
g.fillArc((int)x-75, frame.getHeight()-(int)y-100, 150, 100, j+10, 3);
g.fillArc((int)x-50, frame.getHeight()-(int) y-125, 100, 150, j, 3);
}
}
}
public static void main(String args) {
for (double i=0; i<=25; i+=.1) {
Fuse canvas = new Fuse(100, 45, i, 10);
frame.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
frame.setVisible(true);
frame.getContentPane().setBackground( Color.BLACK );
Fuse fuse = new Fuse(100, 45, 7, 2);
frame.add(fuse);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(fuse.x);
System.out.println(fuse.y);
}
}
java graphics jframe jcomponent
I'm trying to draw a trajectory for a "firework" launcher that I made, but I am running into a problem where my trajectory only repaints within the bounds of the initial window size. When I resize the window, everything repaints setting the bottom left corner to the origin, but my trajectory gets erased eventually. I have attached some images of what I mean. I think it might have to do with the for loop that my trajectory is painted with in my main() but I'm not sure how to fix that. Image examples attached. https://i.imgur.com/ymcSE7N.png
package splode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
public class Fuse extends JComponent {
private static final long serialVersionUID = 1L;
double x;
double y;
static JFrame frame = new JFrame("Fireworst");
int key;
public Fuse(int v, int a, double t, int key) {
frame.setSize(1000, 548);
this.x = v*Math.cos(Math.toRadians(a))*t;
this.y = v*Math.sin(Math.toRadians(a))*t-.5*9.8*(Math.pow(t, 2));
this.key = key;
}
public void paintComponent(Graphics g) {
if (key == 0) {
g.setColor(Color.RED);
g.fillOval((int)x, frame.getHeight()-(int)y-50, 2, 2);
}
else if (key == 1) {
for(int j=0; j<=340; j+=20) {
g.setColor(Color.RED);
g.fillArc((int)x-75, frame.getHeight()-(int)y-100, 150, 100, j+10, 3);
g.fillArc((int)x-50, frame.getHeight()-(int) y-125, 100, 150, j, 3);
}
}
}
public static void main(String args) {
for (double i=0; i<=25; i+=.1) {
Fuse canvas = new Fuse(100, 45, i, 10);
frame.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
frame.setVisible(true);
frame.getContentPane().setBackground( Color.BLACK );
Fuse fuse = new Fuse(100, 45, 7, 2);
frame.add(fuse);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(fuse.x);
System.out.println(fuse.y);
}
}
java graphics jframe jcomponent
java graphics jframe jcomponent
asked Nov 11 at 4:16
mander39
61
61
Any suggestion at all would be fine, I really cant figure out what to change
– mander39
Nov 11 at 4:57
@mander9 i'm not sure why you are using lot of jcomponents.however the issue is when you are resizing the jframe your jcomponents doesnt resize.so some of the design get cut off.when you are adding jcomponents to a jframe you should give it a perfect layout.to understand this add a color border to your jcomponents -setBorder
.then you can see component doesnt resize as you expect.add one jcomponent and set border layout to your frame and add one jcomponent to border center.you will see it's resizing correctly.may be you want to rethink why do you need 25 jcomponents
– Madhawa Priyashantha
Nov 11 at 5:16
1
Thanks for the help, I managed to fix it by generating coordinates in some arraylists instead of using the jcomponents over and over to generate the coordinates
– mander39
Nov 11 at 6:36
add a comment |
Any suggestion at all would be fine, I really cant figure out what to change
– mander39
Nov 11 at 4:57
@mander9 i'm not sure why you are using lot of jcomponents.however the issue is when you are resizing the jframe your jcomponents doesnt resize.so some of the design get cut off.when you are adding jcomponents to a jframe you should give it a perfect layout.to understand this add a color border to your jcomponents -setBorder
.then you can see component doesnt resize as you expect.add one jcomponent and set border layout to your frame and add one jcomponent to border center.you will see it's resizing correctly.may be you want to rethink why do you need 25 jcomponents
– Madhawa Priyashantha
Nov 11 at 5:16
1
Thanks for the help, I managed to fix it by generating coordinates in some arraylists instead of using the jcomponents over and over to generate the coordinates
– mander39
Nov 11 at 6:36
Any suggestion at all would be fine, I really cant figure out what to change
– mander39
Nov 11 at 4:57
Any suggestion at all would be fine, I really cant figure out what to change
– mander39
Nov 11 at 4:57
@mander9 i'm not sure why you are using lot of jcomponents.however the issue is when you are resizing the jframe your jcomponents doesnt resize.so some of the design get cut off.when you are adding jcomponents to a jframe you should give it a perfect layout.to understand this add a color border to your jcomponents -
setBorder
.then you can see component doesnt resize as you expect.add one jcomponent and set border layout to your frame and add one jcomponent to border center.you will see it's resizing correctly.may be you want to rethink why do you need 25 jcomponents– Madhawa Priyashantha
Nov 11 at 5:16
@mander9 i'm not sure why you are using lot of jcomponents.however the issue is when you are resizing the jframe your jcomponents doesnt resize.so some of the design get cut off.when you are adding jcomponents to a jframe you should give it a perfect layout.to understand this add a color border to your jcomponents -
setBorder
.then you can see component doesnt resize as you expect.add one jcomponent and set border layout to your frame and add one jcomponent to border center.you will see it's resizing correctly.may be you want to rethink why do you need 25 jcomponents– Madhawa Priyashantha
Nov 11 at 5:16
1
1
Thanks for the help, I managed to fix it by generating coordinates in some arraylists instead of using the jcomponents over and over to generate the coordinates
– mander39
Nov 11 at 6:36
Thanks for the help, I managed to fix it by generating coordinates in some arraylists instead of using the jcomponents over and over to generate the coordinates
– mander39
Nov 11 at 6:36
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53245789%2fjava-graphics-only-repaint-within-the-initial-window%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
Any suggestion at all would be fine, I really cant figure out what to change
– mander39
Nov 11 at 4:57
@mander9 i'm not sure why you are using lot of jcomponents.however the issue is when you are resizing the jframe your jcomponents doesnt resize.so some of the design get cut off.when you are adding jcomponents to a jframe you should give it a perfect layout.to understand this add a color border to your jcomponents -
setBorder
.then you can see component doesnt resize as you expect.add one jcomponent and set border layout to your frame and add one jcomponent to border center.you will see it's resizing correctly.may be you want to rethink why do you need 25 jcomponents– Madhawa Priyashantha
Nov 11 at 5:16
1
Thanks for the help, I managed to fix it by generating coordinates in some arraylists instead of using the jcomponents over and over to generate the coordinates
– mander39
Nov 11 at 6:36