JFrame 菜单被绘制并且图形在 JFrame 调整大小时消失
我是一名高中生,正在为计算机科学课做期末项目。我的任务是制作一个简单的绘画应用程序,但是我遇到了一些问题。其中包括:调整窗口大小时会擦除绘制的图像,并且我可以在窗口中的菜单上绘制图像。
我相信我正在菜单上绘图,因为我正在使用 JFrame 本身的图形对象。但是我找不到任何替代方案。我尝试过制作单独的组件来绘制,甚至使用 BufferedImage,但我的尝试都没有成功。
这是我的代码:
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()));
}
else {
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.
}
}
我该如何解决这个问题?
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()));
}
else {
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切勿使用 getGraphics() 方法进行绘画。正如您所注意到的,效果只是暂时的。
自定义绘制是通过重写paintComponent()方法而不是paint()方法来完成的。
请参阅自定义绘画方法,了解如何在面板上绘制多个对象。
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 是对的。当您调整窗口大小时,ImplementGUI 组件将被重新绘制,因此将调用 ImplementGUI.paintComponent() 来重新绘制组件的整个表面,并且您在 mouseDragged() 中绘制的内容将丢失。
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.