Trying to move a graphics object using buttons.

Hello, im fairly new to GUI's. Anyway I have 1 class which makes my main JFrame, then I have another 2 classes, one to draw a lil square graphics component (which iwanna move around) which is placed in the center of my main frame and then another class to draw a Buttonpanel with my buttons on which is placed at the bottom of my main frame.

I have then made an event handling class which implements ActionListner, I am confused at how I can get the graphics object moving, and where I need to place the updateGUI() method which the actionPerformed method calls from inside the event handling class.

I am aware you can repaint() graphics and assume this would be used, does anyone have a good example of something simular being done or could post any help or code to aid me, thanks!

[802 byte] By [tranquility2k4a] at [2007-11-26 15:39:19]
# 1
The easiest way is to do you custom painting on a JComponent. Then to move the component around the panel you just use the setLocation() method of the component and it will repaint itself.
camickra at 2007-7-8 21:57:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

Yeah.. here's an example of custom painting on a JPanel with a box. I used a mouse as it was easier for me to setup than a nice button panel on the side.

Anyways... it should make it pretty clear how to get everything setup, just add a button panel on the side. and use it to move the box instead of the mouse.

-Js

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.event.MouseInputAdapter;

public class MoveBoxAroundExample extends JFrame

{

private final static int SQUARE_EDGE_LENGTH = 40;

private JPanel panel;

private int xPos;

private int yPos;

public MoveBoxAroundExample()

{

this.setSize(500,500);

this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

this.setContentPane(getPanel());

xPos = 250;

yPos = 250;

this.setVisible(true);

}

private JPanel getPanel()

{

if(panel == null)

{

panel = new JPanel()

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setColor(Color.RED);

g.fillRect(xPos-(SQUARE_EDGE_LENGTH/2), yPos-(SQUARE_EDGE_LENGTH/2), SQUARE_EDGE_LENGTH, SQUARE_EDGE_LENGTH);

}

};

MouseInputAdapter mia = new MouseInputAdapter()

{

public void mousePressed(MouseEvent e)

{

xPos = e.getX();

yPos = e.getY();

panel.repaint();

}

public void mouseDragged(MouseEvent e)

{

xPos = e.getX();

yPos = e.getY();

panel.repaint();

}

};

panel.addMouseListener(mia);

panel.addMouseMotionListener(mia);

}

return panel;

}

public static void main(String args[])

{

new MoveBoxAroundExample();

}

}

JSnakea at 2007-7-8 21:57:41 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for the reply, I very much apreciate your time and effort for writing out that code and it has proved useful, however. I had figured most of what you stated out before by reading books and looking through examples on websites.

My problem is that, where as in almost every example I see, you have just one class which contains everything else in it. In my case, I have 4 different classes.

1. JFrame Class

2. ButtonPanel Class (Which is placed on JFrame at the bottom)

3. GraphicsCpt Class (Which is placed on JFrame in the center)

4. EventHandling Class (Which responds to the button presses)

I have an actionPerformed(ActionEvent e) method inside the EventHandling class, this actionPerformed method then calls an updateGUI(); method sending the co-ordinates of which I want to move my graphicsCpt, depending on whether I have pressed the up, down, left or right button.

I am unsure as to where to put the updateGUI(); method, theoretically I think I need to place it within the ButtonPanel Class, as I need to assign the action listener to the buttons inside there. But if I do this, my graphicsCpt doesn't seem to repaint correctly. Where as if I place the updateGU(); method inside the JFrame Class, I can get it moving, but that then means I have to make all my buttons in there rather than in a seperate class.

Anyone have any solutions to this problem?

tranquility2k4a at 2007-7-8 21:57:41 > top of Java-index,Desktop,Core GUI APIs...
# 4
-Bump-HELP please :)
tranquility2k4a at 2007-7-8 21:57:41 > top of Java-index,Desktop,Core GUI APIs...
# 5
If you want to repaint a component, then you need a reference to that component and you invoke the repaint() method. Thats all there is to it. How you pass the reference from class to class is up to your design.
camickra at 2007-7-8 21:57:41 > top of Java-index,Desktop,Core GUI APIs...
# 6

Your orginal post said "does anyone have a good example of something simular being done or could post any help"?

I thought I did that. It has your basic concepts packaged up all nicely for you. How you seperate it out is up to you. I'm not going to build your project for you. If you need this level of specific help, you might want to setup a SSCCE of your setup, and maybe someone will go over design issues with you.

JSnakea at 2007-7-8 21:57:41 > top of Java-index,Desktop,Core GUI APIs...