TheLovelySausage
TheLovelySausage

Reputation: 4094

Java Graphics Only Paint on Button Click

I'm not sure how java graphics work. It seems to be executing something itself so I am trying to break it down.

I'm trying to create the blank JPanel and then only draw to it once the JButton has been clicked but it doesn't work

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);
      }

   }

}

I create the content JPanel without the draw function being called and then I try to call it using my ActionListener although it is crashing because of the graphic variable.

What is the correct way to use the java graphics utility?

UPDATE

Maybe I'm not asking this question right but it is possible to create a blank image.

Then draw additional images to that images (squares) after a button has been clicked?

not just updating the dimensions using global variables but generating new images to that existing image

Upvotes: 0

Views: 2138

Answers (2)

user3408531
user3408531

Reputation:

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);
    }
}
}

Upvotes: 0

camickr
camickr

Reputation: 324088

but it is possible to create a blank image. Then draw additional images to that images (squares) after a button has been clicked?

Check out Custom Painting Approaches for two common ways to do painting.

The example allows you to draws Rectangles with the mouse. In your case the logic will be simpler as you would just invoke the addRectangle(...) method when you click a button. Of course you need to set the size/location of the Rectangle somehow.

Upvotes: 1

Related Questions