Why JPanel repaint only once? - java

I want to create window where after 3 seconds image automatically will change.
This is my code:
JFrame frame=new JFrame();
pan pane= new pan();
frame.add(pane);
frame.setBounds(100, 100, 500, 500);
frame.setVisible(true);
try{
for(int i=0;i<returnedArray.size();i++){
pane.img=returnedArray.get(i).getFrontImage();
Thread.sleep(3000);
pane.repaint();
}
}catch(InterruptedException e){
e.printStackTrace();
}
class pan extends JPanel{
public Image img;
public void paint(Graphics g) {
g.drawImage( img, 0, 0, null);
}
}
...but I see last image all the time :(
I think that maybe JVM is improving my code?
How can I avoid this?
Maybe I am doing it wrong?
I will be very gratefull for any clue :)

Could your problem be this line in your for loop:
pane.img=returnedArray.get(2).getFrontImage();
which always selects the same image? Aside from this, you should probably use a Timer as pointed out in comments instead of using Thread.sleep

Related

Jframe goes black before displaying contents? [duplicate]

I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen.
Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
1) you have three general issues
never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)
your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater() meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
hey i had he same problem and the screen turns black every time i run the program.
in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.

paintComponent on JPanel not working

I've spent almost 2 hours now on it but I can't get it working.
I just want to paint a image on a JPanel.
I want to paint the imageChaser image on the arena JPanel.
But it's not displaying.
What am i doing wrong?
Heres my code :
public class GuiGameBoard extends JPanel {
//import stuff
private JPanel arena;
BufferedImage imageChaser;
BufferedImage imageChaserSelected;
BufferedImage imageTarget;
public GuiGameBoard() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
arena = new JPanel();
arena.setPreferredSize(new Dimension(500, 500));
arena.setBackground(Color.BLACK);
this.add(arena);
try
{
File inputChaser = new File("resources\\chaser.png");
imageChaser = ImageIO.read(inputChaser);
File inputChaserSelected = new File("resources\\chaser_selected.png");
imageChaserSelected = ImageIO.read(inputChaserSelected);
File inputTarget = new File("resources\\target.png");
imageTarget = ImageIO.read(inputTarget);
}
catch (IOException ie)
{
System.out.println("Error:"+ie.getMessage());
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imageChaser, 0, 0, null);
}
}
I think the problem is, that your hiding your picture by adding the JPanel arena to your GuiGameBoard class, which already is a JPanel.
But without an SSCCE, giving an adequate answer isn't possible...
I think you forget a 'top-level container' e.g. JFrame.
Take a look at this example
example code.
For more information click here

Java - Screen turns black, when setting a JFrame to Fullscreen

I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen.
Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
1) you have three general issues
never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)
your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater() meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
hey i had he same problem and the screen turns black every time i run the program.
in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.

JFrame image display at frame resize

I have this JFrame containing a children of JPanel wherein it displays the image which is declared in this manner.
BufferedImage image = ImageIO.read(filename);
The program displays the image properly. But the only thing is, it requires to resize the frame to display the image.
Is there a possible way to display the image once the frame appears?
You should override paintComponent(Graphics g) and draw the image therein. In this case, you should do this for the JPanel component (I think? If not, do this for the JComponent(s) you're referring to). Also, since Swing is not thread-safe, ensure these modifications are performed in the EDT.
EXAMPLE
public class Demo{
private static BufferedImage bi;
public static void main(String[] args){
try{
loadImage();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
createAndShowGUI();
}
});
}
catch (IOException e){
// handle exception
}
}
private static void loadImage() throws IOException{
bi = ImageIO.read(new File("src/resource/braveheart.PNG"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(){
#Override
protected void paintComponent(Graphics g){
Graphics g2 = g.create();
g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
g2.dispose();
}
#Override
public Dimension getPreferredSize(){
return new Dimension(bi.getWidth(), bi.getHeight());
}
};
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
OUTPUT
It's important to keep in mind that this example ignores rendering hints, so when you maximize the JFrame, the image quality will be very poor. :)
EDIT
When answering this question, I assumed you had a basic understanding of Swing. I suppose I assumed too much. It is important to mention that all components should be added to the top-level container before it's been realized (i.e. made visible). This will ensure that everything is rendered without having to resize your frame. As others have suggested, you could have simply used a JLabel to render the image, and then added it to your JPanel. Instead, I promoted custom painting, which is perfectly acceptable, and to me, cleaner.
for dispaly Image or ImageIcon is better look for JLabel (basic stuff)

How to put a 800x600 image in a jLabel without having to scroll to see the whole image

i tried ways like getScaledInstance but all does not work. i could not find any solutions online either. the code that i am currently using is:
public class ShowImage_1 extends Panel {
BufferedImage image;
public ShowImage_1() {
try {
File input = new File("C:/Lighthouse.jpg");
image = ImageIO.read(input);
image.getGraphics().drawImage(image, 0, 0, 400, 400, null);
} catch (IOException ie) {
System.out.println("Error:" + ie.getMessage());
}
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Display image");
Panel panel = new ShowImage_1();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
it does not show the full image on the panel. any idea on how to go about doing it?
i want to achieve this effect: http://img209.imageshack.us/i/77894822.png/
but what i get now is this: http://img600.imageshack.us/i/19267006.png/
any other code i can use to get that?
Make the panel large enough to hold the image.. This may solve your problem
Add the image to a JLabel. Add the label to the frame. Then use:
frame.pack();
NOT
frame.setSize(800, 600);
If you need more help post your SSCCE demonstrating the problem.

Categories