我怎样才能让这个Paintcomponent按照我的意图行事?

时间:2014-02-23 23:52:12

标签: java swing user-interface paintcomponent jcomponent

我获得了在java中创建一个简单的绘图程序的任务,该程序使用GUI并具有基本的I / O功能。这就是我教授告诉我做的一切。但是,之前我只做了一个GUI程序,所以直接进入这个绘图程序一直很头疼。现在我差不多完成了,但程序的表现并不像我预期的那样。在Panel上绘制新对象时,它们会在其下方的对象上绘制不可见的白色矩形,以擦除这些对象。我认为这是重绘的结果(xMin,yMin,xMax - xMin + 1,yMax - yMin + 1); DrawShapes中的方法,但无法想到修复它的方法。

另一方面,对象也没有正确保存。我可以按照我的意图导出jpg,但是,它只会导出最后绘制的图像而不是paintComponent画布上的所有图像。

最后,DrawShapes中的clear方法工作方式非常相似。当激活clear方法时,除了最后绘制的图像外,它将清除所有内容。

是否有人比我更熟悉这些工具可以找到解决这些问题的方法?这只是我用过的第一个程序和I / O.

以下是应该绘制形状的面板类:

/**
 * @author me
 */
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

public class DrawShapes extends JPanel{
    Point startPoint = null;
    Point endPoint = null;
    public int drawType = 1;
    BufferedImage image;
    Graphics2D g2d;

public DrawShapes(){
    setBackground(Color.WHITE);

    MyMouseListener m1 = new MyMouseListener();
    addMouseListener(m1);
    addMouseMotionListener(m1);
}//end constructor

//sets draw type, which is the decider of what is being drawn.
public void setType(int type){
    if(type == 1)
    {
        drawType = 1;
    }
    else if(type == 2)
    {
        drawType = 2;
    }
    else if(type == 3)
    {
        drawType = 3;
    }
}//end setType


    public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    if (image == null)
    {
        createEmptyImage();
    }

    g.drawImage(image, 0, 0, null);

    if (startPoint != null && endPoint != null)
    {
            int x = Math.min(startPoint.x, endPoint.x);
            int y = Math.min(startPoint.y, endPoint.y);
            int width = Math.abs(startPoint.x - endPoint.x);
            int height = Math.abs(startPoint.y - endPoint.y);
            switch (drawType)
                {
                    case 1:
                        g.drawRect(x, y, width, height);
                        break;
                    case 2:
                        g.drawOval(x, y, width, height);
                        break;
                    case 3:
                        g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
                        break;
                }
    }
}//end paintComponent

public void save()
{
    BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    this.paint(g);
    g.dispose();
    try{ImageIO.write(bi, "png",new File("test.png"));
    }catch (Exception e){}
    }

private void createEmptyImage()
{
    image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    g2d = (Graphics2D)image.getGraphics();
    g2d.setColor(Color.BLACK);
    g2d.drawString("Add a shape by clicking and dragging.", 40, 15);
}

public void addRect(int x, int y, int width, int height, Color color)
{
       g2d.setColor( color );
       g2d.drawRect(x, y, width, height);
       repaint();
}

public void addOval(int x, int y, int width, int height, Color color)
{
       g2d.setColor( color );
       g2d.drawOval(x, y, width, height);
       repaint();
}

public void addLine(int x1, int y1, int x2, int y2, Color color)
{
        g2d.setColor(color);
        g2d.drawLine(x1, y1, x2, y2);
        repaint();
}

public void clear()
{
       createEmptyImage();
       repaint();
}

class MyMouseListener extends MouseInputAdapter
{
    private int xMin;
    private int xMax;
    private int yMin;
    private int yMax;

    public void mousePressed(MouseEvent e)
    {
           startPoint = e.getPoint();
           endPoint = startPoint;
           xMin = startPoint.x;
           xMax = startPoint.x;
           yMin = startPoint.y;
           yMax = startPoint.y;
    }

