Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Implementing zoom >.<  (Read 1995 times)
0 Members and 1 Guest are viewing this topic.
Offline Sequalit

Junior Member





« Posted 2006-05-01 19:21:48 »

I have a question on implementing zoom on my paint program. I dont understand how exactly it would be done.

you have your canvas, and you can draw each pixel (a fillRect with 1 pixel x 1 pixel) well, i want to be able to zoom in, so you can see each individual pixel and do pixel art, but this zoom also needs to be in a scroll pane.

So how exactly would i go about doing this?

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2006-05-01 19:31:17 »

Use the version of drawImage that allows you to set the desitnation rectangle and set it to whatever your zoom multiple is.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Sequalit

Junior Member





« Reply #2 - Posted 2006-05-02 02:30:38 »

well... thats great if im wanting to zoom in on an image, but im working with an array of rectangles (the pixels that are drawn)

or should i convert that arraylist of rectangles to a jpg somehow, and just draw ontop of the jpeg (great except when they erase and area)

question: should i just figure out the size of my image, and do some math with that + the zoom ratio and then size the rectangles accordingly? or is there a easier way.

heres my code:

Main Class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
package version01;

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class SeqyPaint{
   JFrame frame;
   JPanel panel;
   
   public SeqyPaint(){
      frame = new JFrame("Seqy Paint");
      frame.setSize(100,100);
      //panel = new JPanel(new FlowLayout());
     addWidgets(frame.getContentPane());
      //frame.getContentPane().add(panel);
     //frame.pack();
     frame.setSize(500,500);
      frame.setVisible(true);
   }
   public void addWidgets(Container container){
      DrawingArea drawing = new DrawingArea(this);
      container.add(drawing);
   }
   public static void createAndShowGUI(){
      SeqyPaint sp = new SeqyPaint();
   }
   public static void main(String[] args){
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
         public void run(){
            createAndShowGUI();
         }
      });
   }
}


Drawing canvas
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
package version01;

import javax.swing.JComponent;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DrawingArea extends JComponent implements MouseListener{
   SeqyPaint controller;
   ArrayList pixels;
   public DrawingArea(SeqyPaint controller){
      this.controller = controller;
      pixels = new ArrayList();
      addMouseListener(this);
   }
   public void paintComponent(Graphics g){
      g.setColor(Color.BLUE);
      g.fillRect(0,0,getWidth(), getHeight());
      g.setColor(Color.GREEN);
      g.drawRect(0,0,100,100);
     
      drawPixels(g);
   }
   public void drawPixels(Graphics g){
      for(int i=0;i<pixels.size();i++){
         Pixel t = (Pixel)pixels.get(i);
         g.setColor(new Color(t.R, t.B, t.G));
         g.fillOval(t.p.x, t.p.y, 10, 10);
      }
   }
   public void addPixel(MouseEvent e){
      Pixel t = new Pixel();
      t.R = 100;
      t.G = 255;
      t.B = 45;
      t.p = e.getPoint();
      pixels.add(t);
   }
   public void mouseEntered(MouseEvent e){}
   public void mousePressed(MouseEvent e){}
   public void mouseReleased(MouseEvent e){}
   public void mouseClicked(MouseEvent e){
      addPixel(e);
      repaint();
   }
   public void mouseExited(MouseEvent e){}
}


Pixel (the rectangle)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
package version01;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Pixel{
   int R, B, G;
   Point p;
   
   public Pixel(){
   }
   
}

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2006-05-02 12:21:48 »

This might help:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;

public class Test extends JPanel implements KeyListener {
    Color colors[][];
    double scale = 1.0;

