Wyświetlanie obiektu BufferedImage za pomocą Graphics

0

Witam

Próbuję wykonać kod, który ma wyświetlać obiekt BufferedImage za pomocą Graphics.

 txf=new TeXFormula(frameNote.getTextArea().getSelectedText());
		TeXIcon icon = txf.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
		icon.setInsets(new Insets(5, 5, 5, 5));
		BufferedImage image = new BufferedImage(icon.getIconWidth(),
				icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = image.createGraphics();
		g2.setColor(Color.white);
		g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
		
		JLabel jl = new JLabel();
		jl.setForeground(new Color(0, 0, 0));
		icon.paintIcon(jl, g2, 100, 100);
			
		Graphics g = this.contentPane.getGraphics();
		g.drawImage(image,0,0,null);

Jednak nie wyświetla mi się. Co jest źle? Proszę o wskazówki.

dodanie znacznika <code class="java"> - Furious Programming

0

Rysowanie na ekran musi być w metodzie o sygnaturze

public void paintComponent(Graphics g)

lub

public void paint(Graphics g)

http://zetcode.com/gfx/java2d/

0

OK, ale rozważając taki kod

public class Example1 {
    public static void main(String[] args) {
	
	String latex = "\foo";
	
	TeXFormula formula = new TeXFormula(latex);
	TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
	icon.setInsets(new Insets(5, 5, 5, 5));
	
	BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
	Graphics2D g2 = image.createGraphics();
	g2.setColor(Color.white);
	g2.fillRect(0,0,icon.getIconWidth(),icon.getIconHeight());
	JLabel jl = new JLabel();
	jl.setForeground(new Color(0, 0, 0));
	icon.paintIcon(jl, g2, 0, 0);
	File file = new File("Example1.png");
	try {
	    ImageIO.write(image, "png", file.getAbsoluteFile());
	} catch (IOException ex) { }
    }    
} 

normalnie to się wyświetla i bez metody paintComponent

dodanie znacznika <code class="java"> - Furious Programming

0

Co się wyświetla? W powyższym kodzie nie ma nawet próby wyświetlenia zmiennej image.

0

Aj, mój błąd. jeszcze raz ten sam poprawnie wklejony kod.

public class LatexExample extends JFrame implements ActionListener {
	
	private JTextArea latexSource;
	private JButton btnRender;
	private JPanel drawingArea;

	public LatexExample() {
		this.setTitle("JLatexMath Example");
		this.setSize(500, 500);
		Container content = this.getContentPane();
		content.setLayout(new GridLayout(2, 1));
		this.latexSource = new JTextArea();
		
		JPanel editorArea = new JPanel();
		editorArea.setLayout(new BorderLayout());
		editorArea.add(new JScrollPane(this.latexSource),BorderLayout.CENTER);
		editorArea.add(btnRender = new JButton("Render"),BorderLayout.SOUTH);		
		
		content.add(editorArea);
		content.add(this.drawingArea = new JPanel());		
		this.btnRender.addActionListener(this);
		
		this.latexSource.setText("x=\\frac{-b \\pm \\sqrt {b^2-4ac}}{2a}");
	}

	public void render() {
		try {
			// get the text
			String latex = this.latexSource.getText();
			
			// create a formula
			TeXFormula formula = new TeXFormula(latex);
			
			// render the formla to an icon of the same size as the formula.
			TeXIcon icon = formula
					.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);
			
			// insert a border 
			icon.setInsets(new Insets(5, 5, 5, 5));

			// now create an actual image of the rendered equation
			BufferedImage image = new BufferedImage(icon.getIconWidth(),
					icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g2 = image.createGraphics();
			g2.setColor(Color.white);
			g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
			JLabel jl = new JLabel();
			jl.setForeground(new Color(0, 0, 0));
			icon.paintIcon(jl, g2, 0, 0);
			// at this point the image is created, you could also save it with ImageIO
			
			// now draw it to the screen			
			Graphics g = this.drawingArea.getGraphics();
			g.drawImage(image,0,0,null);
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
					JOptionPane.INFORMATION_MESSAGE);		
		}

	}

	public static void main(String[] args) {
		LatexExample frame = new LatexExample();		
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if( e.getSource()==this.btnRender ) {
			render();
		}
		
	}
}
0

Chodzi Ci o ten wiersz?

g.drawImage(image,0,0,null);

Jeżeli pojawia się jakiś obrazek na ekranie, to zminimalizuj okno (zrzuć na pasek) i przywróć.

0

Fakt, po minimalizacji już się obrazek nie wyświetla. Ale pytanie: przerobiłem kod tak aby wyświetlanie grafiki wykonywało się w paintComponent

public PanelWzor(Podglad p) 
	{
		podglad=p;
		image = new BufferedImage(podglad.getIcon().getIconWidth(),
				podglad.getIcon().getIconHeight(), BufferedImage.TYPE_INT_ARGB);
		
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = image.createGraphics();
		g2.setColor(Color.white);
		g2.fillRect(0, 0, podglad.getIcon().getIconWidth(), podglad.getIcon().getIconHeight());
		
		JLabel jl = new JLabel("");
		jl.setForeground(new Color(0, 0, 0));
		podglad.getIcon().paintIcon(jl, g2, 100, 100);
		g.drawImage(image,120,120,null);
	}

Jednak wyświetla się tylko biały rect, nie ma obiectu image (sprawdziłem system.out.println czy aby na pewno obiekt ten nie jest nullem i nie jest).

0

Przecież obiekt image jest pusty.

0

A możesz konkretnie wskazać co należy poprawić?

0

Nie mam pojęcia co chcesz uzyskać.

0

Chcę aby obiekt image został wyswietlony na białym recu. Ty twierdzisz że image jest pusty, wiec jak mam to poprawić. Przecież inicjuje go w konstruktorze.

0

Ten kod:

Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0,podglad.getIcon().getIconWidth(), podglad.getIcon().getIconHeight());

powoduje, że image (w pamięci) jest białym prostokątem, a ten kod

g.drawImage(image,120,120,null);

wyświetla ten biały prostokąt. Czego więcej od tego kodu oczekujesz?

1 użytkowników online, w tym zalogowanych: 0, gości: 1