    public void mouseDragged(MouseEvent e)
    {
        //This is code I found that should make it so the only area affected by the dragging is repainted.
        endPoint = e.getPoint();
        xMin = Math.min(xMin, endPoint.x);
        xMax = Math.max(xMax, endPoint.x);
        yMin = Math.min(yMin, endPoint.y);
        yMax = Math.max(yMax, endPoint.y);
        repaint(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);

    }

    public void mouseRelease(MouseEvent e)
    {
        //This code paints the shapes on the Buffered Image created as a canvas
        int x = Math.min(startPoint.x, endPoint.x);
        int y = Math.min(startPoint.y, endPoint.y);
        int width = Math.abs(startPoint.x - endPoint.x);
        int height = Math.abs(startPoint.y - endPoint.y);

        if (width != 0 || height != 0)
    {
        g2d.setColor( e.getComponent().getForeground() );
//      g2d.drawRect(x, y, width, height);
                switch (drawType)
                {
                    case 1:
                        addRect(x, y, width, height, e.getComponent().getForeground());
                        break;
                    case 2:
                        addOval(x, y, width, height, e.getComponent().getForeground());
                        break;
                    case 3:
                        addLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, e.getComponent().getForeground());
                        break;
                }//end switch statement.
    }

    startPoint = null;
//  repaint();
    }
}

}//end class

以下是用户界面的代码:

/*@author Me*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyDrawUI extends JFrame
{
    private DrawShapes draw = new DrawShapes();
    private JPanel ButtonPanel = new JPanel();
    private JFrame window = new JFrame("Draw!");
    //constructor
    MyDrawUI(){
        buildUI();
    }

 void buildUI()
 {
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     window.setLayout(new GridLayout(2,2));
     window.add(draw);
     window.add(ButtonPanel, BorderLayout.SOUTH);
     ButtonPanel.setBackground(Color.LIGHT_GRAY);
     draw.setBackground(Color.WHITE);

     //define buttons
     JButton rectangle = new JButton("Rectangle");
     JButton oval = new JButton("Oval");
     JButton line = new JButton("Line");
     JButton exit = new JButton("Exit");
     JButton save = new JButton("Save");
     JButton clear = new JButton("Clear");

     //add buttons
     ButtonPanel.add(rectangle, BorderLayout.SOUTH);
     ButtonPanel.add(oval, BorderLayout.SOUTH);
     ButtonPanel.add(line, BorderLayout.SOUTH);
     ButtonPanel.add(clear, BorderLayout.SOUTH);
     ButtonPanel.add(save, BorderLayout.SOUTH);
     ButtonPanel.add(exit, BorderLayout.SOUTH);
     ButtonPanel.setSize(100, 100);

     save.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
         draw.save();
         }
     });

     clear.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
         draw.clear();
         }
     });

     rectangle.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
             draw.setType(1);
         }
     });

     oval.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
             draw.setType(2);
         }
     });

     line.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
             draw.setType(3);
         }
     });

     exit.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e)
         {
             System.exit(0);
         }
     });

     window.setVisible(true);
     window.setSize(1024, 800);
 }
}

1 个答案:

答案 0 :(得分:3)

我可以看到一些问题,主要的一个问题是你"思考"您已在MouseAdaptor课程中覆盖了某个方法,但是没有

mouseRelease不是导致任何事件触发它的方法。您之后使用的方法是mouseReleased

当重写方法时,使用@Override注释,如果您认为"思考"你在任何父类中都没有覆盖。

@Override
public void mouseReleased(MouseEvent e) {

突然出现了其他一些事情。

  1. 您的MyDrawUI类来自JFrame,但您创建了另一个名为JFrame window的实例,您可以在其上创建您的用户界面。在这种情况下,请从extends JFrame类中删除MyDrawUI,因为它只会增加混淆......
  2. 在此上下文中,建议保留对Graphics上下文的引用,即使是您创建的上下文。在某些系统上,在您调用dispose之前,可能不会将任何内容提交给底层实现。相反,只需在需要时使用image.getGraphics,并在完成后致电g2d.dispose