-2
I’m learning Java and I needed to use Bufferedimage, but for some reason this doesn’t seem to work
Error is that the drawing does not appear in the panel, I followed several tutorials but still not sure, I do not know what the problem may be
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class App extends JFrame{
public App() {
super("Buffered Test");
add(new Pane());
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App();
}
});
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class Pane extends JPanel {
private BufferedImage img;
private Graphics2D g2d;
public Pane() {
img = new BufferedImage(App.WIDTH, App.HEIGHT, BufferedImage.TYPE_INT_RGB);
g2d = (Graphics2D) img.getGraphics();
draw();
}
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
public void draw() {
g2d.setColor(Color.BLACK);
g2d.drawRect(10, 10, 200, 200);
}
public void paintComponent(Graphics2D g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
When I run the program it just appears the Frame, I figured it is some problem when calling the Paint This is all the code
Right.. And what is the question? Where is the error? An important tip here: make life easier for those who are willing to help you.. Of ALL possible details, otherwise, hardly anyone will give up their time to perform analyses of others.
– Filipe L. Constante
Thanks for the tip The Error is that the drawing does not appear in the panel, I followed several tutorials but still not right, I do not know what the problem may be
– Andre Dilay
And on the console, no error?
– Filipe L. Constante
Nothing, the console is empty
– Andre Dilay