Thanks tim, though it's only 5 lines of ... well... easy code but still took me a while to figure it out
Thanks for your adivce. The way of declare attributes which i read from the book is exactlly what you said(and i like to do it that way). but in my class teacher doesn't like that... he says all those attributes that we declacred are pretty much once for all, so he wanted us to put them at the end of the code so everytime we open up the code don't have read all those "useless" parts ;o (and if he sees we declare them at start = points off)
About the inner classes, abstraction(i guess it's a method?) i haven't got a chance to learn them yet, maybe later when i get to put one of my feet at the door step i will start to make my codes look better
A question for a total beginner, no other languages learned. How do i figure things(codes, methods e.g) out? This sounds really stupid, the class i'm taking is 2nd beginning java class i think. Teacher gives the examples, a template, which i would call a code skeleton that is this animation template i'm learning. During class, we learn the methods of drawing and the math(got no problems on that), but then, on homework projects, even we have books and internet to search for methods and syntax of how to use them, i still feel like i'm doing my own discoveries. Sometimes i'm lucky and i get it to work, others i just have no idea at all. Like the codes you helped me with, those 5 lines i just don't see i have a crank at it.
Here is another problem i can't figure out. Animate Ball moving in a Sine cruve, and got it to work, but how can i make the ball tangent to the frame window? In my last post hardwired helped me with the TangentBall problem, and i was able to solve the problem by adding other 3 balls in. I tried to put the math method he did in the Sine project but it doesn't work, i tried to "edit" it myself it just gets worse...
|
Code:
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class SineCurve extends JFrame
{
public void lauchFrame()
{
int Row,Col;
Toolkit MyKit = Toolkit.getDefaultToolkit();
Dimension XY = MyKit.getScreenSize();
Row = XY.width;
Col = XY.height;
Image MyImage = MyKit.getImage(":");
this.setIconImage(MyImage);
this.setTitle("JButton-Sun-Rise Color Test");
this.setSize(7*Row/8, 7*Col/8);
this.setLocation(Row/32,Col/32);
this.setTitle("Sine");
this.setResizable(false);
MyPanel Panel01 = new MyPanel();
Container ContentPanel01 = getContentPane();
ContentPanel01.add(Panel01);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
private class MyPanel extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
Graphics2D gr = (Graphics2D) g;
int X, Y, x1, x2, y1, y2;
int Row, Col;
double Frequency;
Dimension RowCol;
RowCol = getSize();
Row = RowCol.width;
Col = RowCol.height;
g.setColor(Color.green);
g.drawRect(1,1,Row-2,Col-2);
g.setColor(Color.blue);
g.drawLine(0,Col/2,Row,Col/2);
g.drawLine(Row/2,0,Row/2,Col);
g.setColor(Color.red);
Frequency = 1;
Radius = 20;
x1 = Action;
y1 = Col/2 - (int)(Col/2*Math.sin(Frequency*2.0*Math.PI/(Row)*x1));
/*if (Action < Row/2)
{
x1 = Action+Radius;
y1 = Col/2+Radius - (int)(Col/2*Math.sin(Frequency*2.0*Math.PI/(Row)*x1));
}*/
g.setColor(new Color(0,150,150));
Ellipse2D C1 = new Ellipse2D.Double();
C1.setFrameFromCenter(x1,y1,x1+Radius,y1+Radius);
gr.fill(C1);
}
public MyPanel()
{
Action = 0;
JButton StartButton = new JButton("Start Running");
add(StartButton);
ClickStart Start = new ClickStart();
StartButton.addActionListener(Start);
}
private class ClickStart implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ActionListener WakeUp = new TimeStamp();
Timer T = new Timer(10,WakeUp);
T.start();
}
private class TimeStamp implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Action ++;
repaint();
if (Action == Row-40)
{
Action = 0;
}
}
}
}
private int Action,Frequency,y2,Row,Radius;
}
} |