Problem with a simple drawing program
Hi!
I'm creating a program that is going to work as "paint". I have tried to make it possible to press/hold the mouse from one point and release it on another point.
Between these points I'm trying to draw a line, though it does'nt work as I want to. If I try to draw a line, nothing happens. BUT! if I change the size of the whole program window, the line I've just drawn will appear.
I want this line to appear without having to change the size of the window... what have I made wrong???
Can someone please tell me what I've done wrong? :)
Code Java:
package Projekt;
public class Main {
public static void main(String[] args){
Window window = new Window();
window.setSize(1000,800);
window.setVisible(true);
}
}
//Second class
package Projekt;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Window extends JFrame{
public int x;
public int y;
public int x_2;
public int y_2;
public JPanel p1;
public JButton button;
public JButton button2;
public Painting temp;
public Vector vector = new Vector();
public Window(){
super("Ritprogram");
p1 = new JPanel();
p1.setSize(1000, 100);
p1.setBackground(Color.WHITE);
button = new JButton("Save");
button2 = new JButton("Open");
temp = new Painting(0,0,0,0);
getContentPane().add(p1, "North");
getContentPane().add(temp, "Center");
p1.add(button);
p1.add(button2);
Listener myListener = new Listener();
temp.addMouseListener(myListener);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
private class Listener implements MouseListener, ActionListener{
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e) {
x_2 = e.getX();
y_2 = e.getY();
temp = new Ritmetoder(x, x_2, y, y_2);
vector.add(temp);
getContentPane().add(temp, "Center");
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void actionPerformed(ActionEvent ae){
}
}
}
//Third Class
package Projekt;
import java.awt.*;
import javax.swing.*;
public class Painting extends JPanel{
public int x;
public int y;
public int x_2;
public int y_2;
public Painting(int x1, int x2, int y1, int y2){
x = x1;
x_2 = x2;
y = y1;
y_2 = y2;
setBackground(Color.GRAY);
setPreferredSize(new Dimension(800, 800));
}
public void paintComponent(Graphics g){
g.drawLine(x, y, x_2, y_2);
}
}
Re: Problem with a simple drawing program
You are attempting to add components to a container after it has been realized (in this case, after the JFrame is visible). Either add it before, or call revalidate on the JPanel or validate on the JFrame. I am not sure what you full end goal is, but you might consider a redesign in which your panel is first added, and the mouse events create objects that are drawn by that panel (as opposed to the mouse events creating new Panels)
Re: Problem with a simple drawing program
I have just tried to follow the designs that we're using in our books from school.
I didn't understand what you ment by call revalidate on the JFrame. I'm a beginner at this and all I have to my help is my book...
Could you try to explain better? :)
Re: Problem with a simple drawing program
Quote:
Originally Posted by
Saiimon
I didn't understand what you ment by call revalidate on the JFrame. I'm a beginner at this and all I have to my help is my book...
Could you try to explain better? :)
I would recommend expanding your resources beyond just your book. In fact, the java API should be at the top of your list to become familiar with. The JFrame class has a method named validate (it is a method defined in Container - a parent class of JFrame and inherited by JFrame). To quote the API:
Quote:
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
You add things to a container after its displayed - call validate after you do so.
Re: Problem with a simple drawing program
So i should write it like this instead?? Because it seems to work :)
Code Java:
public void mouseReleased(MouseEvent e) {
x_2 = e.getX();
y_2 = e.getY();
temp = new Painting(x, x_2, y, y_2);
vector.add(temp);
getContentPane().add(temp, "Center");
validate();
repaint();
}
Re: Problem with a simple drawing program
Quote:
Originally Posted by
Saiimon
So i should write it like this instead?? Because it seems to work :)
Yes, presuming you do not wish to redesign as I recommended above.
Re: Problem with a simple drawing program
Sorry... I will be watching tutorials and searching for more info about; JFrame, Javax.swing and Java API. My problem is that the program I'm trying to make is going to be my final project in my programming course. And since we haven't made ANYTHING AT ALL in JFrame and such, the teacher will regard all minor design issues and such.
Therefore I just need to be able to make a program that is having some kind of graphics... And this will probably be enough :)
Thanks for your help :)
I am now able to continue with my project :)