I have a program selection tool that i made. it opens a JFrame with 17 buttons, 15 of which are customizable, and they get their text from a .txt document located in the C: drive. when i click the assign button, it opens a JFileChooser to select a file to open when the button is clicked. You then select a button to change, and then type the text you want displayed by the button. After that the program rewrites the .txt file and updates the buttons. here is the code for updating:
public static void restart() {
start.assignButtonActions();
start.assignButtonText();
start.paint(graphics);
}
public void assignButtonActions() {
/**
* assign button actions
*/
for (int i = 0; i < buttonAction.length; i++) {
buttonAction[i] = io.readSpecificFromHD("C:\\ButtonActions.txt", i
+ 1 + actionButton.length);
}
}
public void assignButtonText() {
for (int i = 0; i < actionButton.length; i++) {
/**
* set button text
*/
actionButton[i].setText(io.readSpecificFromHD(
"C:\\ButtonActions.txt", i + 1));
}
}
public void paint(Graphics g) {
g.drawImage(getImage("files/background.png"), 0, 0, FRAMEWIDTH,
FRAMEHEIGHT, null);
refresh();
}
public void refresh() {
graphics.drawImage(getImage("files/background.png"), 0, 0, FRAMEWIDTH,
FRAMEHEIGHT, null);
for (int i = 0; i < actionButton.length; i++) {
actionButton[i].repaint();
}
assignButton.repaint();
helpButton.repaint();
}
Thats all the code that is required for this question i believe. The problem is, after the method restart() is called, the background is there, with a white square around the buttons, with it being white inside the square. not really a major problem, but really incredibly annoying and pretty unprofessional. At first i thought it was that the buttons were resizing after the background is painted, so i made it so that the refresh runs twice each time its called. didnt help one bit.
EDIT:
I fixed the problem. I took hovercraft's answer and modified what i learned a little bit. all i had to do was modify the restart() method to:
public static void restart() {
start.assignButtonActions();
start.assignButtonText();
start.repaint();
}
because the repaint(); repaint the whole component which was what hovercraft said. Thank you a ton everyone! hope this helps future questions.
You appear to be handling your Swing graphics incorrectly by calling paint(...) directly and trying to use a Graphics object outside of a JComponent's paintComponent(...) method. Don't do this, as all the Swing graphics tutorials will tell you (if you've not gone through some of them yet, you will want to do this soon). Instead do all graphics within a JComponent's (such as a JPanel's) paintComponent(...), call the super's method first, and use the Graphics object provided by the JVM in the paintComponent's method parameter.
Edit
Tutorial links:
The introductory tutorial is here: Lesson: Performing Custom Painting.
The advanced tutorial is here: Painting in AWT and Swing.
I'm thinking that you'll have to re-write most of your graphics code. Changes you should make:
Draw only in a JPanel or other JComponent-derived class, not in a JFrame or other top-level window.
Draw in your class's paintComponent(...) method.
Place an #Override annotation just above your paintComponent(...) method to be sure that you are in fact overriding the super method.
Call the super's paintComponent(...) as the first line (usually) of your paintComponent(...) override method.
Use the Graphics object passed into this method by the JVM.
Do not use a Graphics object obtained by calling getGraphics() on a component (with rare exceptions).
Do not give your class a Graphics field and try to store the Graphics object in it. The Graphics objects given by the JVM do not persist and will quickly become null or non-usable.
Do not call paint(...) or paintComponent(...) directly yourself (with rare exceptions -- and your current code does not qualify as one of the exceptions, trust me).
You will likely not need to call repaint() on your JButtons
Related
I'm creating a game where I need to draw some lines and dots. I have a general function called paintDot (check code below) and I want to call it in a different function. I don't know how to call it, any help?
public void paintDot (Graphics g, int x, int y)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(x,y,15,15);
}
This is the other function/method where I need to call the drawing function:
ATM the coordinates are just hardcoded so I know it's working correctly.
As you can see, I'm calling the paintDot method with bad arguements. Don't know what argument should be placed at Graphics g
private void gameWindow (int dif)
{
this.removeAll();
areaImage = new JPanel ();
//distance between points = 75
//point grid = 7*6
areaImage.setBounds(50,50,675,600);
areaImage.setBackground(Color.WHITE);
areaImage.setBorder(BorderFactory.createLineBorder(Color.black));
add(areaImage);
answer = new JTextField();
answer.setBounds(835,200,150,50);
answer.setBorder(BorderFactory.createLineBorder(Color.black));
answer.setHorizontalAlignment(JTextField.CENTER);
answer.setFont(new Font("Verdana", Font.BOLD, 20));
add(answer);
info= new JLabel ("Write your answer here:");
info.setBounds(830,155,250,50);
info.setFont(new Font("Verdana", Font.BOLD, 12));
add(info);
checkAnswer = new JButton ("Check Answer");
checkAnswer.setBounds(835,310,150,50);
checkAnswer.addActionListener(this);
add(checkAnswer);
next = new JButton ("Next");
next.setBounds(835,410,150,50);
next.addActionListener(this);
add(next);
end = new JButton ("End Game");
end.setBounds(835,510,150,50);
end.addActionListener(this);
add(end);
revalidate();
repaint();
int x = 75,y=75;
for(int num=0;num<6;num++)
{
for(int xx=0; x<7;xx++)
{
paintDot (areaImage,x,y); // here is the problem
x=x*2;
}
y=y*2;
}
}
I have a general function called paintDot (check code below) and I want to call it in a different function
You can't.
Painting can only be done in the paintComponent() method.
You should NEVER be invoking paintComponent() directly.
All painting code MUST be in the paintComponent() method.
If you want to paint 7 dots. Then that painting code MUST be in the paintComponent() method which means the looping code would be in the paintComponent() method and then you invoke the paintDot(...) method from withing the loop. The painting of the dots must be done EVERY time Swing determines the component needs to be repainted.
You have asked several questions on this topic and the answer is always the same. Read the tutorial link you have been given and follow the examples. The tutorial draws a square, but the concept would be similar for drawing 7 dots.
So once again, read the tutorial, download the code and play with the working example. Start by changing the tutorial code to draw 7 dots. Once you understand how that works, then you add the logic to your real code.
The tutorial link is give to you for a reason. If there is something you don't understand in the tutorial, then ask a question, but don't post code that looks nothing like the example from the tutorial and wonder why it doesn't work!
You have this for:
for(int xx=0; x<7;xx++)
where you have an index called xx and you try to do a cycle. The problem is that you test for x < 7 instead of xx < 7 and since x is greater than 7, you will never get into the for.
You could extend JPanel and overwrite the drawing functions, such as the paintComponent(Graphics g) (Thanks camickr) or paintAll(Graphics g) (I believe) method.
You might also want to add a JLabel with a BufferedImage using createGraphics()
Please note if you are trying to make a full-fledged game, you would need a game loop and other stuff, which is NOT fun without a library.
This is not an attempt in shameless self-promotion, it's a suggestion.
IF you need a game loop, canvases, multiple screens and stuff,
a library could be the way to go.
I made the library j2D to make 2D games.
#Override
public void run() {
while (running) {
currentState.update();
prepareGameImage();
// next line draws image(frame) to the screen
currentState.render(gameImage.getGraphics());
repaint();
}
// End game immediately when running becomes false.
System.exit(0);
}
private void prepareGameImage() {
if (gameImage == null) {
gameImage = createImage(gameWidth, gameHeight);
}
Graphics g = gameImage.getGraphics();
g.clearRect(0, 0, gameWidth, gameHeight);
}
this is a snippet of game loop. a little explanation from the book about how it works. In prepareGameImage() we prepare an off-screen image by creating and initializing the gameImage variable with a width of gameWidth and a height of gameHeight. (i dont get how this works --->) Next, on every frame, we clear this image using a rectangle of equal size to clear all images that have been drawn to the screen in the previous frame. This ensures that images from the previous frame do not bleed into the current frame. Every frame starts anew.
what i dont understand is last 2 lines of the snippet. value of gameImage.getGraphics(); gets stored inside of Graphics variableg. method g.clearRect(0, 0, gameWidth, gameHeight); should only affect variable g and should not affect value generated by gameImage.getGraphics(); could you explain how does the last 2 lines of code do the task - "images from the previous frame do not bleed into the current frame" :( :(
thanks
gameImage.getGraphics();
only passes the refference(does not make a copy) to the internal Graphics of the gameImage.
Lets say that gameImage is an instance of some class A that has a private variable of the type Graphics G.
And has a method for accessing that variable:
public Graphics getGraphics(){
return this.G;
}
as you can se... by calling the getGraphics, you only have a reference(pointer) to the graphics.
The "Graphics" element is something global to your program. It is managing all of your graphics, no matter where they are. But since it is this global thing, you can't just use a variable you define like, say, a String, but must get it from an existing object that has a reference to it. For example, every Image has a reference to the graphics object. The variable g you have is a reference to this element now and it can be used. This reference is then used in the next line to clear out the whole screen and previously created images so they do not bleed into the current frame.
(Note: This explaination may not be 100% accurate, but that's how it made me understand it in the first place.)
Whenever I drag to change the window size in my applet, the paint method appears to run again. What is really happening? Is the paint method just re-running itself? Is there a way I can make it do this automatically (without changing the window size)? The for loop does not seem to be working. I had a thread.sleep method in the code but it still didn't help. I will include the code for the paint method below.
// -----------------------------------------------------------------
// Paints the stick figures on the applet.
// -----------------------------------------------------------------
public void paint(Graphics page) {
// for (int f = 0; f < 6; f++) {
resize(400, 150);
for (int u = 0; u < stickdudes.length; u++)
stickdudes[u].draw(page);
// Delay.sleep(3000);
// }
}
to call the paint() method again and again the repaint() method is used both in AWT and Swing
From Painint in AWT and Swing: AWT Painting Guidelines,
Programs may trigger a future call to paint() by invoking repaint(), but shouldn't call paint() directly.
I have done a program that numerically solves a set of differential equations which describes how an "arbitrary" illness move in an isolated and constant population, it was a programming assignment from a class I took a while ago. What I've done to extend it is to add some graphical components that can pause, reset and "play" the simulation, as well as some components that allows me to change some constants in the equations.
All this was an exercise in programming as I find it to be fun and exciting and want to become better.
However, at the moment I'm stuck, what I want to do now is to make a very simple form of animation of it. I want to visualize the data I get for the number of infected, susceptibles and resistants in a grid as points. I managed to create the grid and have an idea of how to place the dots.
The problem I have is how to draw the dots as the program is working, I can draw one dot in the grid but only as the grid is created, that's it. I need to be able to create a dot at a specific place in the grid, this goes on until the number of dots reaches a finite number, say 30. At that points I want to have the first dot, the one the left, removed, all the dots shifted to the left and place the new dot at the furthest right of the grid, the whole thing is then repeated.
I think I will be able to figure it out with some help/hints about the paintComponent() method and whether I need to use repaint() method at all, I can't get my head around these for some reason. I've read through my course literature on Java, but despite the extensive sections where he explains most of the different graphical components he does not say that much about those methods, only that you don't call for the paintComponent() method, it is done automatically.
If there is something unclear let me know and I'll try to clarify it.
Thanks in advance.
//
Fox Mulder
I think I will be able to figure it out with some help/hints about the paintComponent() method and whether I need to use repaint() method at all, I can't get my head around these for some reason.
Basically, say you create a custom component by extending JPanel. When you #Override the paintComponent() method, it get implicitly called for you, so you never have to call it. So what ever you paint inside the method, gets drawn on your surface. For example
public class DrawingPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}
}
When you call repaint() you are basically causing the paintComponent method to be call implicitly. So to answer your question, Yes you will need to call it if you want to animate, as you will need to update some kind of variable (like the x and y) in the paintComponent() method, to see any change in the drawing.
You can see more at Performing Custom Painting
Not to handle the actual animation, you'll want to use a javax.swing.Timer. You can see more at How to use Swing Timers. Here's the basic construct
Timer ( int delayInMillis, ActionListener listener )
where delayInMillis is the time to delay between ticks(in this case animations) and the ActionListener listens for "ticks". Each tick, the actionPerformed of the ActionListener is called. There, you can put the code to update any variables you use for animation.
So for example you update the x and y, in the actionPerformed, then call repaint()
public class DrawingPanel extends JPanel {
int x = 50;
int y = 50;
public DrawingPanel() {
Timer timer = new Timer(40, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
x += 5;
y += 5;
repaint();
}
});
timer.start();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}
}
Now this was just a simple example. But in your case you want to animate a scatter plot. So what you can do is have a list of Points and in the actionPerformed you can add pull points from that list and push them into another list that is to be drawn. So say you have this
List<Point> originalPoints;
List<Point> pointsToDraw;
...
#Override
protected void paintComponent(Grapchics g) {
super.paintComponent(g);
for (Point point : pointsToDraw) {
g.fillOval(point.x - 5, point.y - 5, 10, 10);
}
}
Basically all the points in pointsToDraw list will be drawn. Initially it will be empty. And in the timer, you can add to the list, until the originalPoints list is exhausted. For example.
List<Point> originalPoints;
List<point> pointsToDraw;
private int currentIndex = 0;
public DrawingPanel(List<Point> originalPoints) {
this.originalPoints = originalPoints;
pointsToDraw = new ArrayList<>();
Timer timer = new Timer(40, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if (currentIndex == originalPoints.size() - 1) {
((Timer)e.getSource()).stop();
} else {
pointsToDraw.add(originalPoints.get(currentIndex));
currentIndex++;
}
repaint();
}
});
timer.start();
}
So basicall you just keep a current index. When the index reaches the size of the original list, you stop the timer. Otherwise you just pop from the originalPoints and push to the pointsToDraw. For every point you add the pointsToDraw, a repaint() is called, and there will be another point for the paintComponent to draw a circle with.
The END
UDPATE
I just reread your question, and I think I have have misunderstood it. If you want all the points drawn, then basically just have one list. And draw all the points initially. with each tick, just remove the first index, advance all the rest up an index, and add a new one to the end. Though this is the implementation of a LinkedList so you may just want to use that
I'm stuck with a problem of bringing a field to the front as it's drawing, has anyone countered a problem like this before?
In plain words, the problem is that I'm using NegativeMarginVerticalFieldManager from the Advanced UI samples, I use it to display a highlighted text under each panel button, the text should go in front of the content under the panel, but now it's going in the back of it.
So, does any one know how to make it appear in the front?
The order of paint is different. Could you override paint in your fields and check order of painting?
Unfortunately there z-order is connected to order how fields were added to manager and I don't know how to change it.
What you could do - override paint in manager and call paint for children in your needed order:
public class MyManager extends NegativeMarginVerticalFieldManager {
protected void paint(Graphics g) {
for (int i = 1; i < getFieldCount(); i++) {
paintChild(getField(i), g);
}
if (getFieldCount() > 0)
paintChild(getField(0), g);
}
}
This is hack - I'm painting first field after all other.