ch habe einen kleinen Lvl editor Programiert, da das Lvl aber sehr breit ist muss man es eig mit scrool bar verschieben können um das ganze lvl bearbeiten zu können. Ich würde gerne wissen wie ich das am besten amche, alles was bis jetzt mit der JScroolBar und dem AdjustmentListener gemacht wurde kann, ist es warscheinlich auch, falsch sein! Nach fehlenden infos bitte Fragen
Java:
package main;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import java.awt.Graphics;
import java.awt.Scrollbar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Frame extends JFrame {
background bg;
paint screen;
JMenuBar bar;
JMenu file;
JMenuItem item;
JMenuItem save;
JScrollBar jScrollBar1;
JComboBox<String> skin;
String[] blockString = {"Boden", "Pipe", "Sky", "Player"};
String name;
private FileWriter writer;
private int skinSelection;
private int x;
private int y;
File doco;
ArrayList<Character> mapLetters;
ArrayList<background> bgArray;
ArrayList<background> bgFinish;
public Frame(background bg) {
super("LvlEditor");
this.bg = bg;
x = 0;
y = 0;
bgFinish = new ArrayList<background>();
mapLetters = new ArrayList<Character>();
bgArray = new ArrayList<background>();
screen = new paint();
for(int ii = 0; ii < bg.getBoxLength(); ii++){
char nicht = 'S' ;
mapLetters.add(nicht);
}
for(int i = 0; i < bg.getBoxLength();i++){
if(x >= 12800){
y += 64;
x = 0;
}
this.bg = new background(x, y, 0);
bgArray.add(this.bg);
x += 64;
System.out.println("y="+y/64+", x="+x/64);
}
skin = new JComboBox<String>(blockString);
bar = new JMenuBar();
file = new JMenu("File");
item = new JMenuItem("New");
save = new JMenuItem("Save");
jScrollBar1 = new JScrollBar();
screen.setBounds(512, 64, 1024, 768);
skin.setBounds(10, 50, 120, 50);
jScrollBar1.setBounds(0, 750, 1024, 17);
jScrollBar1.setOrientation(Scrollbar.HORIZONTAL);
screen.addMouseListener(new mouseListener());
skin.addItemListener(new itemListener());
item.addActionListener(new actionListener());
save.addActionListener(new actionListener());
jScrollBar1.addAdjustmentListener(new adjustmentListener());
bar.add(file);
file.add(item);
file.add(save);
setJMenuBar(bar);
add(skin);
add(screen);
screen.add(jScrollBar1);
}
private class actionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == item){
name = JOptionPane.showInputDialog(getParent(), "Name der File", "File", JOptionPane.OK_OPTION);
doco = new File(name+".txt");
try {
doco.createNewFile();
writer = new FileWriter(name+ ".txt");
}catch (IOException e1) {e1.printStackTrace();}
}
if(e.getSource() == save ){
for(int i = 0; i < bg.getBoxLength(); i++){
try {
writer.write(mapLetters.get(i));
writer.flush();
} catch (IOException e1) {e1.printStackTrace();}
}
try {
writer.close();
} catch (IOException e1) {e1.printStackTrace();}
}
}
}
private class adjustmentListener implements AdjustmentListener {
@Override
public void adjustmentPerformed(AdjustmentEvent e) {
if(e.getValue()==10){
System.out.println("gut");
}
}
}
private class itemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == e.SELECTED){
if(e.getSource() == skin) {
skinSelection = skin.getSelectedIndex();
}
}
}
}
private class mouseListener implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
for(int i = 0; i < bgArray.size();i++) {
if(e.getX() >= bgArray.get(i).getBounding().x && e.getX() <= bgArray.get(i).getBounding().x+64 && e.getY() >= bgArray.get(i).getBounding().y && e.getY() <= bgArray.get(i).getBounding().y+64){
bg = new background(bgArray.get(i).getBounding().x, bgArray.get(i).getBounding().y, skinSelection);
bgFinish.add(bg);
if(skinSelection == 0){
mapLetters.remove(i);
mapLetters.add(i,'G');
}
else if(skinSelection == 1){
mapLetters.remove(i);
mapLetters.add(i,'P');
}
else if(skinSelection == 2){
mapLetters.remove(i);
mapLetters.add(i,'S');
}
else if(skinSelection == 3){
mapLetters.remove(i);
mapLetters.add(i,'$');
}
System.out.println(i);
}
}
}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
@Override
public void mousePressed(MouseEvent e){}
@Override
public void mouseReleased(MouseEvent e){}
}
private class paint extends JLabel{
protected void paintComponent(Graphics g){
for(int i = 0; i < bgArray.size();i++) {
g.drawRect(bgArray.get(i).getBounding().x, bgArray.get(i).getBounding().y, bgArray.get(i).getBounding().width, bgArray.get(i).getBounding().height);
}
for(int ii = 0; ii < bgFinish.size();ii++){
g.drawImage(bgFinish.get(ii).getImage(), bgFinish.get(ii).getBounding().x,bgFinish.get(ii).getBounding().y, null );
}
}
}
public void repaintScreen() {
screen.repaint();
}
}