Recommend
 
 Thumb up
 Hide
7 Posts

BoardGameGeek» Forums » Everything Else » Chit Chat

Subject: Java Help III: NOW what am I doing wrong? rss

Your Tags: Add tags
Popular Tags: [View All]
♪ Isaäc Bickërstaff ♫
United States
Greer
South Carolina
flag msg tools
designer
Entropy Seminar:
badge
The results of a five yeer studee ntu the sekund lw uf thurmodynamiks aand itz inevibl fxt hon shewb rt nslpn raq liot.
Avatar
mb
I'm back, looking for some advice from the Java wizards of BGG. Here's what I have in a project that is underway:

import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;

public class DrawArcs extends JFrame {
public DrawArcs() {
setTitle("DrawArcs");
setLayout(new BorderLayout());
add(new ArcsPanel(), BorderLayout.NORTH);
add(new ButtonsPanel(), BorderLayout.SOUTH);
}

public static void main(String[] args) {
DrawArcs frame = new DrawArcs();
frame.setSize(250, 350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class ArcsPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponents(g);

int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);

int x = xCenter - radius;
int y = yCenter - radius;

g.fillArc(x, y, 2 * radius, 2* radius, 0, 30);
g.fillArc(x, y, 2 * radius, 2* radius, 90, 30);
g.fillArc(x, y, 2 * radius, 2* radius, 180, 30);
g.fillArc(x, y, 2 * radius, 2* radius, 270, 30);
}
}

class ButtonsPanel extends JPanel {
public void init() {
JButton jbtOff = new JButton("OFF");
JButton jbtSlow = new JButton("SLOW");
JButton jbtMed = new JButton("MED");
JButton jbtFast = new JButton("FAST");

JPanel jpButtons = new JPanel();
jpButtons.setLayout(new FlowLayout());
jpButtons.add(jbtOff);
jpButtons.add(jbtSlow);
jpButtons.add(jbtMed);
jpButtons.add(jbtFast);
}
}


For whatever reason, I can't get both the buttons and the arcs to show up on the JFrame at the same time. I've tried placing the buttons in different places in the code (under DrawArcs(), inside ArcsPanel, etc.), and placing it in a class of its own was a last ditch effort. Now, though, the arcs display as teeny-tiny, and I can't figure out what I'm doing incorrectly. Any help?

P.S.: I'm aware that the buttons don't do anything at this time. My ultimate goal is to use them to animate the arcs so that they look like a rotating fan, but first I want to make sure that the stuff displays correctly.
 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
An open-eyed man falling into the well
United States
Roswell
Georgia
flag msg tools
"I don’t believe in magic that works whether or not you believe in it!"
Avatar
mbmbmbmbmb
My Java is rusty, but it looks like the init() method for ButtonsPanel is never called (don't think this is done automatically), and since it doesn't add the buttons to itself they won't show up.

So what I think you need there is more like:

class ButtonsPanel extends JPanel {
public ButtonsPanel() {
super();
init();
}

public void init() {
//...
this.add(jbtOff);
this.add(jbtSlow);
this.add(jbtMed);
this.add(jbtFast);
}


As for the arcs, I would just play with the numbers and print out the width/height/radius to make sure they look right.
1 
 Thumb up
0.25
 tip
 Hide
  • [+] Dice rolls
John Earles
Canada
Toronto
Ontario
flag msg tools
badge
Avatar
mbmbmbmbmb
1) ArcsPanel should be in BorderLayout.CENTER (which is the main content pane) otherwise it is small. This is why your arcs are tiny.

add(new ArcsPanel(), BorderLayout.CENTER);
add(new ButtonsPanel(), BorderLayout.SOUTH);


2) init() is not called. Simply add the buttons in the constructor:

class ButtonsPanel extends JPanel {
public ButtonsPanel() {
super();
add(new JButton("OFF"));
add(new JButton("SLOW"));
add(new JButton("MED"));
add(new JButton("FAST"));
}
}
1 
 Thumb up
0.25
 tip
 Hide
  • [+] Dice rolls
Billy McBoatface
United States
Lexington
Massachusetts
flag msg tools
KGS is the #1 web site for playing go over the internet. Visit now!
badge
Yes, I really am that awesome.
Avatar
mbmbmbmbmb
Another way to fix it is to set the minimum size for the ArcsPanel. E.g., put this inside the ArcsPanel class:

public @Override Dimension getMinimumSize() {
return(new Dimension(100, 100)); /* Force the arcs to be at least 100x100 pixels. */
}


And you should be off and running. Usually when you override setMinimumSize, you would override setPreferredSize in a similar way.
1 
 Thumb up
0.25
 tip
 Hide
  • [+] Dice rolls
The Steak Fairy
United States
Columbia
South Carolina
flag msg tools
Games? People still play games??
badge
Specious arguments are not proof of trollish intent.
Avatar
mbmbmbmbmb
Hmm, I was just going to suggest that you ditch Java and play Mexica, but then I remembered which forum this was.
3 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
♪ Isaäc Bickërstaff ♫
United States
Greer
South Carolina
flag msg tools
designer
Entropy Seminar:
badge
The results of a five yeer studee ntu the sekund lw uf thurmodynamiks aand itz inevibl fxt hon shewb rt nslpn raq liot.
Avatar
mb
Thanks guys! That did it.

Now as for the animation of the arcs into a fan, I'd appreciate it if y'all could chime in on what you think of my method. I was thinking of setting timers associated with the three speeds, each different depending on how fast I want it to go. I was also thinking of having the paintComponent method work in a loop, redrawing the arcs one degree more, based on my timers. Then I would have all the different timers attached to ActionListeners to work with the buttons.

Would that work?
 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
John Earles
Canada
Toronto
Ontario
flag msg tools
badge
Avatar
mbmbmbmbmb
Verkisto wrote:
Thanks guys! That did it.

Now as for the animation of the arcs into a fan, I'd appreciate it if y'all could chime in on what you think of my method. I was thinking of setting timers associated with the three speeds, each different depending on how fast I want it to go. I was also thinking of having the paintComponent method work in a loop, redrawing the arcs one degree more, based on my timers. Then I would have all the different timers attached to ActionListeners to work with the buttons.

Would that work?


I would avoid a loop implementation. Instead have a MultiTimer object which can operate at three speeds, that extends Observable. Your buttons would trigger the timer to switch speeds (or stop). Have your ArcsPanel object implement Observer, and the update method would perform a redraw. You can then call addObserver on the Timer object so that it knows about the callback method on the ArcsPanel. When the timer goes off you should only have to call setChanged() and notifyObservers() which will trigger the object to notify all of its Observers of timer event (and the ArcsPanel will repaint).

Edit: By "operate at three speeds" I don't actually mean that there are multiple timers. The timer would simply operate at a fixed "heartbeat", but it also has a current "speed" attribute that it send along to any Observers. The ArcsPanel can use the "speed" to change its internal pace (i.e. the number of degrees to shift with each heartbeat).
1 
 Thumb up
 tip
 Hide
  • [+] Dice rolls
Front Page | Welcome | Contact | Privacy Policy | Terms of Service | Advertise | Support BGG | Feeds RSS
Geekdo, BoardGameGeek, the Geekdo logo, and the BoardGameGeek logo are trademarks of BoardGameGeek, LLC.