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);
}
}









share|improve this question






















  • 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















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);
}
}









share|improve this question






















  • 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













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);
}
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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

















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',
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
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python