我的代码错在哪里???
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class myPaint extends JFrame
{
private int width = 400;
private int height = 400;
public Canvas canvas = new Canvas();
public void createFrame()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Paint by bugs&bugs ltd.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupFrame(frame.getContentPane());
frame.pack();
frame.setSize(width,height);
frame.setVisible(true);
}
public void setupFrame(Container pane)
{
pane.setLayout(new BorderLayout());
pane.add(new canvas(), BorderLayout.CENTER);
pane.add(new button(), BorderLayout.SOUTH);
}
public class canvas extends Canvas
{
public canvas()
{
setBackground(Color.white);
}
public void paint(Graphics g)
{
g.drawLine(10,10,100,10);
}
}
public class button extends JPanel implements ActionListener
{
final myPaint frame = new myPaint();
final canvas c = new canvas();
public button()
{
JButton b = new JButton("Save");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
try
{
FileDialog fd = new FileDialog(frame, "Save as PNG", FileDialog.SAVE);
fd.setFile("canvas.png");
fd.show();
String name = fd.getFile();
Rectangle r = c.getBounds();
Image image = c.createImage(r.width,r.height);
Graphics g = image.getGraphics();
c.paint(g);
ImageIO.write((RenderedImage)image, "png", new File(name));
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public static void main(String[] args)
{
myPaint objCreate = new myPaint();
objCreate.createFrame();
}
}
问题点数:100、回复次数:4Top
1 楼wellingsok(不及格的程序员)回复于 2003-12-01 15:58:57 得分 0
Don't mix AWT components and Swing components in a same program, and Please refer this site and study its java.awt.image section and javax.imageio section.
Top
2 楼wellingsok(不及格的程序员)回复于 2003-12-01 16:01:34 得分 100
sorry, lost the website . http://javaalmanac.com/ .Top
3 楼kypfos(不在寻梦)回复于 2003-12-01 16:22:43 得分 0
下面的代码可以,就是要注意保持引用的正确性。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class myPaint extends JFrame {
private int width = 400;
private int height = 400;
public Canvas canvas = new canvas();
public void createFrame() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Paint by bugs&bugs ltd.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupFrame(frame.getContentPane());
frame.pack();
frame.setSize(width, height);
frame.setVisible(true);
}
public void setupFrame(Container pane) {
pane.setLayout(new BorderLayout());
pane.add(canvas, BorderLayout.CENTER);
pane.add(new button(), BorderLayout.SOUTH);
}
public class canvas extends Canvas {
public canvas() {
setBackground(Color.white);
}
public void paint(Graphics g) {
g.drawLine(10, 10, 100, 10);
}
}
public class button extends JPanel implements ActionListener {
//final myPaint frame = new myPaint();
//final canvas c = new canvas();
public button() {
JButton b = new JButton("Save");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
try {
FileDialog fd =
new FileDialog(myPaint.this, "Save as PNG", FileDialog.SAVE);
fd.setFile("canvas.png");
fd.show();
String name = fd.getFile();
Rectangle r = canvas.getBounds();
Image image = canvas.createImage(r.width, r.height);
Graphics g = image.getGraphics();
canvas.paint(g);
ImageIO.write((RenderedImage) image, "png", new File(name));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main(String[] args) {
myPaint objCreate = new myPaint();
objCreate.createFrame();
}
}Top
4 楼kypfos(不在寻梦)回复于 2003-12-01 16:46:09 得分 0
fuck,明显示是倒分贴,同一个人
又一个没公德的人。Top




