Jump to content

Java Advanced Paint

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Beeko

Beeko

    Learning Programmer

  • Members
  • PipPipPipPip
  • 101 posts
Hey guys, I'm working these days on a program like the Paint but a little more advanced, almost a prototype of Photoshop, so far I can make it draw :D Great, right? xD but I am stuck with a few stuff so far, Moving an item, I have no idea how to move a shape on a JPanel that has been drawn already, in my program I add all the Shapes in 1 ArrayList so that every time the user tries drawing a new 1 the previous are not deleted.

I even went as far as trying to make a secondary frame that will show "Layers" so that the user could choose every shape on a layer and control their ranks above each other. But no idea how to implement it :D

Another thing is, JRadioButtons, I Love their job, but I hate the UI in them.... I wanted to make something like the Paint, few Buttons if you click one change the others = false, but was kind of depressing (way too poor coding) are there any Java Objects that can do it?

I opened this thread for those who are willing to share some Ideas with me, I have a few points that will be updated every time someone participates.

1- How to Move a Shape on the Panel.
2- How to Make a Layer Frame to control the Shapes' levels.
3- How to Save my final outcome in a format for the program (like a .psd you can open and edit your work from last time.) As in "Save or Save As: My_Format"
4- How to Make the JRadioButton look like a Button.

Every time a problem of those are solved, I'll update them as (Solved). Hope we could work this project out, Thanks for your help guys.

Best Regards;
Beeko.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

1- How to Move a Shape on the Panel.
Either the shape has a translate method, or else they got a setter method which lets you move it.
Rectangle and Polygon got such a translate(deltaX, deltaY) method.
Line2D has a setLine(x, y, x2, y2) method.
Ellipse2D has a setFrame(x, y, width, height) method.

And I think those 4 shapes will be the most used.
Small program where I move 2 shapes:

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.List;


public class MoveShape extends JFrame {

    private List<Shape> shapes;


    public MoveShape(String title) throws HeadlessException {

        super(title);

        shapes = new ArrayList<Shape>();

        shapes.add(new Rectangle(0, 0, 50, 50));

        shapes.add(new Polygon(new int[]{0, 70, 140, 100, 90}, new int[]{50, 14, 80, 240, 270}, 5));


        JButton button = new JButton("move");

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                for (Shape shape : shapes) {

                    if (shape instanceof Polygon) {

                        ((Polygon) shape).translate(50, 80);

                    }

                    if (shape instanceof Rectangle) {

                        ((Rectangle) shape).translate(100, 100);

                    }

                }

                repaint();

            }

        });

        add(button, BorderLayout.SOUTH);

        setSize(800,800);

        setVisible(true);

    }


    @Override

    public void paint(Graphics g) {

        super.paint(g);

        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.WHITE);

        g2.fillRect(0, 0, getWidth(), getHeight());


        g2.setColor(Color.BLACK);

        for (Shape shape : shapes) {

            g2.draw(shape);

        }

    }


    public static void main(String[] args) {

        MoveShape moveShape = new MoveShape("move painted shape.");

    }

}

Quote

2- How to Make a Layer Frame to control the Shapes' levels.
That's with JLayeredPane you're doing that?
What do you mean with control the shape's levels? You mean the Z-index?


Quote

3- How to Save my final outcome in a format for the program (like a .psd you can open and edit your work from last time.) As in "Save or Save As: My_Format"
You are able to write objects to a file. The most drastic solution would be to put your whole GUI object which contains all the shape data in it into a file.
That's propably a bit overkill tho, you don't need to store all the jpanels and jbuttons etc.

You need to figure out which data you need, and which you don't, write those to a file. And read them out later.
I happen to be busy on a java-example program containing lots of examples. And out of the few examples I allready added, reading and writing objects is 1 of em. So here's the examples to read & write:
Attached File  WriteObjectsToFile.png   62.79K   53 downloads
Note you can write and read more than 1 object. And those objects don't have to be of the same class.

Quote

4- How to Make the JRadioButton look like a Button.

Quote

Another thing is, JRadioButtons, I Love their job, but I hate the UI in them.... I wanted to make something like the Paint
Where in paint do you find radiobuttons? :)
Or do you mean a (J)Togglebutton like those bold/italic/underline buttons
Posted Image

To make others go false when 1 is pressed, I think you can put all the buttons in a ButtonGroup

Attached Files






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users