Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.BufferedImage;
- import javax.swing.*;
- public class SelbstBrot implements ActionListener, MouseListener, MouseMotionListener, MouseWheelListener{
- private final int DRAW_BROT = 0;
- private final int DRAG_SQUARE = 1;
- private final int ZOOM_IN = 2;
- private final int ZOOM_OUT = 3;
- private int modus = DRAW_BROT;
- public final int WINDOW_SIZE = 500;
- private int mousePressedX = 0;
- private int mousePressedY = 0;
- private int mouseCurrentX = 0;
- private int mouseCurrentY = 0;
- BrotZeichnen b;
- JFrame frame;
- Container brotContainer;
- public SelbstBrot()
- {
- b = new BrotZeichnen();
- frame = new JFrame("SelbstBrot");
- brotContainer = new Container(){
- public void paint(Graphics g) {
- switch (modus) {
- case DRAG_SQUARE:
- g.drawImage(b.getCurrentBrot(), 0, 0, null);
- drawSquare(g);
- break;
- case ZOOM_IN:
- drawSquare(g);
- // TODO warum wird square erst bei drawImage geschrieben?
- b.drawBread (b.getCurrentBrot());
- g.drawImage(b.getCurrentBrot(), 0, 0, null);
- break;
- case ZOOM_OUT:
- b.drawBread (b.getCurrentBrot());
- g.drawImage(b.getCurrentBrot(), 0, 0, null);
- // drawSquare(g);
- break;
- case DRAW_BROT:
- b.drawBread (b.getCurrentBrot());
- g.drawImage(b.getCurrentBrot(), 0, 0, null);
- break;
- }
- }};
- brotContainer.setSize(WINDOW_SIZE, WINDOW_SIZE);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(500, 520);
- frame.setJMenuBar(createMenuBar());
- frame.add(brotContainer);
- frame.setVisible(true);
- brotContainer.addMouseListener(this);
- brotContainer.addMouseMotionListener(this);
- brotContainer.addMouseWheelListener(this);
- b.setCurrentBrot(new BufferedImage(500,500, BufferedImage.TYPE_INT_RGB ));
- }
- /**
- * Detects the drawing mode and draws a square into the specified Graphics accordingly.
- * @param brot
- */
- private void drawSquare(Graphics brot) {
- int x = 0, y = 0, sideLength = 0;
- switch (modus) {
- case DRAG_SQUARE:
- x = mousePressedX;
- y = mousePressedY;
- sideLength = Math.max(mouseCurrentX-mousePressedX, mouseCurrentY-mousePressedY);
- if (sideLength < 0)
- sideLength = 0;
- break;
- case ZOOM_IN:
- x = mousePressedX - WINDOW_SIZE/20;
- y = mousePressedY - WINDOW_SIZE/20;
- sideLength = WINDOW_SIZE/10;
- // System.out.println("fail2");
- break;
- case ZOOM_OUT:
- // TODO frame does not work if checkValues modifies xpixel and ypixel
- x = WINDOW_SIZE/2 - WINDOW_SIZE/20;
- y = WINDOW_SIZE/2 - WINDOW_SIZE/20;
- sideLength = WINDOW_SIZE/10;
- break;
- }
- // System.out.println(x);
- brot.setColor(Color.white);
- brot.drawRect(x, y, sideLength, sideLength);
- }
- @Override
- public void mousePressed(MouseEvent e) {
- mousePressedX = e.getX();
- mousePressedY = e.getY();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- modus = DRAW_BROT;
- switch (e.getButton()) {
- case MouseEvent.BUTTON1: //left mouse button: square dragged, now zoom
- int xabstand = e.getX() - mousePressedX;
- if (xabstand > 0) {
- b.setXpixel(b.getXpixel() + mousePressedX*b.getPixelsize());
- b.setYpixel(b.getYpixel() + mousePressedY*b.getPixelsize());
- double verhaeltnis = WINDOW_SIZE / xabstand;
- b.setPixelsize(b.getPixelsize()/verhaeltnis);
- brotContainer.repaint();
- }
- break;
- case MouseEvent.BUTTON2: //double-click with mouse wheel: reset
- if (e.getClickCount() == 2) {
- resetValuesAndRepaint();
- }
- break;
- case MouseEvent.BUTTON3: //right mouse button dragged: drag
- int xAbstand = mousePressedX-e.getX();
- int yAbstand = mousePressedY-e.getY();
- if (xAbstand != 0 || yAbstand != 0) {
- b.setXpixel (b.getXpixel() + xAbstand*b.getPixelsize());
- b.setYpixel (b.getYpixel() + yAbstand*b.getPixelsize());
- brotContainer.repaint();
- }
- break;
- }
- }
- @Override
- public void mouseWheelMoved(MouseWheelEvent e) {
- mousePressedX = e.getX();
- mousePressedY = e.getY();
- double neueMittex = b.getXpixel() + mousePressedX * b.getPixelsize();
- double neueMittey = b.getYpixel() + mousePressedY * b.getPixelsize();
- if (e.getWheelRotation() < 0) {
- b.setPixelsize(b.getPixelsize()/10);
- modus = ZOOM_IN;
- }
- else {
- b.setPixelsize(b.getPixelsize()*10);
- modus = ZOOM_OUT;
- }
- b.setXpixel(neueMittex - WINDOW_SIZE/2 * b.getPixelsize());
- b.setYpixel(neueMittey - WINDOW_SIZE/2 * b.getPixelsize());
- checkValues();
- brotContainer.repaint();
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- if (e.getModifiersEx() == InputEvent.BUTTON1_DOWN_MASK) {
- mouseCurrentX = e.getX();
- mouseCurrentY = e.getY();
- modus = DRAG_SQUARE;
- brotContainer.repaint();
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- modus = DRAW_BROT;
- String command = e.getActionCommand();
- if (command.equals("red")) {
- b.setColorDivisors(1, 4, 4);
- brotContainer.repaint();
- }
- else if (command.equals("blue")) {
- b.setColorDivisors(4, 2, 1);
- brotContainer.repaint();
- }
- else if (command.equals("green")) {
- b.setColorDivisors(4, 1, 2);
- brotContainer.repaint();
- }
- else if (command.equals("freaky")) {
- b.setColorDivisors(0.1, 0.2, 0.3);
- brotContainer.repaint();
- }
- else if (command.equals("custom")) {
- final JDialog dialog = new JDialog(frame, "Coustom color", true);
- dialog.setLayout(new GridLayout(4, 2));
- dialog.setLocationRelativeTo(brotContainer);
- Label redLabel = new Label("Red:");
- Label greenLabel = new Label("Green:");
- Label blueLabel = new Label("Blue:");
- final TextField redField = new TextField(3);
- final TextField greenField = new TextField(3);
- final TextField blueField = new TextField(3);
- JButton ok = new JButton("ok");
- ok.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try {b.setColorDivisors(Double.valueOf(redField.getText()),
- Double.valueOf(greenField.getText()),
- Double.valueOf(blueField.getText()));}
- catch (Exception ex) {}
- dialog.setVisible(false);
- paint();
- }});
- JButton cancel = new JButton("cancel");
- cancel.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- dialog.setVisible(false);
- }
- });
- dialog.add(redLabel);
- dialog.add(redField);
- dialog.add(greenLabel);
- dialog.add(greenField);
- dialog.add(blueLabel);
- dialog.add(blueField);
- dialog.add(ok);
- dialog.add(cancel);
- dialog.pack();
- dialog.setVisible(true);
- }
- else if (command.equals("Get coordinates")) {
- System.out.println(b.getXpixel());
- System.out.println(b.getYpixel());
- System.out.println(b.getPixelsize());
- brotContainer.repaint();
- }
- }
- public void checkValues() {
- if (b.getXpixel() < -2.0) b.setXpixel(-2.0);
- if (b.getYpixel() < -1.5) b.setYpixel(-1.5);
- if (b.getPixelsize() > 0.00625) b.setPixelsize(0.00625);
- if (b.getXpixel()+WINDOW_SIZE*b.getPixelsize() > 1.2)b.setXpixel(1.2 - WINDOW_SIZE*b.getPixelsize());
- if (b.getYpixel()+WINDOW_SIZE*b.getPixelsize() > 1.7)b.setYpixel(1.7 - WINDOW_SIZE*b.getPixelsize());
- }
- public void paint() {
- brotContainer.repaint();
- }
- public void resetValuesAndRepaint() {
- b.setXpixel(-2.0);
- b.setYpixel(-1.5);
- b.setPixelsize(0.00625);
- brotContainer.repaint();
- }
- public JMenuBar createMenuBar() {
- JMenuBar menuBar = new JMenuBar();
- JMenu farbe = new JMenu("Farbe");
- menuBar.add(farbe);
- ButtonGroup group = new ButtonGroup();
- JRadioButtonMenuItem yellowButton = new JRadioButtonMenuItem("yellow");
- yellowButton.setSelected(true);
- JRadioButtonMenuItem redButton = new JRadioButtonMenuItem("red");
- JRadioButtonMenuItem blueButton = new JRadioButtonMenuItem("blue");
- JRadioButtonMenuItem greenButton = new JRadioButtonMenuItem("green");
- JRadioButtonMenuItem freakyButton = new JRadioButtonMenuItem("freaky");
- JRadioButtonMenuItem customButton = new JRadioButtonMenuItem("custom");
- group.add(yellowButton);
- group.add(redButton);
- group.add(blueButton);
- group.add(greenButton);
- group.add(freakyButton);
- group.add(customButton);
- yellowButton.addActionListener(this);
- redButton.addActionListener(this);
- blueButton.addActionListener(this);
- greenButton.addActionListener(this);
- freakyButton.addActionListener(this);
- customButton.addActionListener(this);
- farbe.add(yellowButton);
- farbe.add(redButton);
- farbe.add(blueButton);
- farbe.add(greenButton);
- farbe.add(freakyButton);
- farbe.add(customButton);
- JMenuItem getCoordinates = new JMenuItem("Get coordinates");
- menuBar.add(getCoordinates);
- getCoordinates.addActionListener(this);
- return menuBar;
- }
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- public void mouseClicked(MouseEvent e) {}
- public void mouseMoved(MouseEvent e) {}
- public static void main (String args[])
- {
- new SelbstBrot();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement