新手学java,想实现在JPanel上动态画出余弦函数图像,以下代码有什么问题?
packagecom.text3;importjava.awt.*;importjavax.swing.*;publicclasscosextendsJFrame{JFr...
package com.text3;
import java.awt.*;
import javax.swing.*;
public class cos extends JFrame {
JFrame jf=null;
mypanel jp=null;
public cos()
{
jf=new JFrame();
jp=new mypanel();
jf.add(jp);
jf.setTitle("绘制cos三角函数");
jf.setSize(800,800);
jf.setLocation(30, 56);;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args)
{
new cos();
mypanel s=new mypanel();
Thread t =new Thread(s);
t.start();
// System.out.println("正在执行");
}
}
class mypanel extends JPanel implements Runnable {
int xp=0;
int yp=0;
public mypanel()
{ //xp=6;
}
public void run()
{
while(xp<720)
{
repaint();
try
{
Thread.sleep(100);
//System.out.println("正在执行");
} catch(Exception e)
{
e.printStackTrace();
}
xp+=1;
System.out.println("正在执行");
}
}
public void drawxy(Graphics g)
{
double a=0;
a=Math.cos(xp*Math.PI/180);
yp=(int)(a*50+200);
g.drawString(".", xp, yp);
}
public void paint(Graphics g)
{
//super.paint(g);
//绘制x轴和y轴
g.drawLine(360, 100, 360, 300);
g.drawLine(355, 105, 360, 100);
g.drawLine(365, 105, 360, 100);
g.drawLine(260, 200 , 460, 200);
g.drawLine(455, 195, 460, 200);
g.drawLine(455, 205, 460, 200);
g.drawString("y", 346, 105);
g.drawString("x", 460, 212);
this.drawxy(g);
}
} 展开
import java.awt.*;
import javax.swing.*;
public class cos extends JFrame {
JFrame jf=null;
mypanel jp=null;
public cos()
{
jf=new JFrame();
jp=new mypanel();
jf.add(jp);
jf.setTitle("绘制cos三角函数");
jf.setSize(800,800);
jf.setLocation(30, 56);;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args)
{
new cos();
mypanel s=new mypanel();
Thread t =new Thread(s);
t.start();
// System.out.println("正在执行");
}
}
class mypanel extends JPanel implements Runnable {
int xp=0;
int yp=0;
public mypanel()
{ //xp=6;
}
public void run()
{
while(xp<720)
{
repaint();
try
{
Thread.sleep(100);
//System.out.println("正在执行");
} catch(Exception e)
{
e.printStackTrace();
}
xp+=1;
System.out.println("正在执行");
}
}
public void drawxy(Graphics g)
{
double a=0;
a=Math.cos(xp*Math.PI/180);
yp=(int)(a*50+200);
g.drawString(".", xp, yp);
}
public void paint(Graphics g)
{
//super.paint(g);
//绘制x轴和y轴
g.drawLine(360, 100, 360, 300);
g.drawLine(355, 105, 360, 100);
g.drawLine(365, 105, 360, 100);
g.drawLine(260, 200 , 460, 200);
g.drawLine(455, 195, 460, 200);
g.drawLine(455, 205, 460, 200);
g.drawString("y", 346, 105);
g.drawString("x", 460, 212);
this.drawxy(g);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询