SRL Archive - General

Java muilti-thread to paint graphics.


rj

2013-05-14 21:27:47

I'm trying to draw text on the GUI for my java program but it's not working..

import java.awt.*;
//import java.awt.event.*;

import javax.swing.*;
//import javax.swing.event.*;

//import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.awt.event.WindowAdapter;
//import java.awt.event.WindowEvent;
//import java.awt.AWTException;

public class Size extends JPanel implements ActionListener {

public Size() {

JButton calc;

calc = new JButton("Calculate");

add(calc);

calc.setBounds(135, 200, 120, 40);

setPreferredSize(new Dimension(400, 300));
setLayout(null);

}

public static void main(String[] args) {
JFrame frame = new JFrame("calc");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Size());
frame.pack();
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

}
public void DrawStats(Graphics g) {

g.setFont(new Font(null, Font.PLAIN, 18));
g.setColor(Color.red);
g.drawString("string", 5, 5);

}

}

JPHamlett

2013-05-14 21:40:38

Any errors? Simply saying, it isn't working won't help anyone solve the issue.


You don't have a paint method.

Not sure if "DrawStats()" is a method that can override the paint method

rj

2013-05-14 21:41:38

From: JPHamlett
Any errors? Simply saying, it isn't working won't help anyone solve the issue.


You don't have a paint method.

Not sure if "DrawStats()" is a method that can override the paint method

No errors i just get the JPanel with the calculate button

JPHamlett

2013-05-14 21:44:39

Add something like this


public void paint(Graphics g){
drawStats(g);
}

rj

2013-05-14 21:45:50

From: JPHamlett
Add something like this


public void paint(Graphics g){
drawStats(g);
}

OO that worked but is it muilti-threading and updating in real time?

JPHamlett

2013-05-14 21:48:04

Multi I think.

rj

2013-05-14 21:54:39

From: JPHamlett
Multi I think.


Ok now it's not drawing the button and it's claiming "Calc" cannot be resolved to a variable (at e.getsource) but the code still runs

import java.awt.*;
//import java.awt.event.*;

import javax.swing.*;
//import javax.swing.event.*;

//import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.awt.event.WindowAdapter;
//import java.awt.event.WindowEvent;
//import java.awt.AWTException;

public class Size extends JPanel implements ActionListener {
double base;
int shoesize;
String race;

public Size() {

JButton calc;

calc = new JButton("Calculate");

calc.addActionListener(this);


add(calc);

calc.setBounds(135, 200, 120, 40);

setPreferredSize(new Dimension(400, 300));
setLayout(null);

}

public static void main(String[] args) {
JFrame frame = new JFrame("Size calc");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Size());
frame.pack();
frame.setVisible(true);
}

@Override

public void paint(Graphics g){
DrawStats(g);

}
public void actionPerformed(ActionEvent e) {

if (e.getSource() == calc) {


}
}
public void DrawStats(Graphics g) {

g.setFont(new Font(null, Font.PLAIN, 12));
g.setColor(Color.red);
g.drawString("Aprrox Size", 7, 7);

}
}

Brandon

2013-05-14 22:03:21

Calc is declared within scope of another function. The error simply states that the paint function cannot see the calc variable because it is not within global scope or local scope of the paint function. Calc is declared within the scope of the size function.

Also when dealing with swing, you "should" obey the single thread rule and all events must be done through EDT:


SwingUtilities.InvokeAndWait(new Runnable() { //There are two functions: InvokeAndWait and InvokeLater..
//Do Swing stuff here.. Update GUI..
//Set JFrame stuff/visibility, etc..
});

You don't have to but you should.