矩形的实际外观不起作用

时间:2014-04-21 17:30:10

标签: java swing drawing drawimage drawrectangle

下面,您可以看到我的代码的缩短版本,其中存在错误。我正在开发一个类似于MS Paint的程序。问题是,当我想绘制时,例如一个矩形,我想看看绘制的矩形实际上是什么样子,然后所有以前的矩形直到最后都由程序绘制。然后,如果查看代码的注释部分并使用它们稍作更改(不使用以前的代码),那么它的工作方式我想要它工作。但是,程序必须将图片保存到PC的内存中,我不希望它以这种方式工作。我是JAVA的初学者,我绝对不知道错误在哪里,因为方法drawImage需要类型Image,这就是我正在做的事情。

希望你能理解我在说什么。如果有机会你不能,那么复制代码就很容易了。当你尝试绘制第一个对象时,你会看到错误。

头等舱:

import java.awt.BorderLayout;
import javax.swing.JFrame;

public class MyPaint extends JFrame{

    private Desk paintDesk;

    public static void main(String[] args) {

        @SuppressWarnings("unused")
        MyPaint frame = new MyPaint();                              
    }   
    MyPaint()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        setBounds(400, 100, 600, 600);
        setResizable(false);
        setLayout(new BorderLayout());

        paintDesk = new Desk();

        this.getContentPane().add(paintDesk,BorderLayout.CENTER);
        setVisible(true);
    }
}

第二课:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;

public class Desk extends JComponent{

    private Image paintedImage;
    private Image newImage;     
    private Graphics2D graphics;    

    private Point a;
    private Point b;

    public Desk()
    {   
        a= new Point();
        b= new Point(); 

        setDoubleBuffered(false);   

        addMouseListener(new MouseAdapter(){            
            public void mousePressed(MouseEvent e){             
                if(SwingUtilities.isLeftMouseButton(e))
                {       
                    /*try {
                        ImageIO.write((RenderedImage)paintedImage, "JPG", new File("actual.jpg"));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }*/
                    newImage= paintedImage;
                    a.x = e.getX();
                    a.y = e.getY();             
                }                       
            }
        }); 

        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){         
                if(SwingUtilities.isLeftMouseButton(e))
                {   
                    /*try {
                        newImage= (ImageIO.read(new File("actual.jpg")));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }*/

                    if(newImage!=null)
                    {
                        graphics.drawImage(newImage,0,0,null);
                        repaint();
                    }

                    b.x = e.getX();
                    b.y = e.getY();
                    if(graphics != null)
                    {
                        drawRect();
                    }                       
                    repaint();      
                }               
            }           
        }); 

        addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e){        
                if(SwingUtilities.isLeftMouseButton(e))     
                {                       
                    b.x = e.getX();         
                    b.y = e.getY();                 
                    if(graphics != null)
                    {                       
                        drawRect();     
                    }                   
                    repaint();                                  
                }               
            }
        }); 
    }   
    public void paintComponent(Graphics g){             
        if(paintedImage == null)
        {
            paintedImage = createImage(getSize().width, getSize().height);
            graphics = (Graphics2D)paintedImage.getGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
            clear();        
        }       
        g.drawImage(paintedImage, 0, 0, null);
    }   
    private void clear()
    {       
        graphics.setPaint(Color.white);
        graphics.fillRect(0, 0, getSize().width, getSize().height);
        graphics.setPaint(Color.red);
        repaint();
    }   
    private void drawRect()
    {
        graphics.drawLine(a.x, a.y, b.x, a.y);
        graphics.drawLine(a.x, a.y, a.x, b.y);
        graphics.drawLine(a.x, b.y, b.x, b.y);
        graphics.drawLine(b.x, a.y, b.x, b.y);
    }
}

2 个答案:

答案 0 :(得分:0)

总是在图像上绘画的基本方法是错误的。您不想在图像上绘制矩形,直到用户释放鼠标按钮。在此之前,您需要在paintComponent()方法中的组件上绘制一个临时矩形。

有关使用上述方法的工作示例,请参阅Custom Painting Approaches中的DrawOnImage示例。

答案 1 :(得分:0)

解决这个特定问题的方法是,我在变量newImage中只保存了对变量paintedImage的引用,而不是特定的图像。

newImage变量必须声明为BufferedImage而不是代码

newImage = paintedImage;

必须是这段代码:

newImage = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_RGB);
Graphics g = newImage.createGraphics();
g.drawImage(paintedImage, 0, 0, null);