Drawing a sinusoidal curve:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.lang.*;
public class sinx {
public static void main(String[] args) {
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DrawFrame extends JFrame {
public DrawFrame() {
//Set title and window size
setTitle("sinx");
setSize(WIDTH, HEIGHT);
DrawPanel panel = new DrawPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
}
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
int x,y;
double a;
//Draw a sine curve
//Graphics g=getGraphics();
for(x=0;x{
a=Math.sin(x*Math.PI/180);
y=(int)(80 40*a);
g2.drawString("*",x,y);
}
}
}
It’s relatively simple to write.
package OnlineUserCount;
import java.awt.*;
import javax.swing.*;
public class Sin extends JPanel{
private double x;
private double y;
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
g.setColor(Color.WHITE); //Set the panel background color
g.fillRect(0, 0, 400, 300); //Fill the panel
g.setColor(Color.RED); //Set the color of the line drawing
for(x=0;x
{
y=Math.sin(x*Math. PI/180); //Convert to radians, 1 degree = π/180 radians
y=(100 80*y);//Easy to display on the screen
//g.drawString(".",(int)x,(int)y);//You can also use this method
g.drawLine((int)x, (int)y, (int)x,(int) y); //Draw points
}
}
public static void main(String []args){
Sin s= new Sin();
JFrame j=new JFrame();
j.setTitle("A periodic sinusoidal curve");
j.add(s);
j.setSize(400, 300);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
}
//Screenshot of effect
The above is the detailed content of Design and implement an application using Java to draw an image of the following function:. For more information, please follow other related articles on the PHP Chinese website!