-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
I used Borderfactory to see if the panel was working, and yes the panel is correct, but the drawing inside the panel does not appear
Can anyone figure out why the drawing doesn’t appear? It was to appear a square on the screen
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();
}
});
}
}
//Classe do painel
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Pane extends JPanel {
private BufferedImage img;
private Graphics2D g2d;
public Pane() {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
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() { //Metodo para fazer o desenho
g2d.setColor(Color.BLACK);
g2d.drawRect(10, 10, 200, 200);
}
public void paintComponent(Graphics2D g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
Thanks, I tidied up here and it worked, agr I understood how it works, it was really worth
– Andre Dilay