java中怎么把当前类的Graphics设置为自己的Graphics对象?
就是把getGraphics对象替换为自己的Graphics对象。bufferedImageBimg=newBufferedImage(600,600,BufferedI...
就是把getGraphics对象替换为自己的Graphics对象。
bufferedImage Bimg=new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
Graphics g=Bimg.getGraphics();
public void init() {
super.init();
g=Bimg.getGraphics();
JMenu menu=new JMenu("文件");
JMenuItem JMenuSave=new JMenuItem();
menu.setSize(100,16);
JMenuSave.setText("保存");
JMenuSave.setSize(100,13);
JMenuSave.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("*.jpg","jpg");
chooser.setFileFilter(filter);
int result=chooser.showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION){
File imgFile=chooser.getSelectedFile();
try {
ImageIO.write(Bimg, "jpg", imgFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} } });
menu.add(JMenuSave);
JMenuBar menuBar=new JMenuBar();
menuBar.add(menu);
add(menuBar);
//整块儿画布
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setSize(600,600);
setBackground(Color.white);
-------------
用this.paint(g), 画得图像看不到,但是可以保存,怎么把g设置为显示的呢? 展开
bufferedImage Bimg=new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
Graphics g=Bimg.getGraphics();
public void init() {
super.init();
g=Bimg.getGraphics();
JMenu menu=new JMenu("文件");
JMenuItem JMenuSave=new JMenuItem();
menu.setSize(100,16);
JMenuSave.setText("保存");
JMenuSave.setSize(100,13);
JMenuSave.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("*.jpg","jpg");
chooser.setFileFilter(filter);
int result=chooser.showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION){
File imgFile=chooser.getSelectedFile();
try {
ImageIO.write(Bimg, "jpg", imgFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} } });
menu.add(JMenuSave);
JMenuBar menuBar=new JMenuBar();
menuBar.add(menu);
add(menuBar);
//整块儿画布
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setSize(600,600);
setBackground(Color.white);
-------------
用this.paint(g), 画得图像看不到,但是可以保存,怎么把g设置为显示的呢? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询