    public Test() {
        colors = new Color[20][20];
        for(int x=0;x<20;x++) {
            for(int y=0;y<20;y++) {
                colors[x][y] = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
            }
        }
        addKeyListener(this);
    }
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_UP) {
            scale += 0.1;
            repaint();
        }
        else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            scale -= 0.1;
            repaint();
        }
    }
    public void keyReleased(KeyEvent e){}
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        super.paintComponent(g2);
        g2.scale(scale, scale);
        for(int x=0;x<20;x++) {
            for(int y=0;y<20;y++) {
                g2.setColor(colors[x][y]);
                g2.drawLine(x, y, x, y);
            }
        }
    }
    public static void main(String args[]) {
        JFrame f = new JFrame();
        Test t = new Test();
        f.addKeyListener(t);
        f.getContentPane().add(t);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

Offline Sequalit

Junior Member





« Reply #4 - Posted 2006-05-02 19:14:31 »

that works nicely, but what about snapping the squares to grid, like you cant overlap them cause their the size of the pixel just inlarged? im guessing its time for math? if so i can figure that out.

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Offline g666

Junior Member





« Reply #5 - Posted 2006-05-03 09:52:18 »

Are you sure representing the image with an array of rectangles is the easiest way? just use a BufferedImage.

desperately seeking sanity
Offline Sequalit

Junior Member





« Reply #6 - Posted 2006-05-03 21:52:22 »

Are you sure representing the image with an array of rectangles is the easiest way? just use a BufferedImage.

hehe as i just figured out it isnt (starts to lag incredibly after about 100 squares)
ive looked at bufferedimage api and i didnt know you could create something that you could draw on, how do you do that?

i got my zoom working perfectly thank youCaptainJester! =D

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Offline Sequalit

Junior Member





« Reply #7 - Posted 2006-05-03 22:09:28 »

ive done some research into drawing on a buffered image and came up with some hits *yay for google*

basically you create the buffered image, use that to create a graphics, then draw on that, then you draw the image

but i still need to know where to draw new pixels and stuff....

what about still keeping my aray of pixels, but whenever i draw them to the image, remove them from the array, that way im only computing the newest pixels into the image.? doesnt sound that clean but cleaner that what i have atm lol

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2006-05-03 22:34:54 »

Im not sure I am fully understanding your design/issue BUT...

Are you aware that you can get a Raster from a buffered image and set/get your pixels directly in that?

Im not sure why you need another data structure at all...

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline rdcarvallo

Senior Member


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #9 - Posted 2006-05-04 15:50:52 »

The buffered image has a method setRGB(int x, int y, int argb) so you can modify directly the colors in the image.
   Rafael.-


 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sequalit

Junior Member





« Reply #10 - Posted 2006-05-04 19:04:06 »

Hehe your right Jeff, i dont fully understand it, but thats why im doing this =) to learn.

hrmm... well i got it working perfectly last night, i could zoom, anddraw on my buffered image and everything, with no performance issues (that i saw)

but my way of doing it might be a bad design...

what i do currently is i put my bufferedImage into a HashMap, then i call it from the hashmap, use "bufferedimage.createGraphics()" to get the Graphics2D, then i tell my brush class to draw itself on the image given the Graphics2D, since i have different brushes i figured creating a class for each brush implementing a brush interface would work for that. after the brush is finished doing its thing, the main loop just paints it to the scrollpane.

so what would be better for drawing, setRBG/Raster, or just using the graphics2d class to draw ? (it seems it would be easier to implement flow and opacity using the Graphics2D class than using rasters/setrbg, or am i wrong?)

while(gettingTired())
     crankOutMoreCode();
     if(asleep()){
          wakeUp();
          makeCoffee();
          chugCoffee();
     }
}

-Sequalit
Offline Jeff

JGO Coder




Got any cats?


« Reply #11 - Posted 2006-05-06 00:57:35 »

Using the higher level Graphcis commands are preferrable as they give you the best chance of getting hardware aceleration.  So you are doing it right itr sounds like.

I only mentioned that you can set as well as get pixels witha  Raster inc ase you were doing something that *had* to be software rendered on a per-pixel bases.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06

BurntPizza (55 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!