Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drawImage in paint of JComponent

Status
Not open for further replies.

Toyman

Programmer
Jun 19, 2001
68
0
0
GB
Hi

I've got a JComponent which I use as a TableCellRenderer.
The problem is that the Image is not drawn to the Graphics object of the paint method. Only when the form is resized is the Image shown. If the image change it is not shown at all. (remove //loadPicture((String)value); in the getTableCellRendererComponent method of the TestCellRenderer class)
I've used other methods on the JComponent just to see if it is working... all the other methods works fine... like the drawLine, drawString ...... but not the drawImage.

Any ideas ?

Here is the code

Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TestCellRenderer extends JComponent implements TableCellRenderer{

	Image img;
	ChangeListener cl;
		
	public TestCellRenderer(){
		try{
			loadPicture("ac1.gif");
			setSize(new Dimension(100,100));
			this.setPreferredSize(new Dimension(100,100));
			this.setBorder(new LineBorder(Color.RED));
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
		//loadPicture((String)value);
		this.updateUI();
		return this;
	}
	
	public Image getImage(){
		return img;
	}
	
	public void setImage(Image img){
		this.img = img;
	}
	
	public void addChangeListener(ChangeListener l){
		cl = l;
	}
	
	private void loadPicture(String path){
		try{
			//if (img != null){
			//	img.flush();
			//	img = null;
			//}

			img = Toolkit.getDefaultToolkit().createImage(path);
			if (cl != null){
				cl.stateChanged(new ChangeEvent(this));
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	public void paint(Graphics graphics){
		Graphics2D g = (Graphics2D)graphics;
		try{
			g.drawLine(0,0,getWidth(),getHeight());
			g.drawImage(img,0,0,getWidth(),getHeight(),null);
			g.setColor(Color.RED);
			g.drawLine(getWidth(),0,0,getHeight());
			super.paint(g);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	

}

Code:
import javax.swing.table.*;

public class TestModel extends DefaultTableModel{
	
	public TestModel(){
		this.addColumn("Image Column");
		this.addColumn("Column Two");
	}
}
Code:
import java.awt.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;

class TestPaintInTable extends JFrame{

	private static TestCellRenderer viewer = new TestCellRenderer();

	
	public static void main (String args[]){
		TestPaintInTable frame = new TestPaintInTable();
		frame.setSize(300,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		TestModel model = new TestModel();
		//Data
		Vector vecData = new Vector();
		vecData.add("ac.gif");
		vecData.add("Aircraft One");
		model.addRow(vecData);
		vecData.removeAllElements();
		vecData.add("ac1.gif");
		vecData.add("Aircraft Two");
		model.addRow(vecData);
		
		
		
		TestTable testTable = new TestTable(model);

		TableColumn col = testTable.getColumnModel().getColumn(0);
		TestCellRenderer tCR = new TestCellRenderer();
		tCR.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e){
				System.out.println("State has changed. Image = " + ((TestCellRenderer)e.getSource()).getImage() );
				viewer.setImage(((TestCellRenderer)e.getSource()).getImage());
				viewer.update(viewer.getGraphics());
			}
 			
		});
		col.setCellRenderer(tCR);
		
		JScrollPane scr = new JScrollPane(testTable);
		
		frame.setLayout(new BorderLayout());
		frame.getContentPane().add(viewer,BorderLayout.NORTH);
		frame.getContentPane().add(scr,BorderLayout.CENTER);
		frame.setVisible(true);
	}
}
Code:
import javax.swing.*;
import javax.swing.table.*;

public class TestTable extends JTable{
	
	public TestTable(DefaultTableModel model){
		super(model);
	}
}

Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk
 
never, never, never do this:

Code:
catch (Exception e)
But catch specific exceptions.

For an OutOfMemoryException a printStackTrace () isn't possible.
For someone looking at the code, I don't have any Idea what kind of Exception you're expecting - OutOfMemoryException? ArrayIndexOutOfBounds? FileNotFound? MSwormOrVirusDetected?

But to the (re-)paint Problem I don't have a valuable feedback.

seeking a job as java-programmer in Berlin:
 
Stefan, I would be interested why specifically you would not catch the generic Exception class ?

--------------------------------------------------
Free Database Connection Pooling Software
 
For the reasons I mentioned above:
- unclear, which exception is expected to readers/ maintainers
- probably catching an exception, you didn't expect, and therefore reacting wrong.

Of course there are rare cases, where you catch 'Exception', to convert it into a own kind of exception, and throw your own one, i.e. for automated logging etc..
As advanced programmer, building an API for example.

You don't agree?

seeking a job as java-programmer in Berlin:
 
Dudes

Thanks for the replies. This is just a demo app to demonstrate the PROBLEM I'am having. Stefan you are right one should try a catch the exceptions...

Could we please stay with the original problem :)
Still no solution !!!!

Cheers

Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk
 
Okay I've changed the drawImage to :

g.drawImage(img,0,0,getWidth(),getHeight(),this.getParent());

Now the top Image works ... but the images in the table still doesn't work.

Please help !!!!!!!!!!

Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top