How can I pass an argument to paintComponent in order to call it in a different class?












1















In my main class I have the following code to load an image from my machine and display it on the frame to draw things on it:



public class ShowMap extends JPanel {

private static final int WIDTH = 1340;
private static final int HEIGHT = 613;

public void main(String args) {
JFrame frame = new JFrame("MAP");
frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
JLabel label = new JLabel();
label.setIcon(new ImageIcon("map.png"));
panel.add(label);
}
}


The image I am loading is a map where I would like to indicate the position of some objects by drawing points in the right coordinates. So it is important here to dictate to the DrawPoint class (below) what coordinates should get the point.



Also, I would greatly appreciate an explanation of how to erase a point that has been drawn.



My search led me to the following, but as soon as I add int coordx, int coordy to the arguments of the method, it is no more highlighted, and I don't know how to call this method in ShowMap while passing the coordinates as arguments.



public class DrawPoint extends JPanel {

private int coordx;
private int coordy;

public void paintComponent(Graphics g, int coordx, int coordy){
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}
}









share|improve this question




















  • 1





    First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again

    – MadProgrammer
    Nov 24 '18 at 1:30
















1















In my main class I have the following code to load an image from my machine and display it on the frame to draw things on it:



public class ShowMap extends JPanel {

private static final int WIDTH = 1340;
private static final int HEIGHT = 613;

public void main(String args) {
JFrame frame = new JFrame("MAP");
frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
JLabel label = new JLabel();
label.setIcon(new ImageIcon("map.png"));
panel.add(label);
}
}


The image I am loading is a map where I would like to indicate the position of some objects by drawing points in the right coordinates. So it is important here to dictate to the DrawPoint class (below) what coordinates should get the point.



Also, I would greatly appreciate an explanation of how to erase a point that has been drawn.



My search led me to the following, but as soon as I add int coordx, int coordy to the arguments of the method, it is no more highlighted, and I don't know how to call this method in ShowMap while passing the coordinates as arguments.



public class DrawPoint extends JPanel {

private int coordx;
private int coordy;

public void paintComponent(Graphics g, int coordx, int coordy){
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}
}









share|improve this question




















  • 1





    First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again

    – MadProgrammer
    Nov 24 '18 at 1:30














1












1








1








In my main class I have the following code to load an image from my machine and display it on the frame to draw things on it:



public class ShowMap extends JPanel {

private static final int WIDTH = 1340;
private static final int HEIGHT = 613;

public void main(String args) {
JFrame frame = new JFrame("MAP");
frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
JLabel label = new JLabel();
label.setIcon(new ImageIcon("map.png"));
panel.add(label);
}
}


The image I am loading is a map where I would like to indicate the position of some objects by drawing points in the right coordinates. So it is important here to dictate to the DrawPoint class (below) what coordinates should get the point.



Also, I would greatly appreciate an explanation of how to erase a point that has been drawn.



My search led me to the following, but as soon as I add int coordx, int coordy to the arguments of the method, it is no more highlighted, and I don't know how to call this method in ShowMap while passing the coordinates as arguments.



public class DrawPoint extends JPanel {

private int coordx;
private int coordy;

public void paintComponent(Graphics g, int coordx, int coordy){
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}
}









share|improve this question
















In my main class I have the following code to load an image from my machine and display it on the frame to draw things on it:



public class ShowMap extends JPanel {

private static final int WIDTH = 1340;
private static final int HEIGHT = 613;

public void main(String args) {
JFrame frame = new JFrame("MAP");
frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.setMaximumSize(new Dimension(WIDTH, HEIGHT));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = (JPanel)frame.getContentPane();
JLabel label = new JLabel();
label.setIcon(new ImageIcon("map.png"));
panel.add(label);
}
}


The image I am loading is a map where I would like to indicate the position of some objects by drawing points in the right coordinates. So it is important here to dictate to the DrawPoint class (below) what coordinates should get the point.



Also, I would greatly appreciate an explanation of how to erase a point that has been drawn.



My search led me to the following, but as soon as I add int coordx, int coordy to the arguments of the method, it is no more highlighted, and I don't know how to call this method in ShowMap while passing the coordinates as arguments.



public class DrawPoint extends JPanel {

private int coordx;
private int coordy;

public void paintComponent(Graphics g, int coordx, int coordy){
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}
}






java swing graphics awt custom-painting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 4:58









Andrew Thompson

153k28162344




153k28162344










