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?
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); } }