Poniżej umieszczam kod który rysuje elementy schematu, natomiast nie wiem dlaczego nie działa po pierwszym kliknieciu w Jbuttona tylko znika, a po drugim kliknieciu się pojawia ?
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import rysowanie.*;
/*
* Okno.java
*
* Created on 2011-03-12, 21:02:06
*/
/**
*
* @author darek
*/
public class Okno extends javax.swing.JFrame {
/** Creates new form Okno */
public Okno() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 486, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 672, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(139, 139, 139)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 269, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(297, 297, 297)
.addComponent(jButton1)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Schemat rysuj = new Schemat(jPanel1.getWidth(), jPanel1.getHeight(), jPanel1.getGraphics());
rysuj.NarysujStart();
rysuj.NarysujInstrukcje("instrukcja");
rysuj.NarysujWarunek("warunek");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Okno().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
poniżej klasa rysująca
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
/**
*
* @author darek
*/
public class Schemat {
Graphics g;
int wysokosc, szerokosc;
int pozX = 0;
int pozY = 0;
int centerx = 40;
int wspystart = 40;
int wspxstart;
int szerokoscinstrukcji = 80;
int wysokoscinstrukcji = 40;
int szerokoscstartu = 80;
int wysokoscstartu = 40;
int pozycjanapisuX;
int pozycjanapisuY;
int szerokoscwarunku = 80;
int wysokoscwarunku = 80;
int wsppowrotuiteracjiX;
int wsppowrotuiteracjiY;
int wsptrueX;
int wspfalseY;
int stalastrzalki = 40;
public Schemat(int wys, int szer, Graphics g){
wysokosc = wys;
szerokosc = szer;
wspxstart = ((szerokosc/5)* 2); // pozycja startu dla X
wspystart = 40;
this.g = g;
}
public void NarysujStart(){
g.translate(wspxstart, wspystart);
g.setColor( Color.blue);
g.drawOval(pozY, pozX, szerokoscstartu, wysokoscstartu);
pozycjanapisuX = pozX + 30;
pozycjanapisuY = pozY + 30;
g.drawString("Start", pozycjanapisuY ,pozycjanapisuX );
pozY = pozY + wysokoscstartu;
g.drawLine(centerx, pozY, centerx, pozY + stalastrzalki);
pozY = pozY + stalastrzalki;
int tabX [ ] = {centerx,centerx-3, centerx+3 ,centerx };
int tabY [ ] = { pozY, pozY-5, pozY-5 , pozY };
int n = tabX.length;
g.fillPolygon( tabX, tabY, n-1 );
}
public void NarysujWarunek(String warunek){
int tabX [ ] = {pozX, pozX + szerokoscwarunku/2, pozX + szerokoscwarunku ,pozX + szerokoscwarunku/2 , pozX }; // zamiast 0 pozX gdy bez translate
int tabY [ ] = {pozY + wysokoscwarunku/2 , pozY,pozY + wysokoscwarunku/2 ,pozY + wysokoscwarunku, pozY + wysokoscwarunku/2 };
int n = tabX.length;
this.g.drawPolygon( tabX, tabY, n-1 );
this.wsppowrotuiteracjiX = pozX + szerokoscwarunku;
this.wsppowrotuiteracjiY = pozY + wysokoscwarunku/2;
this.wsptrueX = pozX;
this.wspfalseY = pozY + wysokoscwarunku/2;
g.drawString(warunek, pozX + 5, pozY + szerokoscwarunku/2);
pozY = pozY + wysokoscwarunku;
//strzałka
g.drawLine(centerx, pozY, centerx, pozY + stalastrzalki);
pozY = pozY + stalastrzalki;
// grot
int tabXstrzalka [ ] = {centerx,centerx-3, centerx+3 ,centerx };
int tabYstrzalka [ ] = { pozY, pozY-5, pozY-5 , pozY };
n = tabXstrzalka.length;
g.fillPolygon( tabXstrzalka, tabYstrzalka, n-1 );
}
public void NarysujInstrukcje(String instrukcja){
g.drawRect(pozX, pozY, szerokoscinstrukcji, wysokoscinstrukcji );
g.drawString(instrukcja, pozX + 5 , pozY + 20);
pozY = pozY + wysokoscinstrukcji;
//strzałka
g.drawLine(centerx, pozY, centerx, pozY + stalastrzalki);
pozY = pozY + stalastrzalki;
// grot
int tabX [ ] = {centerx,centerx-3, centerx+3 ,centerx };
int tabY [ ] = { pozY, pozY-5, pozY-5 , pozY };
int n = tabX.length;
g.fillPolygon( tabX, tabY, n-1 );
}
}
Aby mi się przydał musi rysować po pierwszym kliknięciu w JButona ...