A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
Why jvm don´t call paintComponent(Graphics g)?
Fernando hiar
Greenhorn
Joined: Jan 11, 2008
Posts: 29
posted
Feb 22, 2012 18:12:33
0
I loaded the Image in the class CarregarImagem this code é alright, but when I draw the images the jvm don´t call paintComponent, why?
here my code:
PintarImage class:
package Codigo; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; public class PintarImagem extends JPanel { Image nome2[] = new Image[21]; CarregarImagens ima; int x1[] = new int[21]; int y1[] = new int[21]; int pWidth, pHeight; public PintarImagem(int w, int h, CarregarImagens c) { ima = c; pWidth = w; pHeight = h; setImage(); repaint(); } public void setImage() { String s; for(int i = 0; i <= 20; i++) { s = (String)ima.id.get(i); nome2[i] = ima.getImage(s, i); } } protected void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("Pintar"); int x =50, y = 50; g.setColor(Color.red); g.fillRect(50, 50, pWidth,pHeight); for(int i = 0; i <= 20; i++) { if(x > pWidth - 100) { y = y + 100; x = 50; x1[i] = x; y1[i] = y; } try { g.drawImage(nome2[i], x,y,this); g.dispose(); x = x + 100; x1[i] = x; } catch(Exception e) { e.printStackTrace(); } } } }
Inicial class:
package Codigo; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.image.*; import java.io.File; public class Inicial extends JFrame { private CarregarImagens imagens; public static int pWidth = 800; public static int pHeight = 600; CarregarImagens ima; public Inicial() { super("Escolha uma opção"); Container c = getContentPane(); c.setLayout(null); Splash splash = new Splash(); splash.setVisible(true); imagens = new CarregarImagens("ArquivoImagem.txt"); splash.setVisible(false); setSize(pWidth,pHeight); setBackground(Color.white); setVisible(true); setResizable(true); JPanel superior = new PintarImagem(pWidth,pHeight,imagens); c.add(superior); JPanel inferior = new JPanel(); ImageIcon f01 = new ImageIcon("\\Users\\Fernando\\workspace\\Iniciacao\\src\\Codigo\\00021.gif"); JLabel rotulo1 = new JLabel(); rotulo1.setIcon(f01); rotulo1.setBounds(50,400,90,90); ImageIcon f02 = new ImageIcon("\\Users\\Fernando\\workspace\\Iniciacao\\src\\Codigo\\00031.gif"); JLabel rotulo2 = new JLabel(); rotulo2.setIcon(f02); rotulo2.setBounds(150,400,90,90); ImageIcon f03 = new ImageIcon("\\Users\\Fernando\\workspace\\Iniciacao\\src\\Codigo\\00111.gif"); JLabel rotulo3 = new JLabel(); rotulo3.setIcon(f03); rotulo3.setBounds(650,400,90,90); c.add(rotulo1); c.add(rotulo2); c.add(rotulo3); } private class Splash extends JWindow { public Splash() { Container tela = getContentPane(); tela.setLayout(new FlowLayout(FlowLayout.LEFT,1,1)); ImageIcon imagem = new ImageIcon("\\Users\\Fernando\\workspace\\Iniciacao\\src\\Codigo\\splash.gif"); JLabel rotulo = new JLabel(imagem); tela.add(rotulo); pack(); setLocationRelativeTo(null); } } public static void main(String args[]) { Inicial app = new Inicial(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } thanks
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Feb 22, 2012 18:25:19
0
> but when I draw the images the jvm don´t call paintComponent, why?
it probably does, but it's size might be 0
try adding the indicated line
public PintarImagem(int w, int h, CarregarImagens c) { ima = c; pWidth = w; pHeight = h; setPreferredSize(new Dimension(w,h));//<------------ setImage(); repaint(); }
I agree. Here's the link:
http://aspose.com/file-tools
subject: Why jvm don´t call paintComponent(Graphics g)?
Similar Threads
a very elusive bug
Showing Images in Splash Screen
Need help in ball breaker game
How to transfere ActionListener to anothe Class
applet to change an image when keypressed
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter