以下代码是sin曲线图,用了draw函数: In the following example, all the intelligence concerning painting is in the SineDraw class; the SineWave class simply configures the program and the slider control. Inside SineDraw, the setCycles( ) method provides a hook to allow another object—the slider control, in this case—to control the number of cycles. //: c13:SineWave.java // Drawing with Swing, using a JSlider. // <applet code=SineWave // width=700 height=400></applet> import javax.swing.*; import javax.swing.event.*; import java.awt.*; import com.bruceeckel.swing.*;class SineDraw extends JPanel { static final int SCALEFACTOR = 200; int cycles; int points; double[] sines; int[] pts; SineDraw() { setCycles(5); } public void setCycles(int newCycles) { cycles = newCycles; points = SCALEFACTOR * cycles * 2; sines = new double[points]; pts = new int[points]; for(int i = 0; i < points; i++) { double radians = (Math.PI/SCALEFACTOR) * i; sines[i] = Math.sin(radians); } repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); int maxWidth = getWidth(); double hstep = (double)maxWidth/(double)points; int maxHeight = getHeight(); for(int i = 0; i < points; i++) pts[i] = (int)(sines[i] * maxHeight/2 * .95 + maxHeight/2); g.setColor(Color.red); for(int i = 1; i < points; i++) { int x1 = (int)((i - 1) * hstep); int x2 = (int)(i * hstep); int y1 = pts[i-1]; int y2 = pts[i]; g.drawLine(x1, y1, x2, y2); } } }public class SineWave extends JApplet { SineDraw sines = new SineDraw(); JSlider cycles = new JSlider(1, 30, 5); public void init() { Container cp = getContentPane(); cp.add(sines); cycles.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) { sines.setCycles( ((JSlider)e.getSource()).getValue()); } }); cp.add(BorderLayout.SOUTH, cycles); } public static void main(String[] args) { Console.run(new SineWave(), 700, 400); } } ///:~ 注意,import com.bruceeckel.swing.*; 是针对: //: com:bruceeckel:swing:Console.java // Tool for running Swing demos from the // console, both applets and JFrames. package com.bruceeckel.swing; import javax.swing.*; import java.awt.event.*;public class Console { // Create a title string from the class name: public static String title(Object o) { String t = o.getClass().toString(); // Remove the word "class": if(t.indexOf("class") != -1) t = t.substring(6); return t; } public static void setupClosing(JFrame frame) { // The JDK 1.2 Solution as an // anonymous inner class: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // The improved solution in JDK 1.3: // frame.setDefaultCloseOperation( // EXIT_ON_CLOSE); } public static void run(JFrame frame, int width, int height) { setupClosing(frame); frame.setSize(width, height); frame.setVisible(true); } public static void run(JApplet applet, int width, int height) { JFrame frame = new JFrame(title(applet)); setupClosing(frame); frame.getContentPane().add(applet); frame.setSize(width, height); applet.init(); applet.start(); frame.setVisible(true); } public static void run(JPanel panel, int width, int height) { JFrame frame = new JFrame(title(panel)); setupClosing(frame); frame.getContentPane().add(panel); frame.setSize(width, height); frame.setVisible(true); } } ///:~ 来自:thinking in java
SmoothedCurveApplet.java/////////////////////////////////////////////
//本程序说明:请用把本文件夹加入PWS或IIS后,用IE打开SmoothedCurve.asp
/////////////////////////////////////////////
import java.awt.*;
import javax.swing.*;class SmoothedCurvePanel extends JPanel
{
private int x1=22,y1=10; //坐标点1的x,y
private int x2=790,y2=580; //坐标点2的x,y
private int iCount=6; //采样个数
private int[][] iInpoint; //坐标x,y的存储数组
public SmoothedCurvePanel()
{
iInpoint=new int[iCount][2];
iInpoint[0][0]=20; iInpoint[0][1]=20;
iInpoint[1][0]=40; iInpoint[1][1]=40;
iInpoint[2][0]=80; iInpoint[2][1]=80;
iInpoint[3][0]=200; iInpoint[3][1]=200;
iInpoint[4][0]=250; iInpoint[4][1]=150;
iInpoint[5][0]=300; iInpoint[5][1]=80;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawCircle(g);
}
//画光滑曲线
public void drawCircle(Graphics g)
{
for(int i=0;i<iCount;i++)
if(iCount!=0 && i!=iCount-1)
//等x或y
if((iInpoint[i+1][0]-iInpoint[i][0])==0 || (iInpoint[i+1][1]-iInpoint[i][1])==0)
{
g.drawLine(x1+iInpoint[i][0],y2-iInpoint[i][1],x1+iInpoint[i+1][0],y2-iInpoint[i+1][1]);//画直线
}
else
//前升
if((iInpoint[i+1][0]-iInpoint[i][0])>0 && (iInpoint[i+1][1]-iInpoint[i][1])>0)
{
g.drawArc(x1+iInpoint[i][0]-(iInpoint[i+1][0]-iInpoint[i][0])/2,y2-iInpoint[i+1][1],iInpoint[i+1][0]-iInpoint[i][0],iInpoint[i+1][1]-iInpoint[i][1],270,90);
g.drawArc((x1+iInpoint[i][0])+(iInpoint[i+1][0]-iInpoint[i][0])/2,y2-iInpoint[i+1][1],iInpoint[i+1][0]-iInpoint[i][0],iInpoint[i+1][1]-iInpoint[i][1],90,90);
}
else
//前降
if((iInpoint[i+1][0]-iInpoint[i][0])>0 && (iInpoint[i+1][1]-iInpoint[i][1])<0)
{
g.drawArc((x1+iInpoint[i][0])-(iInpoint[i+1][0]-iInpoint[i][0])/2,(y2-iInpoint[i][1]),iInpoint[i+1][0]-iInpoint[i][0],-(iInpoint[i+1][1]-iInpoint[i][1]),0,90);
g.drawArc((x1+iInpoint[i][0])+(iInpoint[i+1][0]-iInpoint[i][0])/2,(y2-iInpoint[i][1]),iInpoint[i+1][0]-iInpoint[i][0],-(iInpoint[i+1][1]-iInpoint[i][1]),180,90);
}
else
//后升
if((iInpoint[i+1][0]-iInpoint[i][0])<0 && (iInpoint[i+1][1]-iInpoint[i][1])>0)
{
g.drawArc((x1+iInpoint[i+1][0])-(-(iInpoint[i+1][0]-iInpoint[i][0])/2),y2-iInpoint[i+1][1],-(iInpoint[i+1][0]-iInpoint[i][0]),iInpoint[i+1][1]-iInpoint[i][1],0,90);
g.drawArc((x1+iInpoint[i+1][0])-(iInpoint[i+1][0]-iInpoint[i][0])/2,y2-iInpoint[i+1][1],-(iInpoint[i+1][0]-iInpoint[i][0]),iInpoint[i+1][1]-iInpoint[i][1],180,90);
}
else
//后降
if((iInpoint[i+1][0]-iInpoint[i][0])<0 && (iInpoint[i+1][1]-iInpoint[i][1])<0)
{
g.drawArc(x1+iInpoint[i+1][0],(y2-iInpoint[i][1])+(iInpoint[i+1][1]-iInpoint[i][1])/2,-(iInpoint[i+1][0]-iInpoint[i][0]),-(iInpoint[i+1][1]-iInpoint[i][1]),270,90);
g.drawArc(x1+iInpoint[i+1][0],(y2-iInpoint[i][1])-(iInpoint[i+1][1]-iInpoint[i][1])/2,-(iInpoint[i+1][0]-iInpoint[i][0]),-(iInpoint[i+1][1]-iInpoint[i][1]),90,90);
}
}
}public class SmoothedCurveApplet extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SmoothedCurvePanel());
}
}
SmoothedCurve.html<html>
<body>
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = 800 HEIGHT = 601>
<PARAM NAME = CODE VALUE = "SmoothedCurveApplet" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="false">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.3" CODE = "SmoothedCurveApplet" WIDTH = 800 HEIGHT = 801 scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED>
</NOEMBED>
</EMBED>
</COMMENT>
</OBJECT>
</body>
</html>
In the following example, all the intelligence concerning painting is in the SineDraw class; the SineWave class simply configures the program and the slider control. Inside SineDraw, the setCycles( ) method provides a hook to allow another object—the slider control, in this case—to control the number of cycles.
//: c13:SineWave.java
// Drawing with Swing, using a JSlider.
// <applet code=SineWave
// width=700 height=400></applet>
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import com.bruceeckel.swing.*;class SineDraw extends JPanel {
static final int SCALEFACTOR = 200;
int cycles;
int points;
double[] sines;
int[] pts;
SineDraw() { setCycles(5); }
public void setCycles(int newCycles) {
cycles = newCycles;
points = SCALEFACTOR * cycles * 2;
sines = new double[points];
pts = new int[points];
for(int i = 0; i < points; i++) {
double radians = (Math.PI/SCALEFACTOR) * i;
sines[i] = Math.sin(radians);
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int maxWidth = getWidth();
double hstep = (double)maxWidth/(double)points;
int maxHeight = getHeight();
for(int i = 0; i < points; i++)
pts[i] = (int)(sines[i] * maxHeight/2 * .95
+ maxHeight/2);
g.setColor(Color.red);
for(int i = 1; i < points; i++) {
int x1 = (int)((i - 1) * hstep);
int x2 = (int)(i * hstep);
int y1 = pts[i-1];
int y2 = pts[i];
g.drawLine(x1, y1, x2, y2);
}
}
}public class SineWave extends JApplet {
SineDraw sines = new SineDraw();
JSlider cycles = new JSlider(1, 30, 5);
public void init() {
Container cp = getContentPane();
cp.add(sines);
cycles.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
sines.setCycles(
((JSlider)e.getSource()).getValue());
}
});
cp.add(BorderLayout.SOUTH, cycles);
}
public static void main(String[] args) {
Console.run(new SineWave(), 700, 400);
}
} ///:~
注意,import com.bruceeckel.swing.*;
是针对:
//: com:bruceeckel:swing:Console.java
// Tool for running Swing demos from the
// console, both applets and JFrames.
package com.bruceeckel.swing;
import javax.swing.*;
import java.awt.event.*;public class Console {
// Create a title string from the class name:
public static String title(Object o) {
String t = o.getClass().toString();
// Remove the word "class":
if(t.indexOf("class") != -1)
t = t.substring(6);
return t;
}
public static void setupClosing(JFrame frame) {
// The JDK 1.2 Solution as an
// anonymous inner class:
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// The improved solution in JDK 1.3:
// frame.setDefaultCloseOperation(
// EXIT_ON_CLOSE);
}
public static void
run(JFrame frame, int width, int height) {
setupClosing(frame);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void
run(JApplet applet, int width, int height) {
JFrame frame = new JFrame(title(applet));
setupClosing(frame);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
setupClosing(frame);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible(true);
}
} ///:~
来自:thinking in java