Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package graphq.gui;

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import graphq.Handler;
import graphq.utils.Tool;

public class Canvas {
	private Rectangle selection;
	private Point clickPoint;

	public static void main(String[] args) {
		new Canvas();
	}

	public Canvas() {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
						| UnsupportedLookAndFeelException ex) {
					ex.printStackTrace();
				}

				// main frame
				JFrame frame = new JFrame("Graphq Paint");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.add(new Pane());
				frame.setSize(1920, 1080);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);

				// tool box
				JFrame toolFrame = new JFrame();
				JButton pencil = new JButton();
				JButton select = new JButton();
				toolFrame.add(pencil);
				toolFrame.add(select);
				pencil.setSize(64, 64);
				select.setSize(64, 64);
				toolFrame.setVisible(true);
				toolFrame.setSize(128, 64);
				toolFrame.pack();

				// buttons
				pencil.addActionListener(new pencilClick());
				select.addActionListener(new selectClick());
				ImageIcon pencilIcon = new ImageIcon("./src/images/pencil.png");
				pencil.setIcon(pencilIcon);

				ImageIcon selectIcon = new ImageIcon("./src/images/select.png");
				select.setIcon(selectIcon);
			}
		});
	}

	public class pencilClick implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Handler.setSelectedTool(Tool.PENCIL);
		}
	}

	public class selectClick implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Handler.setSelectedTool(Tool.SELECT);
		}
	}

	public class Pane extends JPanel {

		private List<List<Point>> points;

		public Pane() {
			points = new ArrayList<>(25);
			MouseAdapter ma = new MouseAdapter() {
				private List<Point> currentPath;

				@Override
				public void mousePressed(MouseEvent e) {
					if (Handler.getSelectedTool() == Tool.PENCIL) {
						currentPath = new ArrayList<>(25);
						currentPath.add(e.getPoint());

						points.add(currentPath);
					}
					if (Handler.getSelectedTool() == Tool.SELECT) {
						clickPoint = e.getPoint();
						selection = null;
					}
				}

				@Override
				public void mouseDragged(MouseEvent e) {
					if (Handler.getSelectedTool() == Tool.PENCIL) {
						Point dragPoint = e.getPoint();
						currentPath.add(dragPoint);
						repaint();
					}

					if (Handler.getSelectedTool() == Tool.SELECT) {
						Point dragPoint = e.getPoint();
						int x = Math.min(clickPoint.x, dragPoint.x);
						int y = Math.min(clickPoint.y, dragPoint.y);

						int width = Math.max(clickPoint.x, dragPoint.x) - x;
						int height = Math.max(clickPoint.y, dragPoint.y) - y;

						if (selection == null) {
							selection = new Rectangle(x, y, width, height);
						} else {
							selection.setBounds(x, y, width, height);
						}
						repaint();
					}

				}

				@Override
				public void mouseReleased(MouseEvent e) {
					if (Handler.getSelectedTool() == Tool.PENCIL) {
						currentPath = null;
					}
					if (Handler.getSelectedTool() == Tool.SELECT) {
						selection = null;
						repaint();
					}

				}

			};

			addMouseListener(ma);
			addMouseMotionListener(ma);
		}

		@Override
		public Dimension getPreferredSize() {
			return new Dimension(200, 200);
		}

		protected void paintComponent(Graphics g) {
			if (Handler.getSelectedTool() == Tool.PENCIL) {
				super.paintComponent(g);
				Graphics2D g2d = (Graphics2D) g.create();
				for (List<Point> path : points) {
					Point from = null;
					for (Point p : path) {
						if (from != null) {
							g2d.drawLine(from.x, from.y, p.x, p.y);
						}
						from = p;
					}
				}
				g2d.dispose();
			}

			if (Handler.getSelectedTool() == Tool.SELECT) {
				super.paintComponent(g);
				if (selection != null) {
					g.setColor(UIManager.getColor("List.selectionBackground"));
					Graphics2D g2d = (Graphics2D) g.create();
					g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
					g2d.fill(selection);
					g2d.dispose();
					g2d = (Graphics2D) g.create();
					g2d.draw(selection);
					g2d.dispose();
				}
			}

		}

	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

}


What I have tried:

setting the size using
Java
toolFrame.setSize(64, 128);


using
<pre lang="java">toolFrame.pack();
and
Java
toolFrame.getContentPane().setSize();


thanks in advance for your help!
Posted
Comments
Richard MacCutchan 28-Aug-18 5:27am    
Most likely because things get resized when you call pack.
Astronomize 28-Aug-18 10:23am    
I removed pack and it still does it.
Richard MacCutchan 28-Aug-18 10:56am    
Sorry, but it is not possible to guess what is happening. You will need to try a few more experiments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900