求助关于画图和图像控制

05-05-24 cugeryang
通过一个按钮事件,向一个面板中添加一个已经在画布上画好的图形并且能够进行控制比如移动删除的实现问题。

面板类:

import java.awt.*;

import java.awt.geom.Line2D;

import java.util.NoSuchElementException;

import java.util.Vector;

import javax.swing.*;

public class CircuitView extends JPanel

implements Observer

{

public CircuitView()

{

grid = new Color(127, 255, 254);

background = new Color(0, 0, 192);

gridx = 15;

gridy = 15;

setBackground(background);

}

public Image createBuffer()

{

Image bufferImage = createImage(getSize().width, getSize().height);

Graphics bufferGraphics = bufferImage.getGraphics();

bufferSize = getSize();

return bufferImage;

}

public void paint(Graphics g)

{

createGrid();

g.drawImage(gridBufferImage, 0, 0, this);

//revalidate();

//g.drawImage(componentBufferImage, 0, 0, this);

//model.getControl().paint(g);

}

public void update(Graphics g)

{

paint(g);

}

private void createGrid()

{

Dimension r = getSize();

if(!r.equals(bufferSize))

{

gridBufferImage = createBuffer();

Graphics2D bufferGraphics = (Graphics2D)gridBufferImage.getGraphics();

bufferGraphics.setColor(background);

bufferGraphics.fillRect(0, 0, getWidth(), getHeight());

int x0 = 0;

int x1 = (int)r.getWidth();

int y0 = 0;

int y1 = (int)r.getHeight();

bufferGraphics.setPaint(grid);

float dashPattern[] = {

2.0F, 2.0F

};

bufferGraphics.setStroke(new BasicStroke(1.0F, 0, 0, 10F, dashPattern, 0.0F));

for(int x = x0; x < x1; x += gridx)

{

java.awt.geom.Line2D.Double l = new java.awt.geom.Line2D.Double(x, y0, x, y1);

bufferGraphics.draw(l);

}

for(int y = y0; y < y1; y += gridy)

{

java.awt.geom.Line2D.Double l = new java.awt.geom.Line2D.Double(x0, y, x1, y);

bufferGraphics.draw(l);

}

bufferGraphics.dispose();

}

}

public void update()

{

repaint();

}

private CircuitModel model;

public Color grid;

public Color background;

public int gridx;

public int gridy;

Image gridBufferImage;

Dimension bufferSize;

}

画布类:

import javax.swing.*;

import java.awt.*;

import java.awt.geom.Line2D;

public class AndPort extends Canvas{

public AndPort(){

setSize(60,60);

createPort();

}

public Image createBuffer()

{

Image bufferImage = createImage(60,60);

Graphics bufferGraphics = bufferImage.getGraphics();

return bufferImage;

}

public void createPort(){

AndBufferImage = createBuffer();

Graphics2D bufferGraphics = (Graphics2D)AndBufferImage.getGraphics();

bufferGraphics.setPaint(Color.BLACK);

java.awt.geom.Line2D.Double a = new java.awt.geom.Line2D.Double(0,20, 15, 20);

bufferGraphics.draw(a);

java.awt.geom.Line2D.Double b = new java.awt.geom.Line2D.Double(0,40, 15, 40);

bufferGraphics.draw(b);

java.awt.geom.Line2D.Double c = new java.awt.geom.Line2D.Double(45,30, 60, 30);

bufferGraphics.draw(c);

java.awt.geom.Rectangle2D.Double d = new java.awt.geom.Rectangle2D.Double(15,5, 30, 50);

bufferGraphics.draw(d);

String s="And";

bufferGraphics.drawString(s,20,35);

bufferGraphics.dispose();

}

public void paint(Graphics g){

Graphics2D g2=(Graphics2D)g;

createPort();

g2.drawImage(AndBufferImage,0,0,this);

}

public Image getimage(){

return AndBufferImage;

}

public Image AndBufferImage;

}

怎么样才能把我在画布上画这个图形通过一个按钮画到我做的那个面板上,并且可以多次添加画上去并且控制移动什么的。请各位高手救命啊。感激不尽。

猜你喜欢