panel.getGraphics();获取的画笔为空是什么情况
时间: 2024-06-03 21:11:09 浏览: 99
?这通常是因为在调用panel.getGraphics()之前,尚未将画布初始化或绘制到屏幕上。如果您正在使用Swing框架,则可以在确定绘制组件之前调用panel.invalidate(),以确保组件以后会在屏幕上绘制。您还可以使用paintComponent()方法或paint()方法在组件上进行绘制。如果您仍然遇到问题,请尝试查看控制台输出是否显示有关Java图形库的任何错误信息,以帮助您进一步调试。
相关问题
private class MouseDemo extends JFrame implements MouseMotionListener{ int x,y; public MouseDemo() { //注册鼠标监听 jpl7.addMouseMotionListener(this); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 清空画板 jpl7.getGraphics().clearRect(0, 0, jpl7.getWidth(), jpl7.getHeight()); } }); } //重写JFrame的paint()方法 public void paint(Graphics g) { //设置画笔颜色 g.setColor(Color.black); //画一个实心圆 g.fillOval(x, y, 5, 5); } //鼠标移动的处理方法 public void mouseDragged(MouseEvent e) { //获取坐标 x=e.getX(); y=e.getY(); System.out.println(x); System.out.println(y); //重画,repaint()触发paint() this.repaint(); } //鼠标移动的处理方法 public void mouseMoved(MouseEvent e) { } 怎么重写jpl7的paintComponent方法
你可以在 `MouseDemo` 类中创建一个内部类,继承 `JPanel` 并重写 `paintComponent(Graphics g)` 方法来实现 `jpl7` 面板的绘图功能。代码如下所示:
```
private class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 在这里添加你的绘图代码
// 可以通过 Graphics 对象 g 来进行绘图
}
}
```
然后,在 `MouseDemo` 构造方法中创建一个 `MyPanel` 对象,并将其添加到 `JFrame` 中,代码如下所示:
```
public MouseDemo() {
MyPanel panel = new MyPanel();
panel.addMouseMotionListener(this);
jpl7.add(panel);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 清空画板
panel.getGraphics().clearRect(0, 0, panel.getWidth(), panel.getHeight());
}
});
}
```
这样就可以通过 `MyPanel` 类中重写的 `paintComponent(Graphics g)` 方法来实现 `jpl7` 面板的绘图功能了。
java,1、绘图应用程序。利用学过的GUI方法实现一个较为完整 的绘图应用程序。用户可以选择要画的图形(如线椭圆,圆,矩形,弧,圆角矩形,多边形,折线等等),选择画笔颜色和画笔粗度,用鼠标单击和拖拽等动作绘图。
好的,这个任务可以分成几个步骤来完成。
1. 创建一个GUI界面,包含一个画板和一些控制按钮,比如选择图形、选择颜色、选择粗细等等。
2. 在画板上监听鼠标事件,根据用户的动作绘制相应的图形。
3. 实现不同图形的绘制方法,比如绘制线条、椭圆、矩形、弧、圆角矩形、多边形、折线等等。
4. 实现控制按钮的响应方法,比如选择颜色、选择粗细、选择图形等等。
下面是一个简单的Java代码示例,可以帮助你了解如何实现这个应用程序:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawingApp extends JFrame {
private JPanel controlPanel;
private JPanel drawingPanel;
private JComboBox<String> shapeBox;
private JComboBox<String> colorBox;
private JComboBox<Integer> sizeBox;
private JButton clearButton;
private int startX, startY, endX, endY;
private int selectedShape = 0;
private Color selectedColor = Color.BLACK;
private int selectedSize = 1;
public DrawingApp() {
// 初始化界面
setTitle("Drawing App");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建控制面板和绘图面板
controlPanel = new JPanel();
drawingPanel = new DrawingPanel();
drawingPanel.setBackground(Color.WHITE);
// 添加控制按钮
String[] shapes = {"Line", "Oval", "Rectangle", "Arc", "Round Rectangle", "Polygon", "Polyline"};
shapeBox = new JComboBox<>(shapes);
shapeBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedShape = shapeBox.getSelectedIndex();
}
});
controlPanel.add(shapeBox);
String[] colors = {"Black", "Red", "Green", "Blue"};
colorBox = new JComboBox<>(colors);
colorBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selected = (String) colorBox.getSelectedItem();
switch (selected) {
case "Red":
selectedColor = Color.RED;
break;
case "Green":
selectedColor = Color.GREEN;
break;
case "Blue":
selectedColor = Color.BLUE;
break;
default:
selectedColor = Color.BLACK;
break;
}
}
});
controlPanel.add(colorBox);
Integer[] sizes = {1, 2, 3, 4, 5};
sizeBox = new JComboBox<>(sizes);
sizeBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectedSize = (int) sizeBox.getSelectedItem();
}
});
controlPanel.add(sizeBox);
clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((DrawingPanel) drawingPanel).clear();
}
});
controlPanel.add(clearButton);
// 将控制面板和绘图面板添加到窗口中
add(controlPanel, BorderLayout.NORTH);
add(drawingPanel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new DrawingApp();
}
private class DrawingPanel extends JPanel {
private int[] xPoints = new int[100];
private int[] yPoints = new int[100];
private int nPoints = 0;
public DrawingPanel() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
switch (selectedShape) {
case 0:
drawLine();
break;
case 1:
drawOval();
break;
case 2:
drawRectangle();
break;
case 3:
drawArc();
break;
case 4:
drawRoundRectangle();
break;
case 5:
drawPolygon();
break;
case 6:
drawPolyline();
break;
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endX = e.getX();
endY = e.getY();
switch (selectedShape) {
case 0:
drawLine();
break;
case 1:
drawOval();
break;
case 2:
drawRectangle();
break;
case 3:
drawArc();
break;
case 4:
drawRoundRectangle();
break;
case 5:
break;
case 6:
xPoints[nPoints] = endX;
yPoints[nPoints] = endY;
nPoints++;
drawPolyline();
break;
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(selectedColor);
g.drawLine(startX, startY, endX, endY);
}
private void drawLine() {
Graphics g = getGraphics();
g.setColor(selectedColor);
g.drawLine(startX, startY, endX, endY);
}
private void drawOval() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawOval(startX, startY, width, height);
}
private void drawRectangle() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawRect(startX, startY, width, height);
}
private void drawArc() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawArc(startX, startY, width, height, 45, 90);
}
private void drawRoundRectangle() {
Graphics g = getGraphics();
int width = endX - startX;
int height = endY - startY;
g.setColor(selectedColor);
g.drawRoundRect(startX, startY, width, height, 10, 10);
}
private void drawPolygon() {
Graphics g = getGraphics();
if (nPoints > 0) {
g.setColor(selectedColor);
g.drawPolygon(xPoints, yPoints, nPoints);
}
}
private void drawPolyline() {
Graphics g = getGraphics();
if (nPoints > 0) {
g.setColor(selectedColor);
g.setStroke(new BasicStroke(selectedSize));
g.drawPolyline(xPoints, yPoints, nPoints);
}
}
public void clear() {
Graphics g = getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
nPoints = 0;
}
}
}
```
这是一个简单的绘图应用程序,你可以根据需求对其进行修改和完善。
阅读全文