drawing lines in JPanel
Hi everyone in the forum,
i have a problem i am stuck with:
i am developing an interface with some internal frames (children for a main window)
One of the parts of it should be to show some graphics so i have created anotrer class which returns a panel to paint them, but it is not working, and i do not get why. MOreover in the code submitted g is null. Why?
Any idea or suggestion?
Thanks in advance
publicclass mInterfaceextends JPanel{
public JComponent openGraphic(){
JLabel labelX;
JLabel labelY;
Border border = BorderFactory.createTitledBorder("Component");
JPanel panel =new JPanel(new GridLayout(10, 2));
panel.setBorder(border);
labelX =new JLabel("X= ");
panel.add(labelX);
labelY =new JLabel("Y= ");
panel.add(labelY);
Graphics g = panel.getGraphics( );
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(50,50,200,200);
[1345 byte] By [
patucosa] at [2007-10-2 20:53:05]

> Hi everyone in the forum,
> i have a problem i am stuck with:
> i am developing an interface with some internal
> frames (children for a main window)
> One of the parts of it should be to show some
> graphics so i have created anotrer class which
> returns a panel to paint them, but it is not working,
> and i do not get why. MOreover in the code submitted
> g is null. Why?
Because you can't call getGraphics() from an arbitrary place in your code and expect it to return something meaningful.
Painting methods such as drawLine() should be called only from the paintComponent() method (or from some method called by paintComponent() which passes along the Graphics object originally passed to paintcomponent itself).
Read:
http://java.sun.com/products/jfc/tsc/articles/painting/index.html
Thanks for the reply,
i have tryed the idea but still becoming a null pointer. i do not get why
public JComponent openGraphic(){
JLabel labelX;
JLabel labelY;
Border border = BorderFactory.createTitledBorder("Component");
JPanel panel = new JPanel(new GridLayout(10, 2));
panel.setMinimumSize(new Dimension(100, 550));
panel.setPreferredSize(new Dimension(100, 550));
panel.setBorder(border);
labelX = new JLabel("X= ");
panel.add(labelX);
labelY = new JLabel("Y= ");
panel.add(labelY);
paintComponent(this.getGraphics( ));
return panel;
}
public void paintComponent(Graphics g){
super.paintComponent(g);// erases background
Graphics2D g2 = (Graphics2D)g;//cast for java2
// my graphics:
g2.setColor(new Color(255,0,0));
g2.fillRect(10,10,200,50);
g2.setColor(new Color(0,0,0));
g2.drawString("Hello World", 10, 10);
}
> Thanks, after dealing a while a was concious what i
> was doing. Moreover the article you attached shows
> clearly how to solve my prob.
>
> But another question: how can be the zooming in and
> out done?
Several ways. Here's one:
=================
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ZoomTest {
ZoomPanel zp;
ZoomTest() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
zp = new ZoomPanel(1.0);
f.add(zp, BorderLayout.CENTER);
JButton zoomInButton = new JButton(new AbstractAction("Zoom In") {
public void actionPerformed(ActionEvent ae) {
zp.setZoomFactor(zp.getZoomFactor() + 0.1);
}
});
JButton zoomOutButton = new JButton(new AbstractAction("Zoom Out") {
public void actionPerformed(ActionEvent ae) {
zp.setZoomFactor(zp.getZoomFactor() - 0.1);
}
});
f.add(zoomInButton, BorderLayout.NORTH);
f.add(zoomOutButton, BorderLayout.SOUTH);
f.setSize(400,300);
f.setVisible(true);
}
public static void main(String[] args) {
new ZoomTest();
}
private static class ZoomPanel extends JPanel {
private double zoomFactor;
private AffineTransform zoomTransform;
private Image image;
public ZoomPanel(double zoomFactor) {
this.zoomFactor = zoomFactor;
zoomTransform = AffineTransform.getScaleInstance(zoomFactor, zoomFactor);
try {
image = ImageIO.read(new File("C:\\TEMP\\test.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
void setZoomFactor(double zoomFactor) {
if (zoomFactor < 0) {
zoomFactor = 1.0;
}
this.zoomFactor = zoomFactor;
zoomTransform = AffineTransform.getScaleInstance(zoomFactor, zoomFactor);
repaint();
}
double getZoomFactor() {
return this.zoomFactor;
}
protected void paintComponent(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
if (image != null) {
Graphics2D g2d = (Graphics2D)g;
g2d.setTransform(zoomTransform);
g2d.drawImage(image,0,0,this);
}
}
}
}
>
> Thanks