asked Nov 23 '18 at 23:32









Taoufik SekkatTaoufik Sekkat

154




154








  • 1





    First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again

    – MadProgrammer
    Nov 24 '18 at 1:30














  • 1





    First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again

    – MadProgrammer
    Nov 24 '18 at 1:30








1




1





First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again

– MadProgrammer
Nov 24 '18 at 1:30





First, you can’t - and shouldn’t. Paint should paint the current state of the component, which means you should be changing a state variable of the component and then calling repaint. Second, you simply call super.paintComponent, it will clear context so you can paint on it again

– MadProgrammer
Nov 24 '18 at 1:30












1 Answer
1






active

oldest

votes


















0














Here is a demonstration of what MadProgrammer wrote in his comment : "should be changing a state variable of the component and then calling repaint" :



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingTest extends JFrame {

private static final int SIZE = 300;
private DrawPoint drawPoint;

public SwingTest() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(SIZE, SIZE));
add(drawPoint);
pack();
setVisible(true);
}

//demonstrate change in DrawPoint state
private void reDraw() {

Random rnd = new Random();
Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
drawPoint.setCoordx(rnd.nextInt(SIZE));
drawPoint.setCoordy(rnd.nextInt(SIZE));
drawPoint.repaint();
});
timer.start();
}

public static void main(String args){
SwingUtilities.invokeLater(() -> new SwingTest().reDraw());
}
}

class DrawPoint extends JPanel {

private int coordx, coordy;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}

//use setters to change the state
void setCoordy(int coordy) { this.coordy = coordy; }
void setCoordx(int coordx) {this.coordx = coordx;}
}





share|improve this answer
























  • This works perfectly, Thank you!

    – Taoufik Sekkat
    Nov 24 '18 at 15:03











  • c0der, would you please explain how I could do this on a loaded image from my machine?

    – Taoufik Sekkat
    Nov 24 '18 at 15:26











  • Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

    – c0der
    Nov 24 '18 at 17:46











  • I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

    – Taoufik Sekkat
    Nov 24 '18 at 20:36













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%2f53453884%2fhow-can-i-pass-an-argument-to-paintcomponent-in-order-to-call-it-in-a-different%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here is a demonstration of what MadProgrammer wrote in his comment : "should be changing a state variable of the component and then calling repaint" :



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingTest extends JFrame {

private static final int SIZE = 300;
private DrawPoint drawPoint;

public SwingTest() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(SIZE, SIZE));
add(drawPoint);
pack();
setVisible(true);
}

//demonstrate change in DrawPoint state
private void reDraw() {

Random rnd = new Random();
Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
drawPoint.setCoordx(rnd.nextInt(SIZE));
drawPoint.setCoordy(rnd.nextInt(SIZE));
drawPoint.repaint();
});
timer.start();
}

public static void main(String args){
SwingUtilities.invokeLater(() -> new SwingTest().reDraw());
}
}

class DrawPoint extends JPanel {

private int coordx, coordy;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}

//use setters to change the state
void setCoordy(int coordy) { this.coordy = coordy; }
void setCoordx(int coordx) {this.coordx = coordx;}
}





share|improve this answer
























  • This works perfectly, Thank you!

    – Taoufik Sekkat
    Nov 24 '18 at 15:03











  • c0der, would you please explain how I could do this on a loaded image from my machine?

    – Taoufik Sekkat
    Nov 24 '18 at 15:26











  • Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

    – c0der
    Nov 24 '18 at 17:46











  • I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

    – Taoufik Sekkat
    Nov 24 '18 at 20:36


















0














Here is a demonstration of what MadProgrammer wrote in his comment : "should be changing a state variable of the component and then calling repaint" :



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingTest extends JFrame {

private static final int SIZE = 300;
private DrawPoint drawPoint;

public SwingTest() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(SIZE, SIZE));
add(drawPoint);
pack();
setVisible(true);
}

//demonstrate change in DrawPoint state
private void reDraw() {

Random rnd = new Random();
Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
drawPoint.setCoordx(rnd.nextInt(SIZE));
drawPoint.setCoordy(rnd.nextInt(SIZE));
drawPoint.repaint();
});
timer.start();
}

public static void main(String args){
SwingUtilities.invokeLater(() -> new SwingTest().reDraw());
}
}

class DrawPoint extends JPanel {

private int coordx, coordy;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}

//use setters to change the state
void setCoordy(int coordy) { this.coordy = coordy; }
void setCoordx(int coordx) {this.coordx = coordx;}
}





share|improve this answer
























  • This works perfectly, Thank you!

    – Taoufik Sekkat
    Nov 24 '18 at 15:03











  • c0der, would you please explain how I could do this on a loaded image from my machine?

    – Taoufik Sekkat
    Nov 24 '18 at 15:26











  • Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

    – c0der
    Nov 24 '18 at 17:46











  • I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

    – Taoufik Sekkat
    Nov 24 '18 at 20:36
















0












0








0







Here is a demonstration of what MadProgrammer wrote in his comment : "should be changing a state variable of the component and then calling repaint" :



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingTest extends JFrame {

private static final int SIZE = 300;
private DrawPoint drawPoint;

public SwingTest() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(SIZE, SIZE));
add(drawPoint);
pack();
setVisible(true);
}

//demonstrate change in DrawPoint state
private void reDraw() {

Random rnd = new Random();
Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
drawPoint.setCoordx(rnd.nextInt(SIZE));
drawPoint.setCoordy(rnd.nextInt(SIZE));
drawPoint.repaint();
});
timer.start();
}

public static void main(String args){
SwingUtilities.invokeLater(() -> new SwingTest().reDraw());
}
}

class DrawPoint extends JPanel {

private int coordx, coordy;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}

//use setters to change the state
void setCoordy(int coordy) { this.coordy = coordy; }
void setCoordx(int coordx) {this.coordx = coordx;}
}





share|improve this answer













Here is a demonstration of what MadProgrammer wrote in his comment : "should be changing a state variable of the component and then calling repaint" :



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SwingTest extends JFrame {

private static final int SIZE = 300;
private DrawPoint drawPoint;

public SwingTest() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(SIZE, SIZE));
add(drawPoint);
pack();
setVisible(true);
}

//demonstrate change in DrawPoint state
private void reDraw() {

Random rnd = new Random();
Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
drawPoint.setCoordx(rnd.nextInt(SIZE));
drawPoint.setCoordy(rnd.nextInt(SIZE));
drawPoint.repaint();
});
timer.start();
}

public static void main(String args){
SwingUtilities.invokeLater(() -> new SwingTest().reDraw());
}
}

class DrawPoint extends JPanel {

private int coordx, coordy;

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
}

//use setters to change the state
void setCoordy(int coordy) { this.coordy = coordy; }
void setCoordx(int coordx) {this.coordx = coordx;}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 11:15









c0derc0der

8,81351745




8,81351745













  • This works perfectly, Thank you!

    – Taoufik Sekkat
    Nov 24 '18 at 15:03











  • c0der, would you please explain how I could do this on a loaded image from my machine?

    – Taoufik Sekkat
    Nov 24 '18 at 15:26











  • Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

    – c0der
    Nov 24 '18 at 17:46











  • I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

    – Taoufik Sekkat
    Nov 24 '18 at 20:36





















  • This works perfectly, Thank you!

    – Taoufik Sekkat
    Nov 24 '18 at 15:03











  • c0der, would you please explain how I could do this on a loaded image from my machine?

    – Taoufik Sekkat
    Nov 24 '18 at 15:26











  • Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

    – c0der
    Nov 24 '18 at 17:46











  • I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

    – Taoufik Sekkat
    Nov 24 '18 at 20:36



















This works perfectly, Thank you!

– Taoufik Sekkat
Nov 24 '18 at 15:03





This works perfectly, Thank you!

– Taoufik Sekkat
Nov 24 '18 at 15:03













c0der, would you please explain how I could do this on a loaded image from my machine?

– Taoufik Sekkat
Nov 24 '18 at 15:26





c0der, would you please explain how I could do this on a loaded image from my machine?

– Taoufik Sekkat
Nov 24 '18 at 15:26













Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

– c0der
Nov 24 '18 at 17:46





Assuming your image is Image bgImage draw it by g.drawImage(bgImage, 0, 0, this);. If you need help with it post a new question and leave me a message here so I can try and help.

– c0der
Nov 24 '18 at 17:46













I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

– Taoufik Sekkat
Nov 24 '18 at 20:36







I posted a new question, link: stackoverflow.com/questions/53462122/…. Thank you for all the help.

– Taoufik Sekkat
Nov 24 '18 at 20:36






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453884%2fhow-can-i-pass-an-argument-to-paintcomponent-in-order-to-call-it-in-a-different%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

Costa Masnaga

Fotorealismo

Sidney Franklin