My overall goal is to create a jigsaw puzzle game that moves a set of images around the GUI screen with mousedrag. My current issue is to make this snapping function that when the mouse dragging the image releases, the image will snap into a predetermined place. This is where I want to create "boxes". I want to create 16 boxes for the 16 puzzle pieces and use their coordinates as the snapping location for the images when I release the mouse. (Just how in puzzle games the pieces snap to the correct place when you hover a general area). I am attempting this by making 16 JPanels, but I don't know if this is the smart way. Also, it seems I can't even add the JPanels to my JLayeredPane. I tried to set a border for one of the JPanels to help me see if it was added, and it was not.
Mouse drag works fine, everything works fine. I just want to be able to add some sort of representation of a box that the images when released by the mouse will snap into the box's location. Please let me know if you have any insights to this problem!
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Gui1 {
public static void main(String[] args) {
new Gui1();
}
public Gui1() { //Constructor for setting up the GUI
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JLayeredPane pane = new JLayeredPane();
JFrame frame = new JFrame("Testing");
JPanel canvas1 = new JPanel();
JPanel canvas2 = new JPanel();
JPanel canvas3 = new JPanel();
JPanel canvas4 = new JPanel();
JPanel canvas5 = new JPanel();
JPanel canvas6 = new JPanel();
JPanel canvas7 = new JPanel();
JPanel canvas8 = new JPanel();
JPanel canvas9 = new JPanel();
JPanel canvas10 = new JPanel();
JPanel canvas11 = new JPanel();
JPanel canvas12 = new JPanel();
JPanel canvas13 = new JPanel();
JPanel canvas14 = new JPanel();
JPanel canvas15 = new JPanel();
JPanel canvas16 = new JPanel();
canvas1.setPreferredSize(new Dimension(300,300));
canvas1.setBorder(BorderFactory.createLineBorder(Color.blue,10));
pane.add(canvas1, JLayeredPane.DEFAULT_LAYER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ImageGrab());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Related
Can't figure out why this wont work, I get a blank panel added to my gridbag panel.
I added a JLabel("test") between the frames I'm trying to add just to see that something IS getting added, it just isn't visible?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
public class Workspace extends JTabbedPane {
static JFrame frame = new JFrame();
private JPanel grid;
private String text = new String("testing.");
Workspace() {
this.addTab("title", growPanel());
}
public static void main(String[] args) throws IOException {
createAndShowGUI();
}
private JPanel growPanel() {
JPanel gp = new JPanel(false);
gp.setPreferredSize(new Dimension(800,600));
//Add a button that adds my frame
JButton addComponentBtn = new JButton("Add Component");
addComponentBtn.addActionListener(e-> {
try {
addComponent();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
gp.add(addComponentBtn, BorderLayout.PAGE_START);
grid = new JPanel(new GridLayout(0, 2, 10, 10)); //any number of rows, 2 columns, H and V gap
JScrollPane sp = new JScrollPane(grid);
sp.setPreferredSize(new Dimension(800,600));
gp.add(sp, BorderLayout.CENTER);
this.setVisible(true);
return gp;
}
void addComponent() throws IOException {
System.out.println("adding");
grid.add(new JLabel("test"));
grid.add(new intf());
this.repaint();
frame.pack();
}
private static void createAndShowGUI() throws IOException {
//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new Workspace(), BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
And the intf.java class for the JInternalFrame:
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class intf extends JInternalFrame {
public intf () {
super("Document",
true, //resizable
true, //closable
true, //maximizable
true);//iconifiable
JPanel jp = new JPanel();
JLabel jl = new JLabel("Hi I'm a label");
jp.add(jl);
this.add(jp);
this.pack();
this.repaint();
}
}
Edit:
The JPanel is used to hold the Layout, I don't think I can assign one to the TabbedPane?
Users will load in images which will appear on the left side of the GridLayout (I will upgrade this to a GridBag). After selecting regions on said images, graphs will appear on the right side. This is also why we use a scrollPane so that there is no limit to the number of images loaded. The TabbedPane will be used to split the load if a user loads in too many images (this is all not in the demo code shown here and not relevant to the problem).
The code I posted is for testing why the JInternalFrame doesn't show, so it only shows what is relevant to getting that to work.
Thank you all for your input.
It turned out to be a simple mistake of not having a necessary .setVisible(true)
Under the addComponent() method, the problem is solved when I replace
grid.add(new intf());
with
intf a = new intf();
a.setVisible(true);
grid.add(a);
The radio buttons don't seem to be grouped together properly as one of them is slightly slanting towards the left. I am not sure what the error is. Everything in the code seems fine to me...I am not sure what is missing.
I have attached an image below showing the problem. The Ide I am using is NetBeans.
Thank you in advanced! :)
package pizzaorder2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.FlowLayout;
import javax.swing.SwingUtilities;
import javax.swing.JRadioButton;
public class PizzaOrder2 extends JFrame {
public static void main(String[] args) {
JFrame frame = new PizzaOrder2();
JRadioButton tomato = new JRadioButton("Tomato");
JRadioButton barbeque = new JRadioButton("Barbeque");
ButtonGroup group = new ButtonGroup();
group.add(tomato);
group.add(barbeque);
JPanel radiopanel = new JPanel();
radiopanel.add(tomato);
radiopanel.add(barbeque);
frame.getContentPane().add(radiopanel);
radiopanel.setBounds(240,330,110,70);
radiopanel.setOpaque(false);
tomato.setForeground(Color.white);
barbeque.setForeground(Color.white);
frame.setLayout(null);
frame.setSize(600, 700);
frame.getContentPane().setBackground(new Color(40, 80, 120));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Try adjusting radiopanel to this:
JPanel radiopanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
to explain a little better, you're just setting it up so items align on the left, rather than the center (which I believe is default). You will need to import FlowLayout for this.
The search bar is 'search_bar', I'm trying to move it to the left hand side of the box with 'j_panel.add(search_bar, BorderLayout.WEST);' but it doesn't move from the middle. Any ideas on how to do this? Thanks
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class Main {
public static void main(String args[]) {
JFrame jfrm = new JFrame("Test");
jfrm.setSize(1024, 600);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setResizable(false);
JTextField search_bar = new JTextField("Search...", 25);
search_bar.setPreferredSize(new Dimension(1,35));
JTextArea body = new JTextArea(32,83);
body.setPreferredSize(new Dimension());
body.setEditable(false);
JButton search_button = new JButton("Search");
JPanel j_panel = new JPanel();
j_panel.setLayout(new FlowLayout());
j_panel.add(search_bar, BorderLayout.WEST);
j_panel.add(search_button, null);
j_panel.add(body, BorderLayout.EAST);
j_panel.setBackground(Color.gray);
search_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
final String text = search_bar.getText();
body.setText(text);
}
});
jfrm.setContentPane(j_panel);
jfrm.setVisible(true);
}
}
j_panel.setLayout(new FlowLayout());
j_panel.add(search_bar, BorderLayout.WEST);
BorderLayout.WEST is not applicable for FlowLayout. If you want to use a BorderLayout, use one instead of FlowLayout.
You cannot position components exactly the way you'd like when using FlowLayout.
By using BorderLayout you are bound to have only 5 components, because BorderLayout can handle 5 components max.
I have a jsplitPane and 2 component. I want to not let the user click the divider to maximize the top component.
Here is my code.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class MovingJSplitPaneDivider {
public static void main(String[] a) {
JFrame horizontalFrame = new JFrame();
horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent topButton = new JButton("Left");
JComponent bottomButton = new JButton("Right");
final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(topButton);
splitPane.setBottomComponent(bottomButton);
horizontalFrame.add(splitPane, BorderLayout.CENTER);
horizontalFrame.setSize(150, 150);
horizontalFrame.setVisible(true);
splitPane.setDividerLocation(0.5);
}
}
What I want exactly is that to block the user when he wants to move down the divider.
The following code, as you see below:
public class app extends javax.swing.JFrame implements Runnable {
extends JFrame. But I also need it to extend JPanel, in order to make a transparent JPanel. The problem is that I can't extend both, java throws a mistake:
If I extend JPanel I'm able to make a transparent JPanel, but the program can't run because there's a mistake in a few lines of code (mistake that disappears if I extend JFrame).
However, if I extend JFrame the program will run just fine, but it keeps me away from doing a transparent JPanel. How can I solve this?
Basically, create your custom class extending from a JPanel, use setOpaque to false to make it transparent.
Create an instance of JFrame, set it to undecorated and adjust it's opacity separately.
Add the custom panel to the frame...
Example
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TransparentPanel {
public static void main(String[] args) {
new TransparentPanel();
}
public TransparentPanel() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
try {
BufferedImage img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/issue459.jpg"));
final JLabel label = new JLabel(new ImageIcon(img.getScaledInstance(-1, 200, Image.SCALE_SMOOTH)));
label.setLayout(new CardLayout());
JPanel menu = new JPanel(new GridBagLayout());
JButton button = new JButton("Show");
menu.add(button);
JPanel transparent = new JPanel(new GridBagLayout());
transparent.setOpaque(false);
transparent.add(new JLabel("Look, I'm see through"));
label.add(menu, "menu");
label.add(transparent, "transparent");
CardLayout layout = (CardLayout) label.getLayout();
layout.show(label, "menu");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CardLayout layout = (CardLayout) label.getLayout();
layout.show(label, "transparent");
}
});
add(label);
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
A class can only ever extend one other class, that's why it won't allow you to extend more than 1 class. here is an explanation why that is the case. Perhaps you should try reading this for more information about such window modifications (window transparency and shapes)
If you want specifically a transparent JPanel, perhaps you should look at this answer as it could explain it better than I could.