I am new to graphics in java and I am currently working on a game. Essentially, there are rising bubbles, and the user has to pop them by moving the mouse over them.
I have already made an animation on the JFrame and I need to add a JPanel for a MouseMotionListener on top. However, when I add the JPanel on top of the JFrame (even with the setOpaque to false) it still does not let me see my animation underneath. You can see my code below. If you find coding errors, please let me know.
I have two solutions in mind, either animate in JPanel (which I don't know how to do), or make the JPanel transparent.
Game Class:
public class Game extends JPanel{
public static final int WINDOW_WIDTH = 600;
public static final int WINDOW_HEIGHT = 400;
private boolean insideCircle = false;
private static boolean ifPaused = false;
private static JPanel mainPanel;
private static JLabel statusbar;
private static int clicks = 0;
//Creates a Bubbles object Array
Bubbles[] BubblesArray = new Bubbles[5];
public Game() {
//initializes bubble objects
for (int i = 0; i < BubblesArray.length; i++)
BubblesArray[i] = new Bubbles();
}
public void paint(Graphics graphics) {
//makes background white
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
//paints square objects to the screen
for (Bubbles aBubblesArray : BubblesArray) {
aBubblesArray.paint(graphics);
}
}
public void update() {
//calls the Square class update method on the square objects
for (Bubbles aBubblesArray : BubblesArray) aBubblesArray.update();
}
public static void main(String[] args) throws InterruptedException {
statusbar = new JLabel("Default");
mainPanel = new JPanel();
mainPanel.setOpaque(false);
mainPanel.setBackground(new Color(0,0,0,0));
mainPanel.setVisible(true);
mainPanel.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
}
public void mouseExited(MouseEvent e){
ifPaused = true;
}
public void mouseEntered(MouseEvent e){
ifPaused = false;
}
});
Game game = new Game();
JFrame frame = new JFrame();
frame.add(game);
frame.add(mainPanel);
frame.add(statusbar, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Bubble Burst");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
while (true) {
if (!ifPaused){
game.update();
game.repaint();
Thread.sleep(5);
}
}
}
}
Your game panel is not showing up because you are adding two components to the same location of a BorderLayout (at BorderLayout.CENTER). Therefore mainPanel is not being added "on top of" game, it is replacing it. That being said:
It doesn't seem like your mainPanel is actually doing anything except listening for mouse motion. Could you not just add the MouseAdapter to your game object since it extends JPanel like this:
game.addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
statusbar.setText(String.format("Your mouse is at %d, %d", e.getX(), e.getY()));
}
public void mouseExited(MouseEvent e){
ifPaused = true;
}
public void mouseEntered(MouseEvent e){
ifPaused = false;
}
});
Related
I have 16 Jpanels that I want to be highlighted when I hover my mouse over them. I created the JPanels anonymously and then added them to a parent, and added a MouseListener to each of them. I then added a MouseListener to the parent. The thing is, now it just highlights the parent. How can I fix this?
NOTE: Sometimes the JFrame doesn't show anything - you just have to keep running it until it does (usually takes 2-3 tries). Comment if it still isn't working after >5 tries.
HighlightJPanels (creates the JFrame, the container, and the children, and adds the MouseListeners)
public class HighlightJPanels extends JFrame{
private static final long serialVersionUID = 7163215339973706671L;
private static final Dimension containerSize = new Dimension(640, 477);
private JLayeredPane layeredPane;
static JPanel container;
public HighlightJPanels() {
super("Highlight Test");
setSize(640, 477);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(containerSize);
getContentPane().add(layeredPane);
createContainer();
layeredPane.add(container, JLayeredPane.DEFAULT_LAYER);
createChildren(4, 4);
container.addMouseMotionListener(new HighlightJPanelsContainerMouseListener());
}
private void createChildren(int columns, int rows){
for (int i = 0; i < columns; i++){
for (int j = 0; j < rows; j++){
JPanel child = new JPanel(new BorderLayout());
child.setBackground(Color.LIGHT_GRAY);
child.addMouseListener(new HighlightJPanelsMouseListeners());
container.add(child);
}
}
}
private JPanel createContainer(){
container = new JPanel();
container.setLayout(createLayout(4, 4, 1, 1));
container.setPreferredSize(containerSize);
container.setBounds(0, 0, containerSize.width, containerSize.height);
return container;
}
private GridLayout createLayout(int rows, int columns, int hGap, int vGap){
GridLayout layout = new GridLayout(rows, columns);
layout.setHgap(hGap);
layout.setVgap(vGap);
return layout;
}
public static void main(String[] args) {
new HighlightJPanels();
}
}
HighlightJPanelsChildMouseListeners (creates the MouseListeners that will be added to the children)
public class HighlightJPanelsChildMouseListeners implements MouseListener{
private Border grayBorder = BorderFactory.createLineBorder(Color.DARK_GRAY);
public HighlightJPanelsChildMouseListeners() {
}
public void mouseEntered(MouseEvent e) {
Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
JPanel parent = (JPanel) comp;
parent.setBorder(grayBorder);
parent.revalidate();
}
public void mouseExited(MouseEvent e) {
Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
JPanel parent = (JPanel) comp;
parent.setBorder(null);
parent.revalidate();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
}
HighlightJPanelsContainerMouseListener (creates the MouseListener that will be added to the container)
public class HighlightJPanelsContainerMouseListener implements MouseMotionListener{
static int eX;
static int eY;
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
eX = e.getX();
eY = e.getY();
}
}
The problem is being caused by how you find the JPanel to highlight, on this line:
Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
Fortunately, there's already a function that will do what you want. You can just use getSource() on the event, and it will tell you which panel to highlight. So change your function to this:
public void mouseEntered(MouseEvent e) {
JPanel parent = (JPanel)e.getSource();
parent.setBorder(grayBorder);
parent.revalidate();
}
and do the same thing with mouseExited, and you'll see it highlight the correct panel. And this will remove the need for HighlightJPanelsContainerMouseListener.
I'm trying to code a little maze runner program, and have run into some trouble relating to the paintComponent(). I have gone through the debug and for some reason my paintComponent() is never called, even with the repaint() which is being called by my timer.
private void jpanelinit() {
JPanel Background = new JPanel (new BorderLayout());
JPanel Menu = new JPanel (new BorderLayout());
JPanel Maze = new JPanel (new GridLayout(arow, acolumn));
Background.setPreferredSize(new Dimension(850,850));
Menu.setPreferredSize(new Dimension(850, 100));
Maze.setPreferredSize(new Dimension(850,750));
frame.add(Background);
comboboxinit();
Background.add(Menu, BorderLayout.NORTH);
Background.add(Maze, BorderLayout.SOUTH);
Menu.add(Startpause, BorderLayout.WEST);
Menu.add(Reset, BorderLayout.EAST);
Menu.add(Intervalpick, BorderLayout.CENTER);
Intervalpick.setVisible(true);
Intervalpick.addActionListener(this);
Startpause.setVisible(true);
Startpause.addActionListener(this);
Reset.setVisible(true);
Reset.addActionListener(this);
Maze.setVisible(true);
}
private static void frameinit() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(850,850);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
Those are my frame and jpanel init methods.
#Override
public void paintComponent(Graphics g){
System.out.println("Entered Graphics");
super.paintComponent(g);
g.drawImage(biWall,0,100,850,750, this );
}
This is my paintComponent, the image is indeed buffered and has been stored.
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Intervalpick)
timecheck(); //Checks if the time was changed
if(e.getSource()==Startpause||e.getSource()==Reset)
buttoncheck(e); //Checks if the buttons were pressed
if(t.isRunning())
mazeupdate();
}
private void mazeupdate() {
repaint();
}
That is my actionPerformed which is called with my timer which is set to a 5 sec interval at default.
public class mazerunner extends JPanel implements ActionListener {
static JButton Startpause = new JButton("Start");
static JButton Reset = new JButton("Reset");
static JComboBox Intervalpick= new JComboBox();
static JFrame frame=new JFrame ("Maze Runner");
static int arow=0, acolumn=0, icurrenttime=5000;
static boolean gameplaying=false;
static Timer t;
static BufferedImage biWall, biFloor, biMouse, biCheese;
public static void main(String[] args) {
mazerunner mr= new mazerunner();
filereader(); //Reads the file
imagebuffer(); //Buffers the images
mr.jpanelinit(); //Inits the gui
frameinit(); //Inits the frame
mr.timerinit(); //Inits the timer
}
private static void imagebuffer() {
try{
biWall=ImageIO.read(new File("cobblewall.jpg"));
}
catch(IOException e){}
try{
biFloor=ImageIO.read(new File("woodenfloor.jpg"));
}
catch(IOException e){}
try{
biCheese=ImageIO.read(new File("chest_cheese.jpg"));
}
catch(IOException e){}
try{
biMouse=ImageIO.read(new File("lara_mouse.jpg"));
}
catch(IOException e){}
}
private void timerinit() {
t=new Timer(icurrenttime,this);
}
private void jpanelinit() {
JPanel Background = new JPanel (new BorderLayout()); //Inits all the JPanels
JPanel Menu = new JPanel (new BorderLayout());
JPanel Maze = new JPanel (new GridLayout(arow, acolumn));
Background.setPreferredSize(new Dimension(850,850)); //Sets the size of the panels
Menu.setPreferredSize(new Dimension(850, 100));
Maze.setPreferredSize(new Dimension(850,750));
frame.add(Background); //Adds background into the frame
comboboxinit();
Background.add(Menu, BorderLayout.NORTH); //Adds the other panels into the background
Background.add(Maze, BorderLayout.SOUTH);
Menu.add(Startpause, BorderLayout.WEST); //Adds the menu's components into the menu panel
Menu.add(Reset, BorderLayout.EAST);
Menu.add(Intervalpick, BorderLayout.CENTER);
Intervalpick.setVisible(true); //Sets the components to visible and adds actionlistener
Intervalpick.addActionListener(this);
Startpause.setVisible(true);
Startpause.addActionListener(this);
Reset.setVisible(true);
Reset.addActionListener(this);
Maze.setVisible(true);
}
private static void comboboxinit() {
for(int a=5;a<=30;a=a+5){ //Sets the text inside the combobox
Intervalpick.addItem(a+" Seconds");
}
DefaultListCellRenderer dlcr = new DefaultListCellRenderer(); //Centers the text inside the combobox
dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER);
Intervalpick.setRenderer(dlcr);
}
private static void frameinit() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Inits the jframe
frame.setResizable(false);
frame.setSize(850,850);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
private static void filereader() {
try{
FileReader fr=new FileReader("maze.txt");
BufferedReader br= new BufferedReader(fr);
String LineIn=br.readLine();
int num, row=0;
arow=Integer.parseInt(LineIn);
LineIn=br.readLine();
acolumn=Integer.parseInt(LineIn);
int Maze[][]=new int[arow][acolumn]; //inits the maze itself
LineIn=br.readLine();
do{ //stores the maze from the file into the arrray
int collumn=0;
StringTokenizer tokens=new StringTokenizer(LineIn);
while (tokens.hasMoreTokens()){
num=Integer.parseInt(tokens.nextToken());
Maze[row][collumn]=num;
collumn++;
}
LineIn=br.readLine();
row++;
}while(LineIn!=null);
br.close();
fr.close();
}
catch(FileNotFoundException e){
System.err.println("file not found");
}
catch(IOException e){
System.err.println("read failed");
}
}
#Override
public void paintComponent(Graphics g){
System.out.println("Entered Graphics");
super.paintComponent(g);
g.drawImage(biWall,0,100,850,750, this );
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Intervalpick)
timecheck(); //Checks if the time was changed
if(e.getSource()==Startpause||e.getSource()==Reset)
buttoncheck(e); //Checks if the buttons were pressed
if(t.isRunning())
mazeupdate();
}
private void mazeupdate() {
repaint();
}
private void buttoncheck(ActionEvent e) {
if(e.getSource()==Startpause){ //Starts and pauses the simulation
if(gameplaying==false){
gameplaying=true;
t.start();
Startpause.setText("Pause");
}
else if(gameplaying==true){
gameplaying=false;
t.stop();
Startpause.setText("Start");
}
}
else if(e.getSource()==Reset){ //Resets the maze to the original maze
if(t.isRunning())
t.stop();
gameplaying=false;
Startpause.setText("Start");
filereader();
// t.repaint();
}
}
private void timecheck() {
int boxtime= Integer.parseInt(((String) Intervalpick.getSelectedItem()).split(" ")[0])*1000; //Turns Intervalpick into a milisecond amount
if(boxtime!=icurrenttime){ //Checks if the selected delay is equal to the actual delay
boolean gamerunning=false;
icurrenttime=boxtime;
if(t.isRunning()){ //Stops timer if running
t.stop();
gamerunning=true;
}
t.setDelay(icurrenttime); //Changes delay
if(gamerunning==true) //If timer was running it turns it back on
t.start();
}
}
}
That is my full code if you're interested. My question is why I can't get the image to draw onto the Maze panel.
paintComponent won't be called for three basic reasons.
The component isn't attached to a valid native peer (ie, it's not attached to a window directly or indirectly that is visible on the screen)
or it doesn't have a size greater than 0x0
or it's not visible...
Skimming through your code, I can't find anywhere an instance of mazerunner is actually added to the frame...
You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
so this is my problem. I have an 8*8 grid of panels, all white. Then, when one of them is clicked, it's supposed to change to a random color. The only problem I have right now is that I don't know how to see if the user clicked their mouse in a specific panel. Here is the code I have so far (I'm going to implement the random element afterwards)
`
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GridOfPanels extends JPanel{
int x, y;
public GridOfPanels(){
JPanel content = new JPanel(new GridLayout(8,8));
for(int i = 0; i < 64; i++){
JPanel panel = new JPanel();
panel.setBackground(Color.white);
content.add(panel);
}
this.add(content);
}
public GridOfPanels(Color backColor){
setBackground(backColor);
addMouseListener(new PanelListener());
x = 200;
y = 200;
}
private class PanelListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
}
public static void main(String[] args){
JFrame theGUI = new JFrame();
theGUI.setTitle("Grid");
theGUI.setVisible(true);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setSize(400,400);
Rectangle z = new Rectangle(x, y, 50, 50);
}
}
`
You have to add a listener to each clickable object. Here is a working example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestFrame extends JFrame{
public TestFrame(int size){
JPanel content = new JPanel(new GridLayout(size, size));
JPanel[] panel = new JPanel[size * size];
PanelListener listener = new PanelListener();
for(int i = 0; i < panel.length; i++){
panel[i] = new JPanel();
panel[i].setBackground(Color.white);
panel[i].addMouseListener(listener);
content.add(panel[i]);
}
this.add(content);
}
// MouseListener offers the method mouseClicked(MouseEvent e)
private class PanelListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent event) {
/* source is the object that got clicked
*
* If the source is actually a JPanel,
* then will the object be parsed to JPanel
* since we need the setBackground() method
*/
Object source = event.getSource();
if(source instanceof JPanel){
JPanel panelPressed = (JPanel) source;
panelPressed.setBackground(Color.blue);
}
}
#Override
public void mouseEntered(MouseEvent arg0) {}
#Override
public void mouseExited(MouseEvent arg0) {}
#Override
public void mousePressed(MouseEvent arg0) {}
#Override
public void mouseReleased(MouseEvent arg0) {}
}
public static void main(String[] args){
TestFrame theGUI = new TestFrame(8);
theGUI.setTitle("Grid");
theGUI.setVisible(true);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setSize(400,400);
}
}
You have to add MouseListener to one of the panels. Only one panel will react to click event. In the listener cast the source to JPanel and change the color.
I'm wanting the panel to expand and display buttons and stuff when the mouse is hovered over, then apon mouse exiting the panel must return top original size.
So far I can only print a message:
public JPanel GUI()
{
final JPanel totalGUI = new JPanel();
totalGUI.setBackground(Color.blue);
totalGUI.setLayout(null);
//+++++++++++++++
// - - - - - - PANEL 1!
//+++++++++++++++
JPanel SearchPanel = new JPanel(); //Create new grid bag layout
SearchPanel.setLocation(5, 5);
SearchPanel.setSize(420, 120);
totalGUI.add(SearchPanel);
SearchPanel.addMouseListener(this);
return totalGUI;
}
public void mouseEntered(MouseEvent e) {
System.out.print("Mouse entered");
}
public void mouseExited(MouseEvent e) {
System.out.print("Mouse exited");
}
private static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("RWB");
gay3 demo = new gay3();
frame.setContentPane(demo.GUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(650, 500);
frame.setVisible(true);
}
public static void main(String[] args)
{
createAndShowGUI();
}
You could do this inside the mouseEntered and mouseExited methods:
JPanel source = (JPanel)e.getSource();
//Edit the searchPanel here
If searchPanel isn't the only component that uses this MouseListener, you may first have to check if the source really is an instance of JPanel:
Object o = e.getSource();
if(o instanceof JPanel) {
JPanel source = (JPanel)o;
//Edit the searchPanel here
}
This isn't necessary if you know the source is a JPanel.
I've hit a wall (in my brain) trying to update my board on button presses. Am I right in thinking that the GameBoard class is the one that needs to be repaint()ed?
GameBoard.java
public class GameBoard extends Panel {
static Compass compass = new Compass();
private static final long serialVersionUID = 1;
Graphics2D g2d;
static final Dimension WINDOW_SIZE = new Dimension(1150, 800);
public void boardMaker() throws Exception {
JFrame frame = new JFrame("Display image");
JPanel panel = new JPanel();
/* unimportant stuff
.....
*/
//
DieRoll roll = new DieRoll("Roll Dies");
roll.setC(compass);
roll.setG2D(g2d);
//
Button button = new Button("new");
button.setGameBoard(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
buttonPanel.add(roll);
buttonPanel.setPreferredSize(new Dimension(200,100));
frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
//
frame.getContentPane().add(panel);
frame.setVisible(true);
}
public void paint(Graphics g) {
// not important I think
}
}
Button.java
public class Button extends JButton implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JFrame frame = new JFrame();
Compass c = new Compass();
GameBoard gb = new GameBoard();
Button(String text) {
this.setText(text);
this.addActionListener(this);
}
void setGameBoard(GameBoard gb) {
this.gb = gb;
}
#Override
public void actionPerformed(ActionEvent e) {
gb.g2d.setColor(Color.black);
gb.g2d.fillRect(100, 100, 100, 200);
gb.repaint();
}
}
This gives a null pointer exception. So any idea how to repaint my GameBoard? I'm not mad if I've to rewrite everything because of stupidity! ;)
Thanks
You have the wrong idea about how to draw in Java. Components like Panels draw themselves, and all drawing takes place on the UI thread.
Check out this tutorial: docs.oracle.com/javase/tutorial/2d/index.html
The article Painting in AWT and Swing may offer some perspective on application-triggered painting. The example below illustrates the principle. Note that setForeground() calls repaint() automatically because the foreground color is a bound property, but you can always call it yourself.
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class SwingPaint {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
final GamePanel gp = new GamePanel();
f.add(gp);
f.add(new JButton(new AbstractAction("Update") {
#Override
public void actionPerformed(ActionEvent e) {
gp.update();
}
}), BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
private static class GamePanel extends JPanel {
private static final Random r = new Random();
public GamePanel() {
this.setForeground(new Color(r.nextInt()));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
public void update() {
this.setForeground(new Color(r.nextInt()));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension size = this.getSize();
int d = Math.min(size.width, size.height) - 10;
int x = (size.width - d) / 2;
int y = (size.height - d) / 2;
g.fillOval(x, y, d, d);
g.setColor(Color.blue);
g.drawOval(x, y, d, d);
}
}
}