我试图制作一个可以在屏幕上绘制矩形,形状的程序。到目前为止,我已经创建了一个GUI,它有一个菜单栏和带有一些按钮的工具栏(它们还没有运行)。
现在我遇到了这个问题,我的矩形没有显示在我的绘图面板上,而且我认为由于JPanels彼此重叠而导致我的DrawRectangle类。但我不确定。
这是我的 DrawRectangle类,我不想绘制矩形,然后将它们放入我的 DrawingPanel (如果是这样的话) #39;可能)。
package Shapes;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawRectangle extends JPanel
{
private static final long serialVersionUID = 1L;
public int old_x;
public int old_y;
public int new_x;
public int new_y;
public DrawRectangle(int old_x, int old_y, int new_x, int new_y)
{
this.old_x = old_x;
this.old_y = old_y;
this.new_x = new_x;
this.new_y = new_y;
repaint();
}
public void paintComponent(Graphics g)
{
g.drawRect(old_x, old_y, new_x, new_y);
}
}
这是我的 DrawingPanel类,我想在其中存储DrawRectangle类中的所有绘制图形
package Panel;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import Shapes.DrawRectangle;
public class DrawingPanel
{
private JPanel drawPanel;
private int tool = 1;
int currentX, currentY, oldX, oldY;
public DrawingPanel()
{
drawPanel = new JPanel();
drawPanel.setBackground(Color.WHITE);
drawPanel.setBorder(BorderFactory.createLineBorder(Color.black));
// Testing, neither of them are showing up (trying without mouse)
new DrawRectangle(100,100,100,100);
new DrawRectangle(200,200,300,100);
drawPanel.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent evt){
drawPanelMousePressed(evt);
}
public void mouseReleased(MouseEvent evt){
drawPanelMouseReleased(evt);
}
});
drawPanel.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent evt){
drawPanelMouseDragged(evt);
}
});
}
public JPanel getDrawPanel()
{
return drawPanel;
}
private void drawPanelMouseDragged(MouseEvent evt) {
if (tool == 1) {
currentX = evt.getX();
currentY = evt.getY();
oldX = currentX;
oldY = currentY;
System.out.println(currentX + " " + currentY);
System.out.println("PEN!!!!");
}
}
private void drawPanelMousePressed(MouseEvent evt) {
oldX = evt.getX();
oldY = evt.getY();
System.out.println(oldX + " " + oldY);
}
private void drawPanelMouseReleased(MouseEvent evt) {
currentX = evt.getX();
currentY = evt.getY();
}
}
此外,我还在考虑可能导致我的DrawingPanel内容无法显示的JPanel容器。这是我的 ContainerPanel类
package Panel;
import java.awt.BorderLayout;
import javax.swing.JPanel;
public class ContainerPanel
{
private JPanel containerPanel;
public ContainerPanel()
{
containerPanel = new JPanel(new BorderLayout());
containerPanel.setOpaque(true);
containerPanel.add(new DrawingPanel().getDrawPanel(), BorderLayout.CENTER);
containerPanel.add(new ToolBarPanel().getToolBarPanel(), BorderLayout.NORTH);
}
public JPanel getContainerPanel()
{
return containerPanel;
}
}
stackoverflow中的第一次:)
答案 0 :(得分:3)
drawPanel = new JPanel();
创建面板时,将其分配给变量,以便在getDrawPanel()方法中使用该变量。
new DrawRectangle(100,100,100,100);
在这里,您可以创建一个DrawRectangle但不用它做任何事情。你不会把它添加到drawPanel或任何东西,所以它永远不会被绘制。
我认为这是因为JPanels彼此重叠了我的DrawRectangle类。
即使您修复了上述问题并将DrawRectangle类添加到DrawPanel,您仍会遇到问题:
您需要先阅读Swing基础知识的Swing Tutorial。也许部分:
Custom Painting
- 将向您展示如何正确覆盖paintCompnent()方法并实现getPreferredSize()方法
Doing Without a Layout Manager
- 因为您需要将组件放置在特定位置(即矩形的x / y)。
已经说过一个更简单的解决方案,可以在一个面板上自定义绘制所有Rectangle。查看Custom Painting Approaches。它有一些工作示例来演示执行此操作的两种常用方法: