Reputation: 11
I'm working on a paint program for one of my classes and I'm stuck. This is a part of my code (separated into 3 Java classes). When I click on the button "Ligne", I want to be able to draw a line in the white rectangle. Sorry for the French comments.
//cree une fenetre
public class QUESTION
{
public static void main(String[] args)
{
Paint_GUI test2 = new Paint_GUI();
}
}
import java.awt.*;
import javax.swing.*;
//class contenant le code pour dessiner
public class Paint_Dessin extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.white);
g.setColor(Color.black);
}
public void TracerLigne()
{
System.out.println("LIGNE");
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Paint_GUI extends JFrame
{
//Panels contenant tout les bouton de mon interface
private JPanel panelBtn;
//Bar d'outil Btn
private JButton BtnTracerLigne;
//object Paint_Dessin
private Paint_Dessin espaceDessin = new Paint_Dessin();
public Paint_GUI()
{
final int WINDOW_WIDTH = 650;
final int WINDOW_HEIGHT = 450;
setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
setTitle("Paint v.2.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Appeler la methode qui construit la barre de BTN.
buildPanelBtn();
add(panelBtn, BorderLayout.NORTH);
add(espaceDessin, BorderLayout.CENTER);
// Afficher la fenetre.
setVisible(true);
}
private void buildPanelBtn()
{
BtnTracerLigne = new JButton("Ligne");
BtnTracerLigne.addActionListener(new LigneListener());
// Creer le panel.
panelBtn = new JPanel();
// Ajouter les composantes au label
panelBtn.add(BtnTracerLigne);
}
private class LigneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
espaceDessin.TracerLigne();
}
}
Upvotes: 1
Views: 2856
Reputation: 1924
you should create another class like GraphComponent for instance, that extends JComponent. You should also look at implementing MouseInputListener. I will not give you the solution, but it'a a good start ;).
Anyway, I see you're french so take a look at site-du-zero, it's full of very good tutorials (in french).
N.B: You shouldn't name you classes like QUESTION, put Question instead
EDIT : here another hints :
public class Paint_Dessin extends JComponent implements MouseInputListener{
private List<Point> startPoints = new ArrayList<Point>();
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Point p = new Point(x,y);
startPoints.add(p);
repaint();
}
protected void paintComponent(Graphics g) {
g.setColor(getForeground());
Graphics2D g2 = (Graphics2D) g;
for (Point p : startPoints)
p.draw(g2);
}
public class Point{
private int x,y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
void draw(Graphics2D g2) {
//do the drawing with the right shape you want
}
}
and see Shape. You should implements others method from the MouseInputListener, like mouseDragged,....
Upvotes: 0
Reputation: 285460
You're current code has a problem in that you're creating a new Paint_Dessin in the LigneListener class's actionPerformed method, and while this object is created from the same class as the Paint_Dessin object that is displayed in the JFrame, it is a completely different object and calling methods on it will have absolutely no effect on the Paint_Dessin that is displayed. What you need to do is to declare a private Paint_Dessin variable once in the Paint_GUI class, a class field, initialize this variable either at its declaration or in the Paint_GUI constructor, and then display this object in the GUI and also call methods on this GUI in the listener class. e.g.,
public class Paint_GUI extends JFrame
{
private JPanel panelBtn;
private JButton BtnTracerLigne;
private Paint_Dessin espaceDessin = new Paint_Dessin();
public Paint_GUI()
{
final int WINDOW_WIDTH = 650;
//... code deleted for sake of brevity
// Paint_Dessin espaceDessin = new Paint_Dessin();
add(espaceDessin, BorderLayout.CENTER);
// Afficher la fenetre.
setVisible(true);
}
private void buildPanelBtn()
{
// ....
}
private class LigneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Paint_Dessin tracerLigne = new Paint_Dessin();
// tracerLigne.TracerLigne();
espaceDessin.TracerLigne(); // call the method on the same object!
}
}
Upvotes: 1