使用java swing绘图笔画...笔画不显示

时间:2016-10-06 16:26:38

标签: java swing mouse pen

所以我在这里有两节课:

PhotoComponent类:

(这个类是作为JComponent处理特定图像。当“翻转”时我想绘制笔划而不是图像。所以我用矩形替换图像,试图在它上面绘制笔划。)

public class PhotoComponent extends JComponent {

private Image pic;
private boolean flipped;

private int contentAreaWidth;
private int contentAreaHeight;
p
@Override
public void paintComponent(Graphics g) {
    //Does all the drawing and contains whatever state information is associated with the photo
    //create an action event to auto call repaint
    //call repaint anytime flip was changed to true or false

    System.out.println("Draw: " + draw + ", Pic: " + pic);
    if (draw && pic != null) {
        super.paintComponent(g);

        System.out.println("width using this: " + this.getWidth() + ", actual width of JPanel: " + contentAreaWidth);
        System.out.println("height using this: " + this.getHeight() + ", actual height of JPanel: " + contentAreaHeight);

        g2 = (Graphics2D) g;
        int x = (contentAreaWidth - pic.getWidth(null)) / 2;
        int y = (contentAreaHeight - pic.getHeight(null)) / 2;
        if (!flipped) {
            g2.drawImage(pic, x, y, null);

        } else if (flipped) {
            g2.setColor(Color.WHITE);
            g2.fillRect(x,y,pic.getWidth(null), pic.getHeight(null));
            g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            if (drawingMode) {
                g2.setPaint(Color.RED);
                if (drawOval) {
                    penStrokes.put(ovalX, ovalY);
                    if (penStrokes != null) {
                        for (Integer xCoor : penStrokes.keySet()) {
                            g2.setPaint(Color.RED);
                            int brushSize = 5;
                            g2.fillOval((xCoor - (brushSize / 2)), (penStrokes.get(xCoor) - (brushSize / 2)), brushSize, brushSize);
                            //System.out.println("SIZE OF HASHTABLE: " + penStrokes.size());
                        }
                    }
                    System.out.println("Filling an oval!" + ovalX + ", " + ovalY);
                }
            } else if (textMode) {
                g2.setPaint(Color.YELLOW);
                if (drawRect) {
                    rectDimensions.add(rectX);
                    rectDimensions.add(rectY);
                    rectDimensions.add(rectWidth);
                    rectDimensions.add(rectHeight);

                    for (int i = 0; i < rectDimensions.size(); i+=4) {
                        g2.fillRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
                        g2.drawRect(rectDimensions.get(i), rectDimensions.get(i+1), rectDimensions.get(i+2), rectDimensions.get(i+3));
                    }
                }
            }

            System.out.println("This is being called again!");
        }

    }

}
public void setRectangle(int x, int y, int width, int height) {
    drawRect = true;
    rectX = x;
    rectY = y;
    rectWidth = width;
    rectHeight = height;
}

public void removeRectangle() {
    drawRect = false;
}

public int[] setOval(int currentX, int currentY) {
    drawOval = true;
    int[] toReturn = {ovalX, ovalY};
    ovalX = 

注意上面的DRAWLINE()方法。我正在绘制给定的点,重新绘制,并将旧变量设置为当前变量。

主要课程:

private static PhotoComponent img;
private static JFrame frame;

private static JPanel contentArea;

//Mouse Coordinates for PenStrokes
private static int oldX, oldY;

//Mouse Coordinates for Sticky Notes
private static Point clickPoint;

public static void main (String[] args) {
    frame = new JFrame("PhotoManip");
    img = null;
    contentArea = null;
    oldX = 0;
    oldY = 0;
    setupMenubar(frame);
    setupJFrame(frame);
}

private static void addPhotoComponent(File file) {

            }
            if (img.getTextMode()) {
                img.removeRectangle();
                clickPoint = null;
            }

        }
    });

    img.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (img.getDrawingMode()) {
                if (withinRange(e.getX(), e.getY())) {
                    int[] toUpdate = img.setOval(e.getX(), e.getY());
                    oldX = toUpdate[0];
                    oldY = toUpdate[1];
                    img.repaint();
                }
            }
            if (img.getTextMode()) {
                if (withinRange(e.getX(), e.getY())) {
                    Point dragPoint = e.getPoint();
                h, height);
                    img.repaint();
                }
            }

        }
    });

    if (img!=null) {
        contentArea.add(img);
    }
}

