D
Dr.Java00
Gast
hallo erstmal
ich habe da ein problem!
Ich programmiere JAVA mit GUI in NetBeans IDE 6.8.
Habe ein Fenster erzeugt wo 3 Buttons drauf sind, ein Zeichenfeld, zwei jSlider (um es verschieben zu können an der X UND Y achse).
Alles lief gut.
Wenn ich auf die Buttons klicke, wird automatisch ein Stern,Dreieck und Viereck gezeichnet.
Da hatte ich die Idee noch einen Slider einzufügen, mit dem ich die Größe ändern kann, doch da hängts bei mir.
Habe keinen Plan wie das gehen soll.
Bitte um schnelle Antwort!
[JAVA=42]
package formenzeichnen;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
/**
*
* @author sacherik
*/
public class Fenster extends javax.swing.JFrame {
private boolean paintOn = false;
private ArrayList liste = new ArrayList();
private int screenMiddleX;
private int screenMiddleY;
private int selectedLine;
private JTable jTable;
/** Creates new form Fenster */
public Fenster() {
initComponents();
this.refreshTable();
paintOn = true;
screenMiddleX = 4+jSliderX.getX()+jSliderX.getWidth()/2;
screenMiddleY = 30+jSliderY.getY()+jSliderY.getHeight()/2;
}
private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jSliderYStateChanged(javax.swing.event.ChangeEvent evt) {
if (!liste.isEmpty()) {
((Formen) liste.get(selectedLine)).setPosy(calculatePosY(jSliderY));
repaint();
}
}
private void jSliderXStateChanged(javax.swing.event.ChangeEvent evt) {
if (!liste.isEmpty()) {
((Formen) liste.get(selectedLine)).setPosx(calculatePosX(jSliderX));
repaint();
}
repaint();
}
private void jTableFocusGained(java.awt.event.FocusEvent evt) {
tableSelectionChanged();
setSliders((Formen) this.liste.get(selectedLine));
this.refreshTable();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Viereck viereck = new Viereck("Viereck"+(this.liste.size()+1),
screenMiddleX,
screenMiddleY);
this.liste.add(viereck);
//this.add(viereck.getVierEck());
this.selectedLine = liste.size()-1;
this.refreshTable();
this.setSliders(viereck);
repaint();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Dreieck dreieck = new Dreieck("Dreieck"+(this.liste.size()+1),
screenMiddleX,
screenMiddleY);
this.liste.add(dreieck);
//this.add(dreieck.getDreiEck());
this.selectedLine = liste.size()-1;
this.refreshTable();
this.setSliders(dreieck);
repaint();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Stern stern = new Stern("Stern"+(this.liste.size()+1),
screenMiddleX,
screenMiddleY);
this.liste.add(stern);
//this.add(viereck.getVierEck());
this.selectedLine = liste.size()-1;
this.refreshTable();
this.setSliders(stern);
repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Fenster().setVisible(true);
}
});
}
@Override
public void paint(Graphics g) {
if (paintOn) {
super.paint(g);
//tableSelectionChanged();
for(int i=0; i<this.liste.size();i++) {
Formen eck = null;
Object itNext = this.liste.get(i);
// get DbTitles-Obj from titlesData ArrayList to refresh table
String formName = (itNext).getClass().getName();
if(formName.indexOf("Viereck") != -1) {
eck = (Viereck)this.liste.get(i);
}
if(formName.indexOf("Dreieck") != -1) {
eck = (Dreieck)this.liste.get(i);
}
if(formName.indexOf("Stern") != -1) {
eck = (Stern)this.liste.get(i);
}
eck.zeichnen(g);
}
}
}
public int calculatePosX(javax.swing.JSlider s) {
return (int)(screenMiddleX+((s.getValue()-50)*0.01*s.getWidth()));
}
public int calculatePosY(javax.swing.JSlider s) {
return (int)(screenMiddleY-((s.getValue()-50)*0.01*s.getHeight()));
}
public void setSliders(Formen form) {
jSliderX.setValue(((form.getPosx()-screenMiddleX)*100/jSliderX.getWidth())+50);
jSliderY.setValue(((screenMiddleY-form.getPosy())*100/jSliderY.getHeight())+50);
}
private void refreshTable()
{
DefaultTableModel dftm = new DefaultTableModel();
String a;
a="Ebenen";
dftm.addColumn(a);
Vector v = new Vector();
v.add(a);
dftm.setColumnIdentifiers(v);
try{
Iterator it = this.liste.iterator();
while(it.hasNext())
{
Formen formenObj = null;
Object itNext = it.next();
// get DbTitles-Obj from titlesData ArrayList to refresh table
String formName = (itNext).getClass().getName();
if(formName.indexOf("Viereck") != -1) {
formenObj = (Viereck) itNext;
}
if(formName.indexOf("Dreieck") != -1) {
formenObj = (Dreieck) itNext;
}
if(formName.indexOf("Stern") != -1) {
formenObj = (Stern) itNext;
}
// create Vector for viewing in table
Vector vec = new Vector();
vec.add(formenObj.getName());
dftm.addRow(vec);
}
}
catch(Exception ex){ex.printStackTrace();}
JTable tab = new JTable(dftm);
this.jScrollPane1.setViewportView(tab);
JViewport x = this.jScrollPane1.getViewport();
jTable = (JTable) x.getComponent(0);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setRowSelectionAllowed(true);
jTable.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jTableFocusGained(evt);
}
});
ListSelectionModel selectionModel = jTable.getSelectionModel();
selectionModel.setSelectionInterval(selectedLine, selectedLine);
}
public void tableSelectionChanged() {
int line = jTable.getSelectedRow();
if(line != -1 && selectedLine != line) {
selectedLine = line;
}
}
[/code]
wenn ihr weitere klassen braucht nur schreiben
P.S: ich mache das mit Interface und Vererbung
ich habe da ein problem!
Ich programmiere JAVA mit GUI in NetBeans IDE 6.8.
Habe ein Fenster erzeugt wo 3 Buttons drauf sind, ein Zeichenfeld, zwei jSlider (um es verschieben zu können an der X UND Y achse).
Alles lief gut.
Wenn ich auf die Buttons klicke, wird automatisch ein Stern,Dreieck und Viereck gezeichnet.
Da hatte ich die Idee noch einen Slider einzufügen, mit dem ich die Größe ändern kann, doch da hängts bei mir.
Habe keinen Plan wie das gehen soll.
Bitte um schnelle Antwort!
[JAVA=42]
package formenzeichnen;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
/**
*
* @author sacherik
*/
public class Fenster extends javax.swing.JFrame {
private boolean paintOn = false;
private ArrayList liste = new ArrayList();
private int screenMiddleX;
private int screenMiddleY;
private int selectedLine;
private JTable jTable;
/** Creates new form Fenster */
public Fenster() {
initComponents();
this.refreshTable();
paintOn = true;
screenMiddleX = 4+jSliderX.getX()+jSliderX.getWidth()/2;
screenMiddleY = 30+jSliderY.getY()+jSliderY.getHeight()/2;
}
private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jSliderYStateChanged(javax.swing.event.ChangeEvent evt) {
if (!liste.isEmpty()) {
((Formen) liste.get(selectedLine)).setPosy(calculatePosY(jSliderY));
repaint();
}
}
private void jSliderXStateChanged(javax.swing.event.ChangeEvent evt) {
if (!liste.isEmpty()) {
((Formen) liste.get(selectedLine)).setPosx(calculatePosX(jSliderX));
repaint();
}
repaint();
}
private void jTableFocusGained(java.awt.event.FocusEvent evt) {
tableSelectionChanged();
setSliders((Formen) this.liste.get(selectedLine));
this.refreshTable();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Viereck viereck = new Viereck("Viereck"+(this.liste.size()+1),
screenMiddleX,
screenMiddleY);
this.liste.add(viereck);
//this.add(viereck.getVierEck());
this.selectedLine = liste.size()-1;
this.refreshTable();
this.setSliders(viereck);
repaint();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Dreieck dreieck = new Dreieck("Dreieck"+(this.liste.size()+1),
screenMiddleX,
screenMiddleY);
this.liste.add(dreieck);
//this.add(dreieck.getDreiEck());
this.selectedLine = liste.size()-1;
this.refreshTable();
this.setSliders(dreieck);
repaint();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Stern stern = new Stern("Stern"+(this.liste.size()+1),
screenMiddleX,
screenMiddleY);
this.liste.add(stern);
//this.add(viereck.getVierEck());
this.selectedLine = liste.size()-1;
this.refreshTable();
this.setSliders(stern);
repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Fenster().setVisible(true);
}
});
}
@Override
public void paint(Graphics g) {
if (paintOn) {
super.paint(g);
//tableSelectionChanged();
for(int i=0; i<this.liste.size();i++) {
Formen eck = null;
Object itNext = this.liste.get(i);
// get DbTitles-Obj from titlesData ArrayList to refresh table
String formName = (itNext).getClass().getName();
if(formName.indexOf("Viereck") != -1) {
eck = (Viereck)this.liste.get(i);
}
if(formName.indexOf("Dreieck") != -1) {
eck = (Dreieck)this.liste.get(i);
}
if(formName.indexOf("Stern") != -1) {
eck = (Stern)this.liste.get(i);
}
eck.zeichnen(g);
}
}
}
public int calculatePosX(javax.swing.JSlider s) {
return (int)(screenMiddleX+((s.getValue()-50)*0.01*s.getWidth()));
}
public int calculatePosY(javax.swing.JSlider s) {
return (int)(screenMiddleY-((s.getValue()-50)*0.01*s.getHeight()));
}
public void setSliders(Formen form) {
jSliderX.setValue(((form.getPosx()-screenMiddleX)*100/jSliderX.getWidth())+50);
jSliderY.setValue(((screenMiddleY-form.getPosy())*100/jSliderY.getHeight())+50);
}
private void refreshTable()
{
DefaultTableModel dftm = new DefaultTableModel();
String a;
a="Ebenen";
dftm.addColumn(a);
Vector v = new Vector();
v.add(a);
dftm.setColumnIdentifiers(v);
try{
Iterator it = this.liste.iterator();
while(it.hasNext())
{
Formen formenObj = null;
Object itNext = it.next();
// get DbTitles-Obj from titlesData ArrayList to refresh table
String formName = (itNext).getClass().getName();
if(formName.indexOf("Viereck") != -1) {
formenObj = (Viereck) itNext;
}
if(formName.indexOf("Dreieck") != -1) {
formenObj = (Dreieck) itNext;
}
if(formName.indexOf("Stern") != -1) {
formenObj = (Stern) itNext;
}
// create Vector for viewing in table
Vector vec = new Vector();
vec.add(formenObj.getName());
dftm.addRow(vec);
}
}
catch(Exception ex){ex.printStackTrace();}
JTable tab = new JTable(dftm);
this.jScrollPane1.setViewportView(tab);
JViewport x = this.jScrollPane1.getViewport();
jTable = (JTable) x.getComponent(0);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setRowSelectionAllowed(true);
jTable.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jTableFocusGained(evt);
}
});
ListSelectionModel selectionModel = jTable.getSelectionModel();
selectionModel.setSelectionInterval(selectedLine, selectedLine);
}
public void tableSelectionChanged() {
int line = jTable.getSelectedRow();
if(line != -1 && selectedLine != line) {
selectedLine = line;
}
}
[/code]
wenn ihr weitere klassen braucht nur schreiben
P.S: ich mache das mit Interface und Vererbung