Новичок
Профиль
Группа: Участник
Сообщений: 26
Регистрация: 9.3.2007
Репутация: нет Всего: нет
|
Уважаемый powerOn! сделал через BufferedImage, проблема осталась. Вот мой новый код: | Код | import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import java.io.*; import javax.swing.*; import javax.swing.JTextArea; import java.lang.*; import java.lang.Object; import java.lang.String; import javax.swing.JLabel; import javax.swing.Box; import java.awt.Dimension; import java.awt.geom.*; import java.awt.image.*; import java.awt.event.*; import java.awt.Window; import java.util.*; import java.lang.Math.*; import java.io.IOException; import java.util.Locale; import javax.imageio.ImageReader; import javax.imageio.spi.ImageReaderSpi; import javax.imageio.stream.ImageInputStream;
public class PseudorandomSequence extends JFrame {
private JTextField NField; private JLabel label1; private JButton nextButton; private JButton cancelButton; private JRadioButton rndButton; private JRadioButton congruousButton; private JRadioButton registerButton; private ButtonGroup radioGroup; private int mark;
public PseudorandomSequence( ) {
super( "Chesair" );
label1 = new JLabel(); label1.setText( "Insert dimension 'N', and press Next for continue." ); label1.setToolTipText( "Insert dimension 'N', and press Next for continue." );
NField = new JTextField();
nextButton = new JButton( "Next" ); nextButton.addActionListener(
new ActionListener() { public void actionPerformed( ActionEvent event ) { OperateNext(); } } );
cancelButton = new JButton( "Cancel" ); cancelButton.addActionListener(
new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); } } );
rndButton = new JRadioButton( "Random Function in JAVA", false ); congruousButton = new JRadioButton( "Congrous generator", false ); registerButton = new JRadioButton( "Register generator", false );
radioGroup = new ButtonGroup(); radioGroup.add(rndButton); radioGroup.add(congruousButton); radioGroup.add(registerButton);
RadioButtonHandler handler = new RadioButtonHandler(); rndButton.addItemListener( handler ); congruousButton.addItemListener( handler ); registerButton.addItemListener( handler );
Container container = getContentPane();
Box boxes[] = new Box[5]; boxes[0] = Box.createVerticalBox(); boxes[1] = Box.createHorizontalBox(); boxes[2] = Box.createHorizontalBox(); boxes[3] = Box.createHorizontalBox(); boxes[4] = Box.createHorizontalBox();
boxes[0].add( label1, BorderLayout.PAGE_START ); boxes[0].add( Box.createVerticalStrut(5)); boxes[0].add( NField, BorderLayout.PAGE_END );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setSize( 460, 150 ); Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; }
boxes[1].add( Box.createHorizontalStrut((screenSize.width - 770) / 2)); boxes[3].add( nextButton ); //boxes[1].add( Box.createHorizontalStrut(50)); boxes[4].add( cancelButton ); // boxes[1].add( boxes[3], BorderLayout.WEST ); boxes[1].add( Box.createHorizontalStrut(50) ); boxes[1].add( boxes[4], BorderLayout.EAST ); boxes[1].add( Box.createVerticalStrut(50));
boxes[2].add( rndButton ); boxes[2].add( Box.createHorizontalStrut(5)); boxes[2].add( congruousButton ); boxes[2].add( Box.createHorizontalStrut(5)); boxes[2].add( registerButton );
container.add( boxes[0], BorderLayout.PAGE_START ); container.add( boxes[2] ); container.add( boxes[1], BorderLayout.PAGE_END );
setLocation((screenSize.width - frameSize.width) / 2, // Center of the Screen (screenSize.height - frameSize.height) / 2);
setVisible( true ); } private class RadioButtonHandler implements ItemListener {
public void itemStateChanged( ItemEvent event ) {
if( event.getSource() == rndButton ) { mark = 1;
} else if( event.getSource() == congruousButton ) { //mark = 2;
} else if( event.getSource() == registerButton ) { mark = 3; } } } private void OperateNext() {
if(mark == 1){ Operate1(Operate0()); } else if(mark == 2){ Operate2(Operate0()); } else if(mark == 3){ Operate3(Operate0()); } } private int Operate0() {
String charN = NField.getText(); // read key to string int intN = Integer.decode(charN); // string key to integer
return intN; }
private void Operate1( int intN ) {
int max = 0; int arrayForRandomAmount[] = new int[256]; //int Rnd[] = new int[intN];
int arrayForRandom[]; arrayForRandom = new int[intN];
for( int count = 0; count < intN; count++) {
arrayForRandom[count] = (int)(Math.random()*256); arrayForRandomAmount[arrayForRandom[count]]++; }
for( int count = 0; count < 256; count++) { if(max <= arrayForRandomAmount[count]) max = arrayForRandomAmount[count]; }
PicturesGraphPanel picturesGraphPanel = new PicturesGraphPanel(arrayForRandomAmount, max, "Random Function in JAVA" );
PicturesGroundMix picturesGroundMix = new PicturesGroundMix(arrayForRandom, "Random Function in JAVA", intN);
PicturesAutocorrelationFunction picturesAutoCorrFunc = new PicturesAutocorrelationFunction( arrayForRandom, "Random Function in JAVA"); }
private void Operate2( int intN ) {
int max = 0; int arrayForCongruousAmount[] = new int[256]; int arrayForCongruous[]; arrayForCongruous = new int[intN];
arrayForCongruous[0] = 0; arrayForCongruous[0]++;
for( int count = 1; count < intN; count++ ) { arrayForCongruous[count] = CongruousGenerator( arrayForCongruous[count - 1] ); arrayForCongruousAmount[arrayForCongruous[count]]++; }
for( int count = 0; count < 256; count++) { if(max <= arrayForCongruousAmount[count]) max = arrayForCongruousAmount[count]; }
PicturesGraphPanel picturesGraphPanel = new PicturesGraphPanel(arrayForCongruousAmount, max, "Congrous generator" );
PicturesGroundMix picturesGroundMix = new PicturesGroundMix(arrayForCongruous, "Congrous generator", intN);
PicturesAutocorrelationFunction picturesAutoCorrFunc = new PicturesAutocorrelationFunction( arrayForCongruous, "Congrous generator"); }
private int CongruousGenerator( int x ) {
return ((106*x + 1283 + 6075)%6075); }
private void Operate3( int intN ) {
int max = 0; int arrayForRegisterAmount[] = new int[256]; int arrayForRegister[]; arrayForRegister = new int[intN];
/*arrayForRegister[0] = RegisterBits( 8, 99 ); arrayForRegisterAmount[arrayForRegister[0]]++; for(int count = 1; count < intN; count++){ arrayForRegister[count] = RegisterBits( 8, arrayForRegister[count - 1] ); arrayForRegisterAmount[arrayForRegister[count]]++; }
*/ arrayForRegister = RegisterBits( intN, 2147483647); for(int count = 0; count < intN; count++){ arrayForRegisterAmount[arrayForRegister[count]]++; } for(int count = 0; count < 256; count++) { if(max <= arrayForRegisterAmount[count]) max = arrayForRegisterAmount[count]; }
PicturesGraphPanel picturesGraphPanel = new PicturesGraphPanel(arrayForRegisterAmount, max, "Register generator" );
PicturesGroundMix picturesGroundMix = new PicturesGroundMix(arrayForRegister, "Register generator", intN);
PicturesAutocorrelationFunction picturesAutoCorrFunc = new PicturesAutocorrelationFunction( arrayForRegister, "Register generator"); }
private int[] RegisterBits(int amount, int register ) {
StringBuffer buffer = new StringBuffer(32); int registercopy = 0; int displayMask = 1 << ( 30 ); int bit0 = 0; int bit1 = 0; int bit2 = 0; int bit3 = 0; int bit5 = 0; int bit7 = 0; int intresult[] = new int[amount]; int i = 0;
for ( int count = 1; count <= 8*amount; count++ ){
registercopy = register; bit0 = registercopy & 1; bit1 = registercopy & 2; bit2 = registercopy & 4; bit3 = registercopy & 8; bit5 = registercopy & 32; bit7 = registercopy & 128; register = register >>> 1; register = ((bit0 << 30)^(bit1 << 29)^(bit2 << 28)^(bit3 << 27)^(bit5 << 25)^(bit7 << 23)) | register; intresult[i] <<= 1; intresult[i] = bit0 | intresult[i]; if(count % 8 == 0) { i++; } } return intresult; }
public static void main(String arqs[]) {
PseudorandomSequence application = new PseudorandomSequence();
application.setAlwaysOnTop(false); application.setVisible(true); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class AutocorrelationFunction extends JPanel { private Vector inputvector = new Vector();
private int pow( int i, int j) { for( int count = 1; count <= j; count++ ){ i = i*i; } return i; }
private int getBits( int value ) { int S = 0; int displayMask = 1 << 7;
for( int bit = 1; bit <= 8; bit++ ) {
if((value & displayMask) == 0) { S = S - pow(2, bit); }
else { S = S + pow(2, bit); }
value <<= 1; } return S; }
private int[] AutCorFunc( int inputarray[] ){
int F[] = new int[inputarray.length]; int S[] = new int[inputarray.length]; int A = 0, B = 0; for( int count = 0; count < inputarray.length; count++ ){
S[count] = getBits(inputarray[count]); }
for ( int t = 0; t < inputarray.length; t++ ) {
for (int i = 0; i < inputarray.length; i++) { A = A + S[i] * S[(i + t) % (inputarray.length)]; B = B + S[i]*S[i]; }
F[t] = A/B; }
return F; }
public void paintComponent(Graphics g) {
super.paintComponent(g); BufferedImage buffImage = new BufferedImage( 512, 512, BufferedImage.SCALE_FAST ); Graphics2D gg = buffImage.createGraphics(); Graphics2D g2d = (Graphics2D) g;
int inputarray[] = new int[inputvector.size()]; int F[] = new int[inputvector.size()];
for( int count = 0; count < inputvector.size(); count++ ){ inputarray[count] = Integer.decode("" + inputvector.elementAt(count)); } F = AutCorFunc( inputarray );
for( int count = 0; count < inputarray.length; count++ ){ gg.setPaint(new Color(0, 0, 0, 255)); gg.fill(new Rectangle2D.Double(15, 15 + 2*count, F[count], 2)); } g2d.setPaint( new Color( 255, 255, 255, 255 )); g2d.setPaint( new TexturePaint( buffImage, new Rectangle(562, 562) ) ); g2d.fill( new Rectangle(562, 562));
}
public void setInputArrayAutCorFunc( int newInputarray[] ) {
for( int count = 0; count < newInputarray.length; count++ ) {
inputvector.add(count, newInputarray[count]);
} //repaint(); }
} class PicturesAutocorrelationFunction extends JFrame {
private JLabel label; private AutocorrelationFunction autocorrelationFunction;
public PicturesAutocorrelationFunction( int array[], String supertitle ) {
super( "PicturesAutocorrelationFunction" );
label = new JLabel(); label.setText("AutoCorrelationFunction test: " + supertitle);
autocorrelationFunction = new AutocorrelationFunction(); autocorrelationFunction.setInputArrayAutCorFunc( array );
Box boxes[] = new Box[3]; boxes[0] = Box.createVerticalBox(); boxes[1] = Box.createVerticalBox(); boxes[2] = Box.createVerticalBox();
boxes[1].add(label); boxes[2].add(new javax.swing.JScrollPane(autocorrelationFunction));
boxes[0].add(boxes[1]); boxes[0].add(boxes[2]);
Container container = getContentPane(); container.add( boxes[0], BorderLayout.CENTER );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setSize( 600, 600 ); Dimension frameSize = getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
setVisible( true ); } }
class GroundMix extends JPanel {
private int N; //private int Rnd; private Vector inputvector = new Vector();
public void paintComponent( Graphics g ) {
super.paintComponent(g); Graphics2D g2d = ( Graphics2D ) g; BufferedImage buffImage = new BufferedImage( 512, 512, BufferedImage.SCALE_FAST ); Graphics2D gg = buffImage.createGraphics();
int inputarray[] = new int[inputvector.size()];
for(int count = 0; count < N; count++){
inputarray[count] = Integer.decode("" + inputvector.elementAt(count)); } for(int count = 0; count < N; count++) { //Rnd = (int)(Math.random()*256); gg.setPaint(new Color(0, 0, 0, 255)); /*g2d.fill(new Rectangle2D.Double(0 + 2*((int)(Math.random()*256)), 0 + 2*((int)(Math.random()*256)), 2, 2));*/ gg.fill(new Rectangle2D.Double(0 + 2*inputarray[count], 0 + 2*inputarray[(N+count+1)%N], 2, 2)); } g2d.setPaint( new Color( 255, 255, 255, 255 )); g2d.setPaint( new TexturePaint( buffImage, new Rectangle(512, 512) ) ); g2d.fill( new Rectangle(512, 512)); }
public void setN(int inputN) { N = inputN; }
public void setInputArrayGroundMix( int newInputarray[] ) {
for( int count = 0; count < N; count++ ) {
inputvector.add(count, newInputarray[count]);
} //repaint(); } } class PicturesGroundMix extends JFrame {
private JLabel label; private GroundMix groundmix;
public PicturesGroundMix( int array[], String supertitle, int inputN ) {
super("Ground Mix");
label = new JLabel(); label.setText("Ground Mix test: " + supertitle);
groundmix = new GroundMix(); groundmix.setN( inputN ); groundmix.setInputArrayGroundMix( array );
Box boxes[] = new Box[3]; boxes[0] = Box.createVerticalBox(); boxes[1] = Box.createVerticalBox(); boxes[2] = Box.createVerticalBox();
boxes[1].add(label); boxes[2].add(new javax.swing.JScrollPane(groundmix));
boxes[0].add(boxes[1]); boxes[0].add(boxes[2]);
Container container = getContentPane(); container.add( boxes[0], BorderLayout.CENTER );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setSize( 530, 560 ); Dimension frameSize = getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
setVisible( true ); } } class GraphPanel extends JPanel {
/* //private char CharNumbers[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; private int height[] = new int[256]; private int amount[] = new int[256]; //private String s = ""; private int a = 0; private boolean marker = false;*/ private int maximumOFRndArray = 0; private int N; private int Rnd; private int arrayForRandomAmount[] = new int[256]; private int max = 0;
public void paint( Graphics g ) {
super.paint(g); BufferedImage buffImage = new BufferedImage( 515, 600, BufferedImage.SCALE_FAST ); Graphics2D gg = buffImage.createGraphics(); Graphics2D g2d = ( Graphics2D ) g;
/*Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); double size = frameSize.width; int sizeWidth = (int)size;*/
/*for( int i = 0; i <= 255; i++ ) { marker = marker || height[i] > ( 600 )/2;
}
while ( marker == true ) {
for( int i = 0; i <= 255; i++ ) { height[i] = height[i]/2; marker = false; marker = marker || height[i] > (600)/2; }
} // End while*/
/*for( int count2 = 0; count2 <= 255; count2++ ) {
s = "" + amount[count2]; //g2d.drawChars(CharNumbers, count2, 1, 0, 55 + 15*count2); g2d.drawString(s, 10 + 15*count2, 30 + height[count2]);
}*/
for( int count = 0; count <= 255; count++ ) {
//g2d.setPaint(Color.BLUE); gg.setPaint(new Color(0,0,0,255)); //g2d.setPaint(new GradientPaint(0, 0, Color.blue, height[count], 0, Color.yellow, false )); gg.fill(new Rectangle2D.Double(15 + 3 * count, 15 + maximumOFRndArray - arrayForRandomAmount[count], 3, 0 + arrayForRandomAmount[count])); } g2d.setPaint( new TexturePaint( buffImage, new Rectangle(515, 600) ) ); g2d.fill( new Rectangle(515, 600)); } public void setN(int inputN) { N = inputN; }
public void setMax( int max ) {
maximumOFRndArray = max; //repaint(); } public void setAmountGraphPanel( int Amount[] ) {
for( int count3 = 0; count3 <= 255; count3++ ) {
arrayForRandomAmount[count3] = Amount[count3]; //a = a + amount[count3]; } } /*public void setHeightGraphPanel( int newHeightarray[] ) {
for( int count = 0; count <= 255; count++ ) {
height[count] = ( newHeightarray[count] > 0 ? newHeightarray[count] : 0 ); } repaint(); }*/ }
class PicturesGraphPanel extends JFrame {
private JLabel label; private GraphPanel graph;
public PicturesGraphPanel( int array[], int max, String supertitle ) {
super("Bar Graph");
label = new JLabel(); label.setText("Bar Graph test: " + supertitle);
graph = new GraphPanel(); //graph.setHeightGraphPanel( array ); graph.setAmountGraphPanel( array ); //graph.setN( N ); graph.setMax( max ); graph.createImage(515, 600);
Box boxes[] = new Box[3]; boxes[0] = Box.createVerticalBox(); boxes[1] = Box.createVerticalBox(); boxes[2] = Box.createVerticalBox();
boxes[1].add(label); boxes[2].add(new javax.swing.JScrollPane(graph));
boxes[0].add(boxes[1]); boxes[0].add(boxes[2]);
Container container = getContentPane(); container.add( boxes[0], BorderLayout.CENTER );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setSize( 815, 600 ); Dimension frameSize = getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } setLocation( (screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
setVisible( true ); //setAlwaysOnTop( true ); } }
|
Изображение с JFrame'a всё равно исчезает, если его свернуть и развернуть, или его часть "закрыть" другим окном. Не могу разобраться в чем скрыта проблема. Можно ли избавиться от исчезания изображения? Заранее спасибо.
|