Java Graphics Only Paint on Button Click



我不确定java图形是如何工作的。看起来正在执行某件事,所以我正试图打破它向下

我正在尝试创建空白的JPanel,然后只绘制它一旦JButton被点击但它不起作用

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class testGui {
   // global ======================================================================
   static gui      gc_gui;
   // main ========================================================================
   public static void main(String[] args) {
      gc_gui = new gui();
      gc_gui.cv_frame.setVisible(true);
      listeners();
   }
   // action listeners ============================================================
   public static void listeners() {
      ActionListener ll_square = new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            gc_gui.cv_content.draw(graphic);
         }
      };
      gc_gui.cv_button.addActionListener(ll_square);
   }
   // gui =========================================================================
   public static class gui {
      JFrame    cv_frame;
      JButton   cv_button;
      content   cv_content;
      public gui() {
         cv_frame = new JFrame();
         cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         cv_frame.setTitle("Test GUI");
         cv_frame.setSize(600, 400);
         cv_frame.setLayout(new FlowLayout());   
         cv_button = new JButton("Square");
         cv_content = new content();
         cv_content.setBackground(Color.BLACK);
         cv_content.setPreferredSize(new Dimension(500, 300));
         cv_frame.add(cv_button);
         cv_frame.add(cv_content);
      }
   }
   // content =====================================================================
   public static class content extends JPanel {
      public void paint(Graphics graphic) {
         super.paint(graphic);
      }
      public void update() {
         super.repaint();
      }
      public void draw(Graphics graphic) {
         Graphics2D graphic2D = (Graphics2D) graphic;
         graphic2D.setPaint(Color.RED);
         graphic2D.fillRect(10, 10, 100, 100);
      }
   }
}

我创建的内容JPanel没有绘制功能调用,然后我尝试使用ActionListener调用它尽管它由于图形变量而崩溃。

使用java图形实用程序的正确方法是什么?

更新

也许我问这个问题不对,但有可能创建空白图像。

然后在按钮后为该图像绘制额外的图像(正方形)是否已被单击?

不仅使用全局变量更新维度,而且生成新图像到现有图像

但是可以创建空白图像。然后在点击按钮后,在图像(正方形)上绘制其他图像?

查看自定义绘画方法,了解两种常用的绘画方法。

该示例允许您使用鼠标绘制矩形。在您的情况下,逻辑会更简单,因为您只需在单击按钮时调用addRectangle(...)方法。当然,您需要以某种方式设置矩形的大小/位置。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class TestGui {
public static Content content;
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Test GUI");
    frame.setSize(429, 385);
    frame.getContentPane().setLayout(null);
    JButton cv_button = new JButton("Square");
    cv_button.setBounds(10, 159, 70, 23);
    frame.getContentPane().add(cv_button);
    cv_button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Content.isToDraw = true;
            content.paintImmediately(content.getBounds());
            Content.isToDraw = false;
        }
    });
    JButton cv_buttonClear = new JButton("Clear");
    cv_buttonClear.setBounds(10, 179, 70, 23);
    frame.getContentPane().add(cv_buttonClear);
    cv_buttonClear.addActionListener(new ActionListener() {
         @Override
        public void actionPerformed(ActionEvent e) {
            content.paintImmediately(content.getBounds());
        }
     });
    content = new Content();
     content.setBorder(new LineBorder(new Color(0, 0, 0)));
    content.setBounds(87, 11, 287, 312);
    frame.getContentPane().add(content);
    frame.setVisible(true);
}
}
 class Content extends JPanel {
public static Boolean isToDraw = false;
public void paintComponent(Graphics arg0) {
    if (isToDraw) {
        arg0.setColor(Color.RED);
        arg0.fillRect(0, 0, getWidth(), getHeight());
    } else {
        super.paintComponent(arg0);
    }
}
}

相关内容

  • 没有找到相关文章

最新更新