RGB颜色表示
RGB,是一种颜色空间 ColorSpace
指定红、绿、蓝三种颜色的值,即可构造各种颜色
例如:
黑色:(0, 0, 0)
白色:(255, 255, 255)
红色:(255, 0, 0)
在 Swing 中,使用 Color 类来代表颜色
例如:
Color color = Color.RED;
Color color = new Color(255, 0, 0);
Color color = new Color(0xFF0000);
RGBA颜色表示
最后一个 A 代表透明度,也是 0 - 255 之间
0:全透明
255:完全不透明
绘制几何图形
绘制时,使用 Graphics 和 Graphics2D 下的方法
- Line 直线
- Rect 矩形(包含正方形)
- Oval 椭圆(包含圆)
- Polygon 多边形(含三角形)
- Arc 圆弧 / 扇形
绘制方法分为两种:
- drawXXX():只画线条
- fillXXX():表示只填充
绘制一条正弦曲线
实现方法:
用若干极短的线段,连接起来,模拟一条曲线(粒度越小,看起来越平滑)
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.TreeMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Demo {
public static void main(String[] args) {
MyFrame frame = new MyFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyFrame extends JFrame{
public MyFrame(String title){
super(title);
this.setContentPane(new MyPanel());
}
private class MyPanel extends JPanel{
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int width = this.getWidth();
int height = this.getHeight();
int grain = 1;
int period = 100;
int range = 100;
int x1 = 0;
int y1 = 0;
g2.setColor(Color.RED);
g2.drawLine(0, height / 2, width, height / 2);
for(int i = 0; i < width; i += grain) {
int x = i;
//先乘上振幅在取整,否咋大部分值为0
int y = (int)(Math.sin(2 * Math.PI * x / period) * range);
g2.setColor(Color.BLUE);
g2.drawLine(x1, -y1 + height / 2, x, -y + height / 2);
x1 = x;
y1 = y;
}
}
}
}
显示:
正弦曲线控制
练习:添加几个控制 Spinner,来控制曲线的显示
三个参数:grain、range、period
Spinner:可以微调的转盘控件
创建:参数 initialValue、min、max、step
new JSpinner(new SpinnerNumberModel(1, 1, 5, 1));
设值:setValue();
取值:getValue();
事件:addChangeListener();
代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Demo {
public static void main(String[] args) {
MyFrame frame = new MyFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MyFrame extends JFrame{
MyPanel panel = new MyPanel();
JSpinner spinner1;
JSpinner spinner2;
JSpinner spinner3;
public MyFrame(String title) {
super(title);
Container contentPane = this.getContentPane();
Box box = Box.createHorizontalBox();
box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
spinner1 = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
spinner2 = new JSpinner(new SpinnerNumberModel(100, 50, 300, 10));
spinner3 = new JSpinner(new SpinnerNumberModel(100, 50, 300, 10));
spinner1.addChangeListener(new changeListener());
spinner2.addChangeListener(new changeListener());
spinner3.addChangeListener(new changeListener());
box.add(new JLabel("粒度"));
box.add(spinner1);
box.add(new JLabel("周期"));
box.add(spinner2);
box.add(new JLabel("幅度"));
box.add(spinner3);
contentPane.add(box, BorderLayout.NORTH);
contentPane.add(panel, BorderLayout.CENTER);
panelChange();
}
private class MyPanel extends JPanel{
int grain;
int period;
int range;
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int width = this.getWidth();
int height = this.getHeight();
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
int x1 = 0;
int y1 = 0;
g2.setColor(Color.RED);
g2.drawLine(0, height / 2, width, height / 2);
for(int i = 0; i < width; i += grain) {
int x = i;
//先乘上振幅在取整,否咋大部分值为0
int y = (int)(Math.sin(2 * Math.PI * x / period) * range);
g2.setColor(Color.BLUE);
g2.drawLine(x1, -y1 + height / 2, x, -y + height / 2);
x1 = x;
y1 = y;
}
for(int i = 0; i < width / 100; i++) {
g2.setColor(Color.RED);
g2.drawLine(i * 100, height / 2, i * 100, height / 2 - 10);
}
}
}
private class changeListener implements ChangeListener{
@Override
public void stateChanged(ChangeEvent e) {
panelChange();
}
}
public void panelChange() {
panel.grain = (int)spinner1.getValue();
panel.period = (int)spinner2.getValue();
panel.range = (int)spinner3.getValue();
panel.repaint();
}
}
显示: