Panels on a BorderLayout
- From: fb <fb@xxxxxxxxx>
- Date: Tue, 07 Mar 2006 04:05:02 GMT
Hi. I have just finished this application that draws a rectangle inside a panel & uses a thread or a timer to animate it. The problem I think I'm having is that I can't see the panels on the contentPane. I can make them animate one at a time in the center of a BorderLayout, but I can't seem to get them to work together in east and west Panels.
I tried to follow the SSCCE methodology, but may have failed on the "Short" bit.
Any Ideas on how to fix this...or what's wrong?
ttyl.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
class Ass8_1 extends JFrame {
//the data class
class ThreadDO {
int x = 0;
int size = 0;
public void setX(int in_x) {
x = in_x;
}
public void setSize(int in_size) {
size = in_size;
}
public int getX() {
return x;
}
public int getSize() {
return size;
}
}
ThreadDO threadData = new ThreadDO(); //create data object
//the timer data class
class TimerDO {
int y = 10;
int size = 0;
public void setY(int in_y) {
y = in_y;
}
public void setSize(int in_size) {
size = in_size;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
}
//create data object
TimerDO timerData = new TimerDO();
//here is the panel, where we will do the drawing
class DrawPanelX extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//get x location from data object
float x = (float)threadData.getX();
//get size
float size = (float)threadData.getSize();
Rectangle2D.Float myRect = new Rectangle2D.Float(x,10,size,size);
g2d.drawString("IBM Stock Prices, or... Why You Shouldn't"+
" Bet on the Stock Market...",10,50);
g2d.setColor(Color.blue);
g2d.fill(myRect);
}
}
DrawPanelX thePanelX = new DrawPanelX(); //create draw panel
//here is the panel, where we will do the drawing
class DrawPanelY extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//get x location from data object
float y = (float)timerData.getY();
//get size
float size = (float)timerData.getSize();
Rectangle2D.Float myRect = new Rectangle2D.Float(10,y,size,size);
g2d.drawString("Dell Stock Prices, or... Why You Should"+
" Invest in the Stock Market...",10,50);
g2d.setColor(Color.magenta);
g2d.fill(myRect);
}
}
DrawPanelY thePanelY = new DrawPanelY(); //create draw panel
class MyTC extends Thread{
boolean x_move = true;
public void run() {
while (1 == 1) {
int x = threadData.getX(); // get current values
int size = threadData.getSize();
if (x_move == true) {
x = x + 1;
if (x == 380) {
x_move = false;
}
}
else if(x_move == false){
x = x - 1;
if (x == 0) {
x_move = true;
}
}
//Change size
size = (int) ((size + 2) % 22) + 2;
try {Thread.sleep(50);} catch (InterruptedException e) {}
threadData.setX(x);
threadData.setSize(size);
thePanelX.repaint();
}
}
}
//My Timer Listener
class MyTimerL implements ActionListener {
boolean y_move = true;
public void actionPerformed(ActionEvent e) {
int y = timerData.getY();
int size = timerData.getSize();
if (y_move == true) { // change x/y in a box motion
y = y + 1;
if (y == 380) {
y_move = false;
}
}
else if(y_move == false){
y = y - 1;
if (y == 0) {
y_move = true;
}
}
size = (int) ((size + 2) % 22) + 2;
timerData.setY(y);
timerData.setSize(size);
thePanelY.repaint(); //force panel to repaint
}
}
public void startup() {
this.setTitle("Assignment 8_1 Frame");
this.setSize(420,500);
this.addWindowListener(new ExitListener());
this.setLayout(new BorderLayout());
Container theCP = this.getContentPane();
theCP.setLayout(new BorderLayout());
//Add X-axis rectangle to east Pane
theCP.add(thePanelX, BorderLayout.EAST);
//Add Y-axis rectangle to west Pane
theCP.add(thePanelY, BorderLayout.WEST);
//create and start thread
MyTC theT = new MyTC();
theT.start();
//create, attach listener, and start the first timer
MyTimerL theTL = new MyTimerL();
Timer theTimer = new Timer(50,theTL);
theTimer.start();
this.setVisible(true);
}
class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
}
class ABootUp8_1 {
public static void main(String [] args) {
Ass8_1 theFrame = new Ass8_1();
theFrame.startup();
}
}
.
- Follow-Ups:
- Re: Panels on a BorderLayout
- From: IchBin
- Re: Panels on a BorderLayout
- From: Bart Cremers
- Re: Panels on a BorderLayout
- Prev by Date: Windows install locations J2SE and Tomcat
- Next by Date: Re: Panels on a BorderLayout
- Previous by thread: Windows install locations J2SE and Tomcat
- Next by thread: Re: Panels on a BorderLayout
- Index(es):
Relevant Pages
|