- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 Foo 的类,它扩展了一个名为 Bar 的类,该类扩展了 JPanel 并实现了 ActionListener。当我选择 Circle 并单击绘制按钮时,我绘制了一个圆,当我按下矩形并单击绘制时,它会删除之前的形状并绘制一个矩形。
但是,我想保留 JPanel 上的所有形状,直到我选择单击删除按钮。所以我删除了 super.paintComponent(g)
并且它可以工作,但它也会导致类 Bar 的按钮以故障方式重新出现。我怎样才能阻止按钮再次绘画?我在考虑不扩展 Bar 而让 Foo 扩展 JPanel。
public class Bar extends JPanel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "Draw")
{
this.requestDraw = true;
repaint();
}
if (e.getActionCommand() == "Circle")
{
requestRectangle = false;
requestTriangle = false;
requestCircle = true;
}
if (e.getActionCommand() == "Rectangle")
{
requestCircle = false;
requestTriangle = false;
requestRectangle = true;
}
if (e.getActionCommand() == "Right Triangle")
{
requestCircle = false;
requestRectangle = false;
requestTriangle = true;
}
}
public class Foo extends Bar
{
@Override
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
if(RequestDraw())
{
if(RequestCircle())
circle.draw(g);
if(RequestRectangle())
rectangle.draw(g);
if(RequestTriangle())
rightTriangle.draw(g);
}
if(!RequestDraw())
{
g.setColor(getBackground());
g.fillRect(0,0,getWidth(), getHeight());
}
}
}
}
最佳答案
在 Hovercraft 的所有评论旁边
图形上下文在组件之间共享。 super.paintComponent
的任务之一是在绘制之前“清理”图形上下文。
这就是为什么您会看到两个版本的按钮...
我也会做一些不同的事情。随着时间的推移,这应该有助于扩展性和重用性,同时也会稍微减少逻辑。
我会...
Action
来简单地创建这些形状并将它们添加到模型中...
我只创建了一个三角形(除了位置和大小之外,它没有其他属性),但我相信您会明白大概的意思...(请注意,您需要提供自己的三角形图标为了行动 ;))
public class DrawMe {
public static void main(String[] args) {
new DrawMe();
}
public DrawMe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
DrawModel model = new DefaultDrawModel();
model.addElement(new Triangle(new Rectangle(10, 10, 100, 100)));
DrawPane drawPane = new DrawPane(model);
JToolBar toolBar = new JToolBar();
toolBar.add(new AddTriangleAction(model));
frame.add(toolBar, BorderLayout.NORTH);
frame.add(drawPane);
frame.setSize(400, 400);
frame.setVisible(true);
}
});
}
/**
* Simple action used to add triangles to the model...the model acts
* as a bridge between the action and the UI.
*/
protected class AddTriangleAction extends AbstractAction {
private DrawModel model;
public AddTriangleAction(DrawModel model) {
// Supply your own icon
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("/shape_triangle.png")));
this.model = model;
}
public DrawModel getModel() {
return model;
}
@Override
public void actionPerformed(ActionEvent e) {
// Randomly add the triangles...
int x = (int)(Math.random() * 400);
int y = (int)(Math.random() * 400);
model.addElement(new Triangle(new Rectangle(x, y, 100, 100)));
}
}
/**
* This is the background pane, from which the draw pane extends...
*/
protected class BackgroundPane extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = getWidth() / 2;
int y = getHeight() / 2;
Graphics2D g2d = (Graphics2D) g.create();
RadialGradientPaint rgp = new RadialGradientPaint(
new Point(x, y),
Math.max(getWidth(), getHeight()),
new float[]{0f, 1f},
new Color[]{Color.GRAY, Color.WHITE}
);
g2d.setPaint(rgp);
g2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
g2d.setBackground(Color.BLACK);
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2d.dispose();
}
}
/**
* This is a simple model, I stole the list model because it was quicker
* and easier to demonstrate (don't need to write all the listeners)
*/
public interface DrawModel extends ListModel<DrawMeShape> {
public void addElement(DrawMeShape shape);
public void removeElement(DrawMeShape shape);
}
/**
* A default implementation of the DrawModel...
*/
public class DefaultDrawModel extends DefaultListModel<DrawMeShape> implements DrawModel {
@Override
public void removeElement(DrawMeShape shape) {
removeElement((Object)shape);
}
}
/**
* The actually "canvas" that shapes are rendered to
*/
protected class DrawPane extends BackgroundPane {
// Should provide ability to setModel...
private DrawModel model;
public DrawPane(DrawModel model) {
this.model = model;
model.addListDataListener(new ListDataListener() {
@Override
public void intervalAdded(ListDataEvent e) {
repaint();
}
@Override
public void intervalRemoved(ListDataEvent e) {
repaint();
}
@Override
public void contentsChanged(ListDataEvent e) {
repaint();
}
});
}
public DrawModel getModel() {
return model;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the shapes from the model...
Graphics2D g2d = (Graphics2D) g.create();
DrawModel model = getModel();
for (int index = 0; index < model.getSize(); index++) {
DrawMeShape shape = model.getElementAt(index);
shape.paint(g2d, this);
}
g2d.dispose();
}
}
/**
* A abstract concept of a shape. Personally, if I was doing it, I would
* generate an interface first, but this is just a proof of concept...
*/
public abstract class DrawMeShape {
private Rectangle bounds;
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public Rectangle getBounds() {
return bounds;
}
protected abstract Shape getShape();
/**
* The shape knows how to paint, but it needs to know what to paint...
* @param g2d
* @param parent
*/
public void paint(Graphics2D g2d, JComponent parent) {
g2d = (Graphics2D) g2d.create();
Rectangle bounds = getBounds();
Shape shape = getShape();
g2d.translate(bounds.x, bounds.y);
g2d.setColor(Color.DARK_GRAY);
g2d.fill(shape);
g2d.setColor(Color.BLACK);
g2d.draw(shape);
g2d.dispose();
}
}
/**
* An implementation of a Triangle shape...
*/
public class Triangle extends DrawMeShape {
public Triangle(Rectangle bounds) {
setBounds(bounds);
}
@Override
protected Shape getShape() {
// This should be cached ;)
Path2D path = new Path2D.Float();
Rectangle bounds = getBounds();
path.moveTo(bounds.width / 2, 0);
path.lineTo(bounds.width, bounds.height);
path.lineTo(0, bounds.height);
path.lineTo(bounds.width / 2, 0);
path.closePath();
return path;
}
}
}
快乐的画...
关于java - 保留绘制图形 - 删除 super.paintComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810057/
我一直在为此而苦苦挣扎。我想插入一个图像,并将其“靠近”讨论该图像的文本,但是该页面上的文本将围绕图像环绕/流动。 我已将图像转换为eps格式。最初,我尝试使用图形环境(\begin {figure}
我在用户界面中创建了管理控制台,管理员可以在其中执行所有操作,例如创建、删除用户、向用户分配应用程序以及从用户界面删除用户的应用程序访问权限 我厌倦了使用 Microsoft 图形 API 和 Azu
我在用户界面中创建了管理控制台,管理员可以在其中执行所有操作,例如创建、删除用户、向用户分配应用程序以及从用户界面删除用户的应用程序访问权限 我厌倦了使用 Microsoft 图形 API 和 Azu
我想为计算机图形学类(class)做一个有趣的项目。我知道那里有很多文献(即 SIGGRAPH session 论文)。我对计算机图形学(即图像处理、3D 建模、渲染、动画)兴趣广泛。但是,我只学了
我试图在 MaterializeCSS 网站上创建一些类似于这个的图形,但我不知道它来自哪里,我查看了整个 MaterializeCSS 网站,它不是框架的一部分,我找不到在代码中他们使用的是什么 我
我有一个包含 1 到 6 之间的各种数字的 TextView ,每个数字在每一行上代表一次,例如 123456 213456 214356 ...... 我希望能够绘制一条蓝线来跟随单个数值在列表中向
我目前在 Windows 7 上使用 Netbeans 和 Cygwin,我希望用 C 语言编写一个简单的 2D 游戏。 我设法找到的大多数教程都使用 Turbo C 提供的 graphics.h,C
亲爱的,我正在尝试将 kaggle 教程代码应用于 Iris 数据集。 不幸的是,当我执行图表的代码时,我只能看到这个输出而看不到任何图表: matplotlib.axes._subplots.Axe
我需要加快我正在处理的一些粒子系统的视觉效果。令人眼前一亮的是添加混合、积累以及粒子上的轨迹和发光。目前我正在手动渲染到浮点图像缓冲区,在最后一分钟转换为无符号字符,然后上传到 OpenGL 纹理。为
在研究跨网络的最短路径算法时,我想生成网络图片。我想代表节点(圆圈)、链接(线)、遍历链接的成本(链接线中间的数字)和链接的容量(链接线上它代表的节点旁边的数字)在这张图中。是否有任何库/软件可以帮助
尽管我已将应用程序从库添加到 Azure AD,但我无法看到何时尝试提取数据。但我可以看到添加的自定义应用程序。就像我添加了 7 个应用程序一样; 2 个来自图库(Google 文档、一个驱动器)和
因此,我正在构建一个系统,该系统具有“人员”,“银行帐户”和“银行帐户交易”。 我需要能够回答以下问题: “将所有与1/2/3度有联系的人归还给特定的人”, “返回年龄在40岁以上的所有人” “从德国
我在 JFrame 构造函数中有以下简单代码 super(name); setBounds(0,0,1100,750); setLayout(null); setVis
(这是java)我有一个椭圆形,代表一个单位。我希望椭圆形的颜色代表单位的健康状况。因此,一个完全健康的单位将是全绿色的。随着单位生命值的降低,椭圆形开始从底部填充红色。因此,在 50% 生命值下,椭
我目前正在开发一个学校项目。我们必须制作一个Applet,我选择了JApplet。由于某种原因,我用来显示特定字符串的面板将不会显示。这里可能有什么问题?请指出我正确的方向。另外,我看了一些教程,
我正在尝试创建一个 Simon game 。我正在编写游戏程序,但遇到了问题。我希望程序从队列中读取游戏中之前存在的所有值,并以正确的顺序将它们的颜色变为闪烁(我选择将它们变为灰色,然后在第二秒后恢复
我正在尝试创建一个框架,该框架在同一框架的顶部有一个图形面板(通过布局),在其下方有一个按钮/标签面板。到目前为止,我似乎已经能够将它们放在同一个框架上,但与按钮/标签面板相比,图形面板非常小....
我用 Java 编写了一个解决数独问题的代码,并使用 Java Applet 来设计它。现在,我尝试使用 Java Swing 使其看起来更好,并添加一些功能,例如“保存”数独板等。不幸的是,我对 J
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我现在尝试了 8 个多小时来解决这个问题,但无法弄清楚,请帮助找出我的代码有什么问题。 int main() { int gd = DETECT, gm; float ANGLE =
我是一名优秀的程序员,十分优秀!