Button btn =new Button()
btn.Content ="电压表"
pcbCanvas.AddVisual(btn)
PCBCanvas.SetLeft(btn, 100)
PCBCanvas.SetTop(btn, 100)
import javax.swing.*import javax.swing.border.*
import java.awt.*
import java.awt.event.*
import java.util.*
/**
* @author Hardneedl
*/
final public class PaintDemo extends JApplet {
private final static Dimension PREFERRED_SIZE = new Dimension(800, 600)
public Dimension getPreferredSize() {return PREFERRED_SIZE}
public Dimension getMinimumSize() {return PREFERRED_SIZE}
private Brush brushprivate BrushCanvas canvas
private JCheckBox ckGreen,ckRed,ckBlue
private ButtonGroup rbGroup
private JRadioButton rbSmall,rbMidel,rbBig
public void init() {
canvas=new BrushCanvas(brush = new StringBrush(){
public Component getOwner() {
return canvas
}
}
){
public Dimension getPreferredSize() {
return new Dimension(400, 450)
}
}
ckGreen=new JCheckBox(new ColorValueAction("green", Color.GREEN,brush))
ckRed=new JCheckBox(new ColorValueAction("red", Color.RED,brush))
ckBlue=new JCheckBox(new ColorValueAction("blue", Color.BLUE,brush))
rbGroup=new ButtonGroup()
rbGroup.add(rbSmall=new JRadioButton(new IntegerValueAction("10",10,brush)))
rbGroup.add(rbMidel=new JRadioButton(new IntegerValueAction("16",16,brush)))
rbGroup.add(rbBig=new JRadioButton(new IntegerValueAction("50", 50,brush)))
Container container = getContentPane()
BoxLayout boxLayout=new BoxLayout(container,BoxLayout.Y_AXIS)
container.setLayout(boxLayout)
JComponent p = new JPanel()
p.add(ckGreen)p.add(ckRed)p.add(ckBlue)
container.add(p)
JComponent p0 = new JPanel()
p0.add(rbSmall)p0.add(rbMidel)p0.add(rbBig)
container.add(p0)
container.add(canvas)
}
public void start() {
rbGroup.setSelected(rbBig.getModel(),true)
}
}
/*整数值的动作,getValue("integer")返回代表的整数值*/
class IntegerValueAction extends AbstractAction{
private int v
private Observable observable=new Observable(){
public void notifyObservers(Object arg) {
setChanged()super.notifyObservers(arg)
}
}
public IntegerValueAction(String t, int v,Observer observer) {
super(t)
this.v=v
observable.addObserver(observer)
}
public void actionPerformed(ActionEvent e) {
observable.notifyObservers(getValue("integer"))
}
public Object getValue(String key) {
if (key.equalsIgnoreCase("integer")){
return v
}
return super.getValue(key)
}
}
class ColorValueAction extends AbstractAction{
private Color v=new Color(0)
private Observable observable=new Observable(){
public void notifyObservers(Object arg) {
setChanged()super.notifyObservers(arg)
}
}
public ColorValueAction(String t, Color v, Observer observer) {
super(t)
this.v=v
observable.addObserver(observer)
}
public void actionPerformed(ActionEvent e) {
observable.notifyObservers(getValue("color"))
}
public Object getValue(String key) {
if (key.equalsIgnoreCase("color")){
return v
}
return super.getValue(key)
}
}
/*画笔*/
interface Brush extends Observer{
/**
* 绘图
* @param g 绘图环境
*/
void paint(Graphics g)
Component getOwner()
}
abstract class StringBrush implements Brush{
private int fontSize = 14
private Color color
String getString(){
return "请选择"
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create()
g2.setColor(color==null?Color.BLACK:color)
g2.setFont(g2.getFont().deriveFont((float)fontSize))
g2.translate(40,50)
g2.drawString(getString()==null?"":getString(),0,0)
g2.dispose()
}
public void update(Observable o, Object arg) {
if (arg instanceof Integer) {
fontSize = (int)arg
}
if (arg instanceof Color) {
Color c = (Color)arg
color=new Color(c.getRed(),c.getGreen(),c.getBlue())
}
Component owner = getOwner()
if (owner!=null)owner.repaint()
}
}
/*画布*/
class BrushCanvas extends JComponent{
private Brush brush
public BrushCanvas(Brush brush) {
setBrush(brush)
}
void setBrush(Brush b){
brush=b
}
protected void paintComponent(Graphics g) {
super.paintComponent(g)
if (brush!=null)
brush.paint(g)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)