I have a problem with how to make an other parameter in the paintComponent
method.
I have not found other ways.
import javax.swing.*;
import java.awt.*;
public class Interface extends JPanel
{
protected void paintComponent(Graphics g *here(not part of code btw)*) {
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(0, 0, 100, 100);
}
public void CreateWindow(String name, int Xsize, int Ysize) {
//laver et JFrame og klader det "frame" !
JFrame frame= new JFrame();
frame.setTitle(name);
frame.setSize(Xsize, Ysize);
frame.setLocation(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
Interface JPanel = new Interface();
frame.add(JPanel);
Graphics Grafik = getGraphics();
paintComponent(Grafik);
}
}
When I run the code with a parameter there its doesn't draw a rectangle.
But if there are only the Graphics
parameter it runs fine.
As you can see in the Javadoc, there is only one defined method for paintComponent in JComponent. This means that there is not a way that you can do this, without creating your own JComponent and extensions (subclasses) of JComponent (which would be unnecessarily complicated and difficult). Instead, consider that you can use fields within your class to store the persistent state you need when entering the method paintComponent. Temporary variables, on the other hand, are best defined as local to the method.
Additionally, it's not good practice to name your class Interface
, as interface
is a reserved keyword in Java.
tl;dr - Essentially, no. Use fields/local variables to store your additional data.