From bd0732cfec8982dd3d728defe6728d3116d12af2 Mon Sep 17 00:00:00 2001 From: Marco Stefanetti <marco.stefanetti@gmail.com> Date: Mon, 26 Oct 2020 20:29:21 +0000 Subject: [PATCH] JFrame, JPanel and mouse handling --- .../com/aparapi/examples/mandel/AfGUI.java | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/main/java/com/aparapi/examples/mandel/AfGUI.java diff --git a/src/main/java/com/aparapi/examples/mandel/AfGUI.java b/src/main/java/com/aparapi/examples/mandel/AfGUI.java new file mode 100644 index 00000000..f0f2eefa --- /dev/null +++ b/src/main/java/com/aparapi/examples/mandel/AfGUI.java @@ -0,0 +1,178 @@ +package com.aparapi.examples.mandel; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionAdapter; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.image.BufferedImage; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class AfGUI { + + private AfMain main; + + private JFrame frame; + + private int __COLORS=128; + private int __BLACK=Color.BLACK.getRGB(); + + private int[] colors=new int[__COLORS]; + private BufferedImage image; + private JPanel panel; + + boolean dragging = false; + private int px; + private int py; + + + public AfGUI(AfMain _main) { + + main = _main; + + for(int c=0;c<__COLORS;c++) + { + float hue = (float) c/ (float) __COLORS; + float saturation = 1.0f; + float brightness = 1.0f; + Color color = Color.getHSBColor(hue, saturation, brightness); + colors[c] = color.getRGB(); + } + + + frame = new JFrame("Fractals"); + frame.setSize(main.W, main.H); + frame.setBackground(Color.BLACK); + + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent windowEvent) { + System.exit(0); + } + }); + + panel = new JPanel() { + private static final long serialVersionUID = -2006337199526432552L; + + public void paint(Graphics g) { + g.drawImage(image, 0, 0, this); + } + }; + panel.setBackground(Color.BLACK); + panel.setSize(main.W, main.H); + + image = new BufferedImage(main.W, main.H, BufferedImage.TYPE_INT_RGB); + + frame.add(panel); + + + frame.addMouseWheelListener(new MouseWheelListener() { + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + + e.consume(); + + int amount = (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) ? e.getUnitsToScroll() + : (e.getWheelRotation() < 0 ? -1 : 1); + + System.out.printf("wheel %d \n", amount); + + float zoom = 1f + ((float) amount * 5 / 100f); + main.move(main.W / 2, main.H / 2, zoom); + main.refresh(); + + } + }); + + frame.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + + if (e.getClickCount() == 2 && !e.isConsumed()) { + e.consume(); + System.out.println("Double Click"); + main.move(e.getPoint().x, main.H - e.getPoint().y , 1f); + main.refresh(); + } + + } + + @Override + public void mousePressed(MouseEvent e) { + + System.out.printf("mouse pressed %d %d \n", e.getPoint().x, e.getPoint().y); + px = e.getPoint().x; + py = e.getPoint().y; + dragging=true; + } + + @Override + public void mouseReleased(MouseEvent event) { + + dragging = false; + } + + }); + + frame.addMouseMotionListener(new MouseMotionAdapter() { + + + @Override + public void mouseDragged(MouseEvent e) { + + if(dragging) + { + int nx = e.getPoint().x; + int ny = e.getPoint().y; + + int dx = px - nx; + int dy = py - ny; + + System.out.printf("mouse drag %d,%d \n", dx, dy); + main.move(main.W/2 + dx, main.H/2 - dy, 1f); + main.refresh(); + + px=nx; + py=ny; + } + } + + }); + + frame.setVisible(true); + int cw = panel.getWidth(); + int ch = panel.getHeight(); + frame.setSize(2 * main.W - cw, 2 * main.H - ch); + panel.setSize(main.W, main.H); + + } + + public void setColor(int i, int j, int t) { + + int c = colors[t%__COLORS]; + + if (t >= main.max_iterations) + c = __BLACK; + + image.setRGB(i, main.H - j - 1, c); + + } + + public void refresh() { + + for (int k = 0; k < main.rgb.length; k++) + setColor(k / main.W, k % main.W, main.rgb[k]); + + panel.repaint(); + + System.out.printf("canvas refreshed : %d x %d \n", panel.getWidth(), panel.getHeight()); + + } + +} -- GitLab