How to use multiple graphics on JFrame?












0














I have a JButton and when I click on this button it should draw a graph.



Peach p = new Peach();
MyGr Gr = new MyGr();

build = new JButton("Build graph");
build.setBounds(320,550,100,20);
build.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(Gr);
}

});

add(build);

add(p);

setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


And I have two graphics objects:



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

public class Peach extends JPanel {
......

}


And my graph:



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

public class MyGr extends JPanel {

int w = 200;
int h = 425;

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = ( Graphics2D)g;
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);

Polygon p = new Polygon();

for (int x = -3; x < 4; x++) {

p.addPoint(w + 17 * x, h - 2* ((1) * x * x * x * x + 0 * x * x + (-1) * x * x + (5) * x - 10));
}

g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);


}
}


Right now nothing happens



enter image description here



When I add content of MyGr to Peach class and use only Peach then it works fine, but I want to display a graph when I click on button



enter image description here



So, when I click this button, it should build a graph.
I don't understand why add(Gr); inside actionPerfomed doesn't work.










share|improve this question
























  • The best way to get us to fully and quickly understand your problem would be if you were to to create and post a minimal example program, a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:22










  • Side note: after adding a component (here Gr) to a container, you'll want to call revalidate() and repaint() on the container to lay out the new component and then have the container redraw itself and all its parts. Also you are using setBounds which suggests use of null layouts. If so, this is something that you really don't want to do.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:23






  • 1




    Based on your images, it appears that you may be going about your program wrong. Rather than adding new components to the graph-drawing GUI you should instead consider adding Shape objects to the a collection held by the current drawing JPanel, drawing them within its paintComponent and then calling repaint() on it
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:27










  • thank you,I will try then!
    – Anime Knight
    Nov 18 '18 at 13:33
















0














I have a JButton and when I click on this button it should draw a graph.



Peach p = new Peach();
MyGr Gr = new MyGr();

build = new JButton("Build graph");
build.setBounds(320,550,100,20);
build.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(Gr);
}

});

add(build);

add(p);

setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


And I have two graphics objects:



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

public class Peach extends JPanel {
......

}


And my graph:



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

public class MyGr extends JPanel {

int w = 200;
int h = 425;

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = ( Graphics2D)g;
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);

Polygon p = new Polygon();

for (int x = -3; x < 4; x++) {

p.addPoint(w + 17 * x, h - 2* ((1) * x * x * x * x + 0 * x * x + (-1) * x * x + (5) * x - 10));
}

g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);


}
}


Right now nothing happens



enter image description here



When I add content of MyGr to Peach class and use only Peach then it works fine, but I want to display a graph when I click on button



enter image description here



So, when I click this button, it should build a graph.
I don't understand why add(Gr); inside actionPerfomed doesn't work.










share|improve this question
























  • The best way to get us to fully and quickly understand your problem would be if you were to to create and post a minimal example program, a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:22










  • Side note: after adding a component (here Gr) to a container, you'll want to call revalidate() and repaint() on the container to lay out the new component and then have the container redraw itself and all its parts. Also you are using setBounds which suggests use of null layouts. If so, this is something that you really don't want to do.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:23






  • 1




    Based on your images, it appears that you may be going about your program wrong. Rather than adding new components to the graph-drawing GUI you should instead consider adding Shape objects to the a collection held by the current drawing JPanel, drawing them within its paintComponent and then calling repaint() on it
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:27










  • thank you,I will try then!
    – Anime Knight
    Nov 18 '18 at 13:33














0












0








0







I have a JButton and when I click on this button it should draw a graph.



Peach p = new Peach();
MyGr Gr = new MyGr();

build = new JButton("Build graph");
build.setBounds(320,550,100,20);
build.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(Gr);
}

});

add(build);

add(p);

setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


And I have two graphics objects:



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

public class Peach extends JPanel {
......

}


And my graph:



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

public class MyGr extends JPanel {

int w = 200;
int h = 425;

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = ( Graphics2D)g;
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);

Polygon p = new Polygon();

for (int x = -3; x < 4; x++) {

p.addPoint(w + 17 * x, h - 2* ((1) * x * x * x * x + 0 * x * x + (-1) * x * x + (5) * x - 10));
}

g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);


}
}


Right now nothing happens



enter image description here



When I add content of MyGr to Peach class and use only Peach then it works fine, but I want to display a graph when I click on button



enter image description here



So, when I click this button, it should build a graph.
I don't understand why add(Gr); inside actionPerfomed doesn't work.










share|improve this question















I have a JButton and when I click on this button it should draw a graph.



Peach p = new Peach();
MyGr Gr = new MyGr();

build = new JButton("Build graph");
build.setBounds(320,550,100,20);
build.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(Gr);
}

});

add(build);

add(p);

setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


And I have two graphics objects:



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

public class Peach extends JPanel {
......

}


And my graph:



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

public class MyGr extends JPanel {

int w = 200;
int h = 425;

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = ( Graphics2D)g;
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);

Polygon p = new Polygon();

for (int x = -3; x < 4; x++) {

p.addPoint(w + 17 * x, h - 2* ((1) * x * x * x * x + 0 * x * x + (-1) * x * x + (5) * x - 10));
}

g2.drawPolyline(p.xpoints, p.ypoints, p.npoints);


}
}


Right now nothing happens



enter image description here



When I add content of MyGr to Peach class and use only Peach then it works fine, but I want to display a graph when I click on button



enter image description here



So, when I click this button, it should build a graph.
I don't understand why add(Gr); inside actionPerfomed doesn't work.







java swing jframe awt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 14:19









Andrew Thompson

153k27163338




153k27163338










asked Nov 18 '18 at 13:13









Anime KnightAnime Knight

75




75












  • The best way to get us to fully and quickly understand your problem would be if you were to to create and post a minimal example program, a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:22










  • Side note: after adding a component (here Gr) to a container, you'll want to call revalidate() and repaint() on the container to lay out the new component and then have the container redraw itself and all its parts. Also you are using setBounds which suggests use of null layouts. If so, this is something that you really don't want to do.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:23






  • 1




    Based on your images, it appears that you may be going about your program wrong. Rather than adding new components to the graph-drawing GUI you should instead consider adding Shape objects to the a collection held by the current drawing JPanel, drawing them within its paintComponent and then calling repaint() on it
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:27










  • thank you,I will try then!
    – Anime Knight
    Nov 18 '18 at 13:33


















  • The best way to get us to fully and quickly understand your problem would be if you were to to create and post a minimal example program, a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:22










  • Side note: after adding a component (here Gr) to a container, you'll want to call revalidate() and repaint() on the container to lay out the new component and then have the container redraw itself and all its parts. Also you are using setBounds which suggests use of null layouts. If so, this is something that you really don't want to do.
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:23






  • 1




    Based on your images, it appears that you may be going about your program wrong. Rather than adding new components to the graph-drawing GUI you should instead consider adding Shape objects to the a collection held by the current drawing JPanel, drawing them within its paintComponent and then calling repaint() on it
    – Hovercraft Full Of Eels
    Nov 18 '18 at 13:27










  • thank you,I will try then!
    – Anime Knight
    Nov 18 '18 at 13:33
















The best way to get us to fully and quickly understand your problem would be if you were to to create and post a minimal example program, a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification.
– Hovercraft Full Of Eels
Nov 18 '18 at 13:22




The best way to get us to fully and quickly understand your problem would be if you were to to create and post a minimal example program, a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification.
– Hovercraft Full Of Eels
Nov 18 '18 at 13:22












Side note: after adding a component (here Gr) to a container, you'll want to call revalidate() and repaint() on the container to lay out the new component and then have the container redraw itself and all its parts. Also you are using setBounds which suggests use of null layouts. If so, this is something that you really don't want to do.
– Hovercraft Full Of Eels
Nov 18 '18 at 13:23




Side note: after adding a component (here Gr) to a container, you'll want to call revalidate() and repaint() on the container to lay out the new component and then have the container redraw itself and all its parts. Also you are using setBounds which suggests use of null layouts. If so, this is something that you really don't want to do.
– Hovercraft Full Of Eels
Nov 18 '18 at 13:23




1




1




Based on your images, it appears that you may be going about your program wrong. Rather than adding new components to the graph-drawing GUI you should instead consider adding Shape objects to the a collection held by the current drawing JPanel, drawing them within its paintComponent and then calling repaint() on it
– Hovercraft Full Of Eels
Nov 18 '18 at 13:27




Based on your images, it appears that you may be going about your program wrong. Rather than adding new components to the graph-drawing GUI you should instead consider adding Shape objects to the a collection held by the current drawing JPanel, drawing them within its paintComponent and then calling repaint() on it
– Hovercraft Full Of Eels
Nov 18 '18 at 13:27












thank you,I will try then!
– Anime Knight
Nov 18 '18 at 13:33




thank you,I will try then!
– Anime Knight
Nov 18 '18 at 13:33












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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361241%2fhow-to-use-multiple-graphics-on-jframe%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
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361241%2fhow-to-use-multiple-graphics-on-jframe%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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?