Problem with paintComponent...?
Hello !
Here is my Code:
LineDrawV2
Code :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public abstract class LineDrawV2 extends JFrame implements ActionListener{
public static int i = 0;
public static Double[] storeCoordinates;
public static JFrame frame1 = new JFrame();
private static final long serialVersionUID = 1L;
private static Graphics Graphics;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(LineDrawV2.class.getName()).log(Level.SEVERE, null, ex);
}
frame1.setSize(800, 600);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLocationRelativeTo(null);
frame1.setTitle("Line Draw 2");
//frame1.getContentPane().setLayout(null);
final JFileChooser martin = new JFileChooser();
martin.setAcceptAllFileFilterUsed(false);
martin.setFileFilter(new FileNameExtensionFilter("Text Files", "txt", "rtf"));
JMenuBar mb = new JMenuBar();
frame1.setJMenuBar(mb);
JMenu filemb = new JMenu("File");
mb.add(filemb);
JMenuItem importmb = new JMenuItem("Import");
filemb.add(importmb);
JMenuItem exitmb = new JMenuItem("Exit");
filemb.add(exitmb);
exitmb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
importmb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try {
martin.showOpenDialog(frame1);
File selectedFile = martin.getSelectedFile();
String sexy = selectedFile + "";
System.out.println(sexy);
File file = new File(sexy);
Scanner martin2 = new Scanner(file);
int numLines = 0;
i = 0;
while(martin2.hasNext()){
martin2.nextLine();
numLines++;
}
martin2 = new Scanner(file);
String[] lines = new String[numLines];
while(martin2.hasNext()){
lines[i] = martin2.nextLine();
System.out.println(lines[i]);
i++;
}
String active;
storeCoordinates = new Double[i*4];
int k = i;
int q1 = 0;
while(k>0){
active = lines[i-k];
int q = 0;
String morph = "";
while(q<active.length()){
if(active.charAt(q) != ','){
morph = morph + active.charAt(q);
}
if(q+1 == active.length() || active.charAt(q) == ','){
Double dedomraz = Double.valueOf(morph);
storeCoordinates[q1] = dedomraz;
morph = "";
q1++;
}
q++;
}
k--;
}
drawing();
} catch (FileNotFoundException ex) {
Logger.getLogger(LineDrawV2.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
frame1.setVisible(true);
}
public static void drawing(){
DrawingPanel panel = new DrawingPanel();
frame1.add(panel);
}
}
DrawingPanel
Code :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.lang.Math;
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g){
LineDrawV2 LDV2 = new LineDrawV2() {
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
super.paintComponents(g);
g.setColor(Color.BLACK);
g.drawLine(15, 15, 100, 100);
}
}
so now the problem is that g.drawLine doesn't appear anywhere. I found out if I put drawing(); out of the action listener the line will appear but I need the Line to be drawn after or in the action listener because in the future I will need some numbers from a file that will give the line coordinates (this is just a example line so you can help me just to make it appear...).
Thanks in advance :)
P.S. It's a messy code but I hope you don't mind...
Re: Problem with paintComponent...?
Problems:
- You shouldn't be creating a new LineDrawV2 object inside of DrawingPanel's paintComponent(...) method. Why do you feel that your program needs this?
- You know that code like this risks a nasty NPE error if you declared DrawingPanel inside of LineDrawV2.
- LineDrawV2 should not be abstract. I believe that you're trying to fix an error the wrong way by making it abstract. The correct solution is to have the class implement all necessary interface methods.
- LineDrawV2 shouldn't extend JFrame or implement any interfaces.
- You shouldn't have any static variables as that only confuses things and makes your program non-OOPs compliant.
- After adding DrawingPanel to the JFrame, the JFrame should be packed.
- You call setVisible(true) on the JFrame *after* adding all components, including LineDraw, not before.
- ... and many more problems.
Re: Problem with paintComponent...?
packing the frame really helped ! Thank You :)
and one more question
Quote:
Originally Posted by
Fubarable
Problems:
- You know that code like this risks a nasty NPE error if you declared DrawingPanel inside of LineDrawV2.
I'm confused, how can I add the DrawingPanel in the frame if I don't declare it ?
Re: Problem with paintComponent...?
I may have gotten that backwards: you may get an exception, perhaps stackoverflow exception if you define a new LineDrawV2 inside of DrawingPanel's paintComponent. Bottom line is you don't want to create components or add components in the paint or paintComponent method.