Witam, mam za zadanie napisać edytor graficzny, którym można rysować linie, prostokąty itd. Poradziłem sobie ze zmianą figury do rysowania, lecz po każdym narysowaniu obiektu poprzedni znika. Próbowałem wstawiać współrzędne każdego z nich do tablice i potem pętlą je kolejno rysować ale nie wychodzi... proszę o analizę kodu i o pomoc :D
/*
* NewJFrame.java
*
* Created on 19 listopad 2013, 11:51
*/
package javaapplication23;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author student
*/
public class NewJFrame extends javax.swing.JFrame {
public int x,y,n;
public int x2, y2;
int funkcja=0;
int tab[]= new int[10];
int tab2[]= new int[10];
int tab3[]= new int[10];
int tab4[]= new int[10];
private Object JLabel2;
public class rysuj extends JPanel
{
private boolean jToggleButton1ActionPerformed;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i=0;i<10;i++)
{
tab[i]=x;
tab2[i]=y;
tab3[i]=x2;
tab4[i]=y2;
}
for(int i=0;i<10;i++){
g.setColor(Color.black);
if(funkcja==1)
g.drawLine(tab[i],tab2[i],tab3[i],tab4[i]);
else
if(funkcja==2)
g.drawRect(tab[i],tab2[i],tab3[i],tab4[i]);
}
}
}
public NewJFrame() {
initComponents();
rysuj q= new rysuj();
this.add(q);
q.setSize(10000,10000);
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel1.setText("jLabel1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
formMouseReleased(evt);
}
});
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("jButton4");
jLabel2.setText("jLabel2");
jLabel3.setText("jLabel3");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(258, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(40, 40, 40)
.addComponent(jLabel3)
.addGap(34, 34, 34))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 242, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(21, 21, 21))
);
pack();
}// </editor-fold>
private void formMousePressed(java.awt.event.MouseEvent evt) {
x=evt.getX();
y=evt.getY();
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
x2=evt.getX();
y2=evt.getY();
this.repaint();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for(int i=0;i<1;i++){
System.out.println(tab[i]);
System.out.println(tab2[i]);
System.out.println(tab3[i]);
System.out.println(tab4[i]);}
}
private void formMouseMoved(java.awt.event.MouseEvent evt) {
jLabel2.setText("x:"+String.valueOf(evt.getX()));
jLabel3.setText("y:"+String.valueOf(evt.getY()));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
funkcja= 1;
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
funkcja=2;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
dodanie znacznika <code class="java"> - fp