Code:
/
package javaapplication16;
import java.awt.Graphics.*;
import java.awt.*;
public class Test extends javax.swing.JFrame {
public Test() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
slika = new javax.swing.JLabel();
zeleni = new javax.swing.JButton();
crveni = new javax.swing.JButton();
crtaj = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
slika.setText(" ");
zeleni.setText("zeleni");
zeleni.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zeleniActionPerformed(evt);
}
});
crveni.setText("crveni");
crveni.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
crveniActionPerformed(evt);
}
});
crtaj.setText("crtaj");
crtaj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
crtajActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(zeleni)
.addGap(18, 18, 18)
.addComponent(crveni)
.addGap(18, 18, 18)
.addComponent(crtaj)
.addContainerGap(155, Short.MAX_VALUE))
.addComponent(slika, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(slika, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zeleni)
.addComponent(crveni)
.addComponent(crtaj))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void zeleniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zeleniActionPerformed
slika.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zeleni.gif")));
Graphics g= slika.getGraphics();
g.setColor(Color.WHITE);
g.drawLine(50,100,100,100);
}//GEN-LAST:event_zeleniActionPerformed
private void crveniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crveniActionPerformed
slika.setIcon(new javax.swing.ImageIcon(getClass().getResource("/crveni.gif")));
Graphics g= slika.getGraphics();
g.setColor(Color.WHITE);
g.drawLine(50,100,100,100);
}//GEN-LAST:event_crveniActionPerformed
private void crtajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crtajActionPerformed
Graphics g= slika.getGraphics();
g.setColor(Color.WHITE);
g.drawLine(50,100,100,100);
}//GEN-LAST:event_crtajActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton crtaj;
private javax.swing.JButton crveni;
private javax.swing.JLabel slika;
private javax.swing.JButton zeleni;
// End of variables declaration//GEN-END:variables
}
/
package javaapplication16;
import java.awt.Graphics.*;
import java.awt.*;
public class Test extends javax.swing.JFrame {
public Test() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
slika = new javax.swing.JLabel();
zeleni = new javax.swing.JButton();
crveni = new javax.swing.JButton();
crtaj = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
slika.setText(" ");
zeleni.setText("zeleni");
zeleni.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zeleniActionPerformed(evt);
}
});
crveni.setText("crveni");
crveni.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
crveniActionPerformed(evt);
}
});
crtaj.setText("crtaj");
crtaj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
crtajActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(zeleni)
.addGap(18, 18, 18)
.addComponent(crveni)
.addGap(18, 18, 18)
.addComponent(crtaj)
.addContainerGap(155, Short.MAX_VALUE))
.addComponent(slika, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(slika, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(zeleni)
.addComponent(crveni)
.addComponent(crtaj))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void zeleniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zeleniActionPerformed
slika.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zeleni.gif")));
Graphics g= slika.getGraphics();
g.setColor(Color.WHITE);
g.drawLine(50,100,100,100);
}//GEN-LAST:event_zeleniActionPerformed
private void crveniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crveniActionPerformed
slika.setIcon(new javax.swing.ImageIcon(getClass().getResource("/crveni.gif")));
Graphics g= slika.getGraphics();
g.setColor(Color.WHITE);
g.drawLine(50,100,100,100);
}//GEN-LAST:event_crveniActionPerformed
private void crtajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crtajActionPerformed
Graphics g= slika.getGraphics();
g.setColor(Color.WHITE);
g.drawLine(50,100,100,100);
}//GEN-LAST:event_crtajActionPerformed
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton crtaj;
private javax.swing.JButton crveni;
private javax.swing.JLabel slika;
private javax.swing.JButton zeleni;
// End of variables declaration//GEN-END:variables
}
Ovi handleri(crveniActionPerformed i zeleniActionPerformed) su kljuci problem, jednostavno nece u obradi istog handlera prvo stavi sliku na labelu, pa potom nesto iscrta na njoj.
Ako nekom treba evo celog programa na rapidsharu: http://rapidshare.com/files/191976406/JavaApplication16.rar
Hvala svima unapred