- 论坛徽章:
- 0
|
一个frame中,放置了一个jcomponment layeredviewer, layeredviewer中有一个list,点击button就向其zhong
添加一个componment shapecontainer, 希望每个shapecontainer,都是一个挨着一个的显示,中间没有空隙
但是现在的效果是,每次添加新的shapecontianer的时候总是需要拉动一下才能够正常显示,
有人告诉我需要重载paintcomponment,但是不知道里面如何去做?
显示代码如下,急求高人指点,最好能够给出详细的代码,万分谢谢!!!!!
/*
* Created on 2005-8-10
* @author ruby
* Changes:
*/
package ui;
import graph.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.border.Border;
import data.LasData;
import data.LasParse;
/*
*
* 2005-8-10
* @author ruby
*/
public class LayeredView extends GraphView{// implements Runnable{
private int defaultShapeContainerWidth = 400;
private int defaultShapeContainerHeight= 500;
public LasData lasData = null;
public ArrayList scAl = new ArrayList(0) ; // use to hold ShapeContainer
public int scIndex = -1; //the selected ShapeContianer index in scAl
public String curveType;
public String bgType;
public JPanel layeredPanel = new JPanel();
public int containterHGap = 25;
public int containterVGap = 25;
public GraphEvent graphEvent;
// private GridBagLayout layout;
private LayoutManager layout;
GridBagConstraints gbdc;
public LayeredView()
{
this.layout = new BoxLayout(this, BoxLayout.X_AXIS);
/* this.gbdc=new GridBagConstraints();
this.gbdc.fill=GridBagConstraints.NONE;
this.gbdc.weightx=1; */
this.setLayout(this.layout);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void paintChildren(Graphics g)
{
super.paintChildren(g);
}
public void paint(Graphics g)
{
super.paint(g);
}
public void render(Graphics g)
{
int w = this.getWidth();
int i=0;
Iterator it = this.scAl.iterator();
for (; it.hasNext(); )
{
ShapeContainer sc = (ShapeContainer)it.next();
sc.setPreferredSize(new Dimension((int)sc.getContainerWidth(), (int)sc.getContainerHeight()));
this.add(sc);
sc.paint(g);
if (i == 0)
{
sc.setFirst(true);
}
i++;
}
}
/**
*
* @author ruby
* 2005-8-23
* @param
* @return a new shapecontainer
*/
public ShapeContainer getNewContainter() {
ShapeContainer sc = new ShapeContainer();
sc.setDefault(true);
sc.setLasData(this.lasData);
Color[] c = { Color.BLACK, Color.RED, Color.BLUE, Color.PINK,
Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA,
Color.YELLOW, Color.BLACK, Color.RED, Color.BLUE, Color.PINK,
Color.CYAN, Color.DARK_GRAY, Color.GREEN, Color.MAGENTA,
Color.YELLOW };
Color[] c1 = { Color.PINK, Color.YELLOW, Color.WHITE, Color.GRAY,
Color.PINK, Color.YELLOW, Color.WHITE, Color.GRAY, Color.PINK,
Color.YELLOW, Color.WHITE, Color.GRAY, Color.PINK,
Color.YELLOW, Color.WHITE, Color.GRAY };
ArrayList list = new ArrayList();
ArrayList list1 = new ArrayList();
for (int i = 0; i < c.length; i++) {
list.add(c);
}
for (int j = 0; j < c1.length; j++) {
list1.add(c1[j]);
}
RenderingAttribute ra = new RenderingAttribute();
// if (this.scAl.size()==0)
// {
ra.setHeight(this.defaultShapeContainerHeight);
ra.setWidth(this.defaultShapeContainerWidth);
// }
// else
// {
// ShapeContainer scinList = (ShapeContainer)(this.scAl.get(0));
// scinList.getContainerHeight();
// }
ra.getLineAttribute().setColorArrayList(list);
ra.setFillColorList(list1);
sc.setRenderingAttribute(ra);
sc.setPattern("lithologies", "grid" ;
return sc;
}
}
下面是点击button的事件处理:
if (arg0.getSource() == frame.newContainer) {
ShapeContainer sc = lv.getNewContainter();
lv.scAl.add(sc);
lv.render(lv.getGraphics());
lv.updateUI();
lv.repaint();
lv.show();
} |
|