private static boolean withinRange(int x, int y) {
    if (x > img.getX() && x < img.getX() + img.getWidth()) {
        if (y > img.getY() && y < img.getY() + img.getHeight()) {
            return true;
        }
    }
    return false;
}

private static void flipImage() {
    if (!img.isFlipped()) {
        img.setFlipped(true);
    } else if (img.isFlipped()) {
        img.setFlipped(false);
    }
}
当发生mousedrag时,在此主类中调用

drawLine()。问题是笔画似乎没有显示出来。

我知道该程序正在调用g2.fillOval(),因为我之后打印出一个验证语句。

此外,我已经创建了打印语句,用于按下和拖动鼠标并获得正确的坐标?

为什么不出现红色笔划?我糊涂了。这是我的代码结构的方式吗?

1 个答案:

答案 0 :(得分:2)

问题的关键在于你试图在paintComponent方法之外绘制一些从不支持的方法。无论你画什么都会被paintComponent的下一次调用覆盖,这几乎会立刻发生。我们可以通过存储椭圆的坐标并将其绘制在paintComponent内而不是尝试在paintComponent方法之外的图形对象上绘制来解决此问题。请参阅以下代码:

首先,我们要将以下变量添加到PhotoComponent类:

private boolean drawOval = false;
private int ovalX = 0;
private int ovalY = 0;

然后我们将添加控制它们的方法:

public int[] setOval(int currentX, int currentY) {
    drawOval = true;
    int[] toReturn = {ovalX, ovalY};
    ovalX = currentX;
    ovalY = currentY;
    return toReturn;
}

public void removeOval() {
    drawOval = false;
}

之后我们可以更改paintComponent方法,让它根据这些变量绘制椭圆:

@Override
public void paintComponent(Graphics g) {
    //Does all the drawing and contains whatever state information is associated with the photo
    //create an action event to auto call repaint
    //call repaint anytime flip was changed to true or false
    super.paintComponent(g);
    g2 = (Graphics2D) g;
    int x = (contentAreaWidth - pic.getWidth(null)) / 2;
    int y = (contentAreaHeight - pic.getHeight(null)) / 2;
    if (!flipped) {
        g2.drawImage(pic, x, y, null);

    } else if (flipped) {
        g2.setColor(Color.WHITE);
        g2.fillRect(x, y, pic.getWidth(null), pic.getHeight(null));
        g2.drawRect(x, y, pic.getWidth(null), pic.getHeight(null));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.RED);
    }

    //took the code you already used for drawing the oval and moved it here
    if (drawOval) {
        g2.setPaint(Color.RED);
        int brushSize = 5;
        g2.fillOval((ovalX - (brushSize / 2)), (ovalY - (brushSize / 2)), brushSize, brushSize);
    }
}

最后更改addPhotoComponent方法以更新这些变量,而不是直接尝试绘制椭圆:

private static void addPhotoComponent(File file) {
    Image image = null;
    try {
        image = ImageIO.read(file);
    } catch (IOException e2) {
        System.out.println("ERROR: Couldn't get image.");
    }

    img = new PhotoComponent(image, contentArea);
    img.revalidate();

    img.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                // your code here
                System.out.println("You flipped the photo!!!");
                flipImage();
                img.repaint();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            img.setOval(e.getX(), e.getY());
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            img.removeOval();
        }
    });

    img.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            int[] toUpdate = img.setOval(e.getX(), e.getY());
            oldX = toUpdate[0];
            oldY = toUpdate[1];
        }
    });

    if (img != null) {
        contentArea.add(img);
    }
}