Drawing an image and send it to another class
Hi. I need to draw some lines in one class that I have in a program, and then, when I have my lines drawed, I have to send those lines to another class that have the JFrame and put this lines on the content pane to display it.
My first problem is that I dont know how to invoke a method that have Graphics.
Second problem, I the Graphic ready, I dont know how to send it to the other class.
Regards.
[424 byte] By [
karma1234a] at [2007-11-27 4:29:18]

# 1
Short answer: use a BufferedImage to transfer the graphics.
Long answer:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class SendingLines implements ActionListener {
SendingLinesPanel linePanel;
JLabel label;
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if(ac.equals("ADD LINE"))
linePanel.addLine();
if(ac.equals("TRANSFER"))
transferLines();
}
private void transferLines() {
// Make image of the component.
int w = linePanel.getWidth();
int h = linePanel.getHeight();
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2 = image.createGraphics();
// Have linePanel draw itself into this image.
linePanel.paint(g2);
g2.dispose();
// Set the image in local label.
label.setIcon(new ImageIcon(image));
}
private JPanel getCenterPanel() {
linePanel = new SendingLinesPanel();
linePanel.setBorder(BorderFactory.createEtchedBorder());
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createEtchedBorder());
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(linePanel);
panel.add(label);
return panel;
}
private JPanel getLast() {
String[]ids = { "add line", "transfer" };
JPanel panel = new JPanel();
for(int j = 0; j < ids.length; j++) {
JButton button = new JButton(ids[j]);
button.setActionCommand(ids[j].toUpperCase());
button.addActionListener(this);
panel.add(button);
}
return panel;
}
public static void main(String[] args) {
SendingLines test = new SendingLines();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getCenterPanel());
f.getContentPane().add(test.getLast(), "Last");
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}
class SendingLinesPanel extends JPanel {
List<Line2D.Double> lines = new ArrayList<Line2D.Double>();
Random seed = new Random();
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(Line2D line : lines)
g2.draw(line);
}
public Dimension getPreferredSize() {
return new Dimension(300,300);
}
public void addLine() {
int w = getWidth();
int h = getHeight();
int x1 = seed.nextInt(w);
int y1 = seed.nextInt(h);
int x2 = seed.nextInt(w);
int y2 = seed.nextInt(h);
lines.add(new Line2D.Double(x1, y1, x2, y2));
repaint();
}
}