开发者

JFrame Menu's being drawn over and Graphics disappearing upon JFrame Resize

开发者 https://www.devze.com 2023-03-10 23:44 出处:网络
I am a high school student, and I am working on my final project for my computer science class. My assignment was to make a simple paint application, however I am having some problems. These include:

I am a high school student, and I am working on my final project for my computer science class. My assignment was to make a simple paint application, however I am having some problems. These include: the painted image being erased when the window is resized and that I can draw over the menu's in my window.

I believe I am drawing over the menu's because I am using the graphics object of the JFrame itself. However I can not find any alternatives. I have tried making separate components to draw in, and even using BufferedImage, but none of my attempts have been successful.

Here is my code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

public class Paint implements MouseMotionListener{

private JFrame window;
private drawingComponent pComp;
private int xPos; //xPos of mouse;
private int yPos; //yPos of mouse;
private Color color; // new color
private JTextArea sizeBox;

private int brushShape = 0;

public static void main(String[] args) {
    Paint paint = new Paint();
    paint.ImplementGUI();
}

public Paint() {
    this.pComp = new drawingComponent();
    this.window = new JFrame("JPaint");
    this.window.setSize(500, 500);
    this.window.setVisible(true);
    this.window.addMouseMotionListener(this);
    this.window.add(this.pComp);
}

public void ImplementGUI() {
    JMenuBar menu = new JMenuBar();
    JMenu brush = new JMenu("Brush Settings");
    JMenu brushShape = new JMenu("Shape");
    brush.setSize(100,100);
    brush.add(brushShape);
    menu.add(brush);
    menu.setSize(window.getWidth(), 20);
    menu.setVisible(true);

    this.sizeBox = new JTextArea("Brush Size:");
    this.sizeBox.setText("20");
    brush.add(this.sizeBox);

    this.window.setJMenuBar(menu);
}

public void mouseDragged(MouseEvent pMouse) {
    if (pMouse.isShiftDown() == true) {
        this.pComp.paint(this.window.getGraphics(), pMouse.getX(), pMouse.getY(),
                         window.getBackground(), Integer.parseInt(sizeBox.getText()));
    }
    els开发者_如何学编程e {
        this.pComp.paint(this.window.getGraphics(), pMouse.getX(), pMouse.getY(),
                         color, Integer.parseInt(sizeBox.getText()));
    }
}

public void mouseMoved(MouseEvent pMouse) {
}

}

class drawingComponent extends JComponent {
    public void paint(Graphics g, int x, int y, Color color, int size) {
        g.setColor(color);
        g.fillOval(x-(size/2), y-(size/2), size, size); // 10 is subtracted from
        // coordinates so the mouse pointer is at the exact middle of the brush.
    }
}

How can I fix this problem?


Never use the getGraphics() method to do painting. The effect is only temporary as you have noticed.

Custom painting is done by overriding the paintComponent() method, not the paint() method.

See Custom Painting Approaches for a couple of example of how to paint multiple objects on a panel.


camickr is right. When you resize the window, the ImplementGUI component is repainted, so ImplementGUI.paintComponent() will be invoked to repaint the whole surface of the component, and what you did paint within mouseDragged() will be lost.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号