-1

Could someone provide a basic example of a Java ui wherein a button click draws a rectangle on a jpanel that's next to it?

You find examples of drawing where the mouse is captured, or drawing that's static from loading the ui, but I couldn't find an example of one component that's used (click) to draw on another component.

I have a ui where a user defines the number of boxes (rows and columns) and the ok button should draw these boxes on the JPanel that simulated a sheet of paper.

Thanks for your help, its appreciated.

InnerOrchestra
  • 37
  • 2
  • 10

2 Answers2

0

if you want to draw something on a component, overwrite its paintComponent-Method.

basic pseudo example, using JPanel :

public class MyPanel extends JPanel
{
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      //here your draw stuff
      //like: 
      Graphics2D g2d = (Graphics2D)g;
      g.drawLine(...);
   }
}
Ben
  • 3,378
  • 30
  • 46
  • Thanks for the response. Like I wrote, I'm not looking to draw on component from the component. I'm looking to draw on a component from the click action of another component. Would you know how to do that? Thanks again. – InnerOrchestra Aug 31 '14 at 21:35
  • yes, i know what you want to do. but im afraid that this is not possible, out of the box. when you click on an button, this button can (for example) create an custom Shape-Object, which your custom panel can draw.create an ShapeList.this list contains all your shapes. your custom panel will paint all of this shapes in this list in paintComponent. – Ben Aug 31 '14 at 21:48
0

Here's how I did it. Please let me know how you'd improve this beginner code! ;)

Essentially, here' the trick to get a button to draw rectangles:

  • Extend your main app class to a JFrame (or JComponent) or ???

  • Declare right at the top of your main app your class to draw (DrawCanvas) and extend to a JPanel.

  • Right at the top of your main app class declared an ArrayList to hold the stuff that you'll draw.

  • Declare a variable for the drawing class right at the top of your main app class.

  • In your control event (button in my case) use a function to prepare the stuff to draw (I used one called AddRectangle()).

  • In your drawing class, override the paintComponent and use a for each to draw all the stuff you squirrelled away in your array.

Since you can't control directly the drawing, you have to understand that each time repaint() gets called that drawing function will be called. That means that you have to stash away all your stuff like a bloody squirrel in order for the drawing method to draw or redraw the screen correctly.In the end its usually a bunch of arrays that you'll end up using and a bunch of for each loop to go through them.

package views;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;



public class appMainWindow extends JFrame
{
class PdfLocation
{
    public double xPos;
    public double yPos; 
}

class DrawCanvas extends JPanel
{
      @Override
      public void paintComponent(Graphics g) 
      {
         super.paintComponent(g);
         for (PdfLocation p : PdfLocations)
         {
             g.drawRect((int)p.xPos, (int)p.yPos, 35, 20);
             repaint();
         }
      }
}

public void AddRectangle()
{
    PdfImagesCount++;
    lblPdfcount.setText(Integer.toString(PdfImagesCount));

    PdfLocation rect = new PdfLocation();

    if (PdfLocations.isEmpty() == false)
    {
        PdfLocation spot = PdfLocations.get(PdfLocations.size() - 1);
        rect.xPos = spot.xPos + 45;
        rect.yPos = 10;
    }   
    else
    {
        rect.xPos = 10;
        rect.yPos = 10;         
    }
    PdfLocations.add(rect);             
}

private JFrame frame;
public ArrayList<PdfLocation> PdfLocations = new  ArrayList<PdfLocation>();
public int PdfImagesCount = 0;


public static final int CANVAS_HEIGHT = 700;
public static final int CANVAS_WIDTH = 1000;

private DrawCanvas canvas;
private JLabel lblPdfcount;

public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { appMainWindow window = new appMainWindow(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }

public appMainWindow() 
{ 

    // Set up a custom drawing JPanel
    canvas = new DrawCanvas();
    canvas.setBackground(Color.WHITE);
    canvas.setBounds(150, 25, CANVAS_WIDTH, CANVAS_HEIGHT);
    initialize(); 
}

private void initialize() 
{
    frame = new JFrame();
    frame.setBounds(100, 100, 1280, 850);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnAddARectangle = new JButton("Add a rectangle");
    btnAddARectangle.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            AddRectangle();
            repaint();
        }
    });
    btnAddARectangle.setBounds(0, 6, 151, 29);
    frame.getContentPane().add(btnAddARectangle);


    frame.getContentPane().add(canvas);

    lblPdfcount = new JLabel("PdfCount");
    lblPdfcount.setBounds(10, 43, 61, 16);
    frame.getContentPane().add(lblPdfcount);
}

}

InnerOrchestra
  • 37
  • 2
  • 10