public class BI2DPv extensions javax.swing.JPanel{
BufferedImage bi;// Draw to this and then transfer to g2
@ Override
public void paintComponent (Graphics g) {
// super.paintComponent(g);
Graphics 2D g2=(Graphics 2D)g;
logger.log(Level.INFO, "Entered in paintComponent");
// paintComponent (repaint) simply transcribes bi to g2.
if(bi!=null)g2.drawImage(bi,null,this);
}
//bi is generated by a different method.
}
We have prepared a class for Java 2D drawing that inherits JPanel
.Generating an instance of BI2DPv
in the main class and running bi2dpv.repaint();
does not run paintComponent
; I have seen in the log that bi2dpv.repaint();
is called.
I don't know what BI2DPv#repaint()
is called, so I guess that JPanel
has not been added to top-level containers such as JFrame
or Japplet
.
For example, the following sample code runs bi2dpv.repaint();
every second, but comments out return bi2dpv;
and runs return new JPanel();
and hides bi2dpv
in JFrame
.
Sample Code Edit:Change to add bi2dpv
to JTabbedPane
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class BI2DPv extensions JPanel {
BufferedImage bi;// Draw to this and then transfer to g2
@ Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics 2D g2=(Graphics 2D)g;
// logger.log(Level.INFO, "Entered in paintComponent");
System.out.println("paintComponent");
// paintComponent (repaint) simply transcribes bi to g2.
if(bi!=null){
g2.drawImage(bi, null, this);
}
}
//bi is generated by a different method.
private static JComponent makeUI() {
JPanel bi2dpv = new BI2DPv();
new Timer (1000, e->{
System.out.println("\nbi2dpv.repaint()");
System.out.println("isDisplayable?"+bi2dpv.isDisplayable());
System.out.println("isShowing?"+bi2dpv.isShowing());
System.out.println("isVisible?"+bi2dpv.isVisible());
bi2dpv.repaint();
}).start();
JTabbedPane tabbedPane=new JTabbedPane();
tabbedPane.addTab("aaa", newJPanel());
tabbedPane.addTab("bbb", bi2dpv);
return tabbedPane;
// return new JPanel();
}
public static void main(String...args) {
EventQueue.invokeLater(()->{
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(makeUI());
f.setSize (320,240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
581 PHP ssh2_scp_send fails to send files as intended
612 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.