Marco Galli

+ Follow
since Dec 01, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Marco Galli

Hi, I'm trying to create a sort of Pixel drawing application, I've started using BufferedImage and drawing on its graphic object with graphics method, but I feel really limited for example scaling, panning custom drawing algorithm, custom shapes are all difficult to manage, so I was thinking of another way to do it and came to my mind the idea of creating my own image by drawing each pixel with graphics (drawRect) and then when I want to export the image construct a BufferedImage with the data collected inside my custom class.
What do you think it's better?
1 week ago
Well, I started to do refactor of everything (lol), I have another question about the same topic:
I currently have a Toolbar class that extends JPanel, and I add many components to this class, is this ok or not? I mean in general, is it ok to extend a JPanel and use it to add components and instanciate my custom class?

I just want to clarify this last thing in my mind to remove every doubt
1 month ago

Campbell Ritchie wrote:Most people extend JPanel, overriding only the paintComponent() method to do drawing etc. Not paintComponents(). Are you creating the buffered image from the drawing? If so, how? I would be interested to see how you do that

I use JComponent because I need to draw only in the area specific of the image, JPanel cause the whole panel to be drawn
This is the code I use to draw onto BufferedImage, when the user click mouse1 you call a method to draw

So what would be a case where I need to extend a class and not to use nested classes?
1 month ago

Campbell Ritchie wrote:
As for the Action class. Why are you saying anything about Canvas? Waht does opening a new file mean? Does it mean you are opening a file to save your work, or that you are clearing the display to start drawing anew? What does the new file dialogue do? Is it used elsewhere? If not, why not make it a private (probably static) nest class in the Action class? If you are clearing the Canvas, are you saving the previous drawing anywhere?

The Canvas is a class, it extends JComponent and has a simple BufferedImage onto which I can draw, one method to initialize a new image (called when i want to create a new blank canvas), and some methods to drag around the image, zoom in and out, etc
what opening a new file does is just creating a new blank BufferedImage, the idea was to ask the user if he wants to save before clearing.
new file dialog is a JDialog, it has text fields to input the desired size of the image, some buttons to choose the background (transparent, black, white), one button "Ok" to submit the data and "Cancel" to close the dialog and go back
new file dialog is used only in this area of the program if i remember correclty
I didn't make it nested in other classes to keep a good readability, I've always tried to use classes for everything to encapsulate the code in smaller chunks but it's clear that it's not the right approach
1 month ago

Please explain more.

Inside my Application class I define the size of the window, and the other things of the interface like menu, toolbars, canvas.

That doesn't sound a good idea to me; as a general rule of thumb avoid subclassing display classes. There are only a few that should actually be subclassed.

Me neither, I found these links that seem to explain my problem from a theoretical point of view, but still I don't know how to approach things
1 month ago
I'm working on a simple drawing application, the problem is that some classes require the access to things in other classes...
now I try to explain:
I have a general class for my application where I setup look and feel and general things like the menu.
I have created a class called MyMenu that inherits JMenuBar, in this class to create the buttons (like "new file") I'm using AbstractActions, in particular my NewFileAction is a class I've created that inherits AbstractAction.
The main problem comes when I try to create the behavior of this NewFileActions because it requires my Canvas class (instanciated inside the general class of the application) to create a new Image and I don't think passing the istance of the canvas through all the classes would be a good solution.

Maybe should I delete the classes for actions and just define them inside the Menu? I can't figure out a proper solution
In my opinion, the approach on how I do things with Swing is wrong

I hope my question is understandable
1 month ago

Campbell Ritchie wrote:Welcome to the Ranch

Short answer: NO.

Longer answer: Always consider what your display is going to look like. It will usually look better if you don't have windows/dialogues/etc. appearing and disappearing unexpectedly. But it is probably a bad idea to use two frames. If you want to click a button and have a new display appear with your image, it is usually better to use a JDialog with a JPanel added, and display the image on that panel. Unless the image is small (500×500px counts as small, 2048×3072px counts as large), you are probably better off using a scroll pane. See whether these two Java™ Tutorials sections help: 1  2.

[edit]I wasn't clear in my post. If you have a small image, display it on a JPanel. If you have a large image, add the panel you are showing the image on to a scroll pane. Is that any clearer?

Probably I wasn't clear

I mean, I currently have a JDialog where I can input the desired width and height of the image, press ok and then the image is created inside a panel in the main app.
Like Gimp, photoshop etc..
Similar for the preferences window
I don't know if it's ok to use JDialog or it's better to use JFrame
1 month ago
I've seen many people discouraging the use of JDialogs in applications and now I'm wondering when should I use it.
For example, in my application I have a window to create a new image with fields (height, width, background color etc), should it be a JDialog or it's better to use a JFrame?
Another example is the classic window for preferences with many options and fields, should this be a JDialog or not?
1 month ago