Results 1 to 6 of 6
- 06-23-2012, 05:05 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Calling a method that paints something
Hello all. I am fairly new to Java and don't know much about using Java to paint using 2DGraphics. I have a fairly complex program in the works that implements a lot of painting. I am writing this program to learn more about painting and more about Java in general. However, I have run into a problem that prevents me from writing any further until I figure it out. I decided to simplify the problem into a simple example so you guys didnt have to sort through a bunch of code to figure out what the heck Im talking about.
Please look at the following code and then I will explain my problem:
Here is the first class entitled Board.
Here is the second class entitled Test:import java.awt.*;
import javax.swing.*;
public class Board extends JPanel{
public Board(){
setBackground(Color.LIGHT_GRAY);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawOval(20, 20, 500, 500);
}
}
Now what I want to do is call a method called paintOval in redTurn() that will paint over the oval in Board. Here is the code I want to run:import javax.swing.*;
import java.awt.*;
public class Test {
public static boolean isRedTurn = false;
public static boolean isBlackTurn = false;
public static void main(String[] args) {
new Test();
}
public Test() {
super();
JFrame gui = new JFrame();
gui.setTitle("Paint Program");
gui.setSize(683, 683);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
gui.setVisible(true);
Board panel = new Board();
Container pane = gui.getContentPane();
pane.setLayout(new GridLayout(1,1));
pane.add(panel);
redTurn();
}
public void redTurn() {
isRedTurn = true;
******* I want to call paintOval here*************
}
}
The error i get if I try to call paintOval() from within redTurn() is this: method paintOval in test cannot be applied to given typespublic void paintOval(Graphics g){
super.paintComponent(g);
if(Test.isRedTurn){
g.setColor(Color.red);
g.fillOval(20, 20, 500, 500);
}
}
required: java.awt.Graphics;
found: no arguments
So my questions are these: can I pass something into paintOval() to satisfy the method to paint properly in the same JPanel as Board? If so what is it?
Am I going about this completely wrong? Is there another way to paint something in Board's JPanel from within Test?
Thanks for your input guys, I appreciate it in advance!
-
Re: Calling a method that paints something
Some suggestions:
- You'll want to make all of your static variable instance variables. When you create a static variable or method, you cannot use it in an object-oriented way, causing your program to lose flexibility and power, and for this reason, you should use static anything only for very specific reasons, none of which apply here.
- Consider giving Board a boolean variable, redTurn, that its paintComponent will use to fill or not fill a red oval if desired.
- Then give Board a public void method called setRedTurn(boolean) that sets the redTurn boolean field and then calls repaint() on the Board instance. This will allow classes that hold Board object to change Board's state and let Board change its display.
- Get 90% of your code out of your main static method and into a proper OOPs class.
- Do read the painting in Swing tutorials to see how to do this correctly. You can find the basic tutorials here and the advanced tutorial here.
- When posting code here, wrap the code in [code] [/code] tags not [quote] [/quote] tags. Otherwise your code becomes unreadable.
- 06-25-2012, 03:02 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Re: Calling a method that paints something
Thanks for your quick and concise reply, Fubarable! Your suggestion of using the Redturn boolean with the setmethod worked like a charm. The links to the tutorials were also a big help.
-
Re: Calling a method that paints something
You're quite welcome -- glad it helped!
- 06-25-2012, 10:05 AM #5
Re: Calling a method that paints something
For your future reference: BB Code List - Java Programming Forum
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-26-2012, 07:37 AM #6
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
Re: Calling a method that paints something
Thanks guys for you help and links. I have made some progress on my program thanks to your help. However, I have run into another problem that, yet again, I cannot seem to get around. I have spent hours and tried as many different solutions as I can think of but to no avail. Seriously, I am out of ideas. I hoped to get a little bit more help from the experts here.
Let me explain the some of the aspects of my program to clarify before I hit you with the code. My program generates 576 ovals called holes that need to be able to be made red or black after I click on them. The red player and then the black player take turns When an oval is clicked, it needs to stay either red or black for the rest of the execution of the program. This is the issue I am having trouble with. I cant seem, to find a way get the oval to stay filled with the color I clicked it with. I would really appreciate suggestions on how to do this. If I wasnt clear enough about the problem please ask specific questions so that I can clarify better for you guys.
Here is the first Class named Board
And here is the second Class:Java Code:package BoardAttributes; import Game.*; import java.awt.*; import javax.swing.*; public class Board extends JPanel{ protected String name; protected boolean filled = false; protected boolean pylonStart; protected boolean pylonEnd; protected boolean redTurn = false; protected boolean blackTurn = false; protected boolean isRed = false; protected boolean isBlack = false; protected int placedHoleX; protected int placedHoleY; public boolean isFixedRed = false; public boolean isFixedBlack = false; public static void main(String[] args) { new Board(); } public Board(){ //constructor setBackground(Color.LIGHT_GRAY); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.drawLine(33, 33, 608, 33); g.drawLine(33, 608, 608, 608); g.setColor(Color.BLACK); g.drawLine(33, 33, 33, 608); g.drawLine(608, 33, 608, 608); //************************************************* int x = 15; int y = 15; final int r = 10; for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { g.setColor(Color.BLACK); g.drawOval(x, y, r, r); if(redTurn){ g.setColor(Color.red); g.fillOval(Test.ovalStartX, Test.ovalStartY , r, r); } if(blackTurn){ g.setColor(Color.BLACK); g.fillOval(Test.ovalStartX, Test.ovalStartY, r, r); } x += 25; }//end of inner for y += 25; x = 15; }//end of outer for //************************************************* } public void setRedDraw(){ blackTurn = false; redTurn = true; repaint(); } public void setBlackDraw(){ redTurn = false; blackTurn = true; repaint(); } }
Thanks so much for your help in advance!Java Code:package Game; import BoardAttributes.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class Test extends JPanel { private JLabel statusbar; private int x = 0; private int y = 0; public boolean isRedTurn = false; public boolean isBlackTurn = false; public int clickedHole; public int centerOfHoleX; public int centerOfHoleY; public static int ovalStartX; public static int ovalStartY; public boolean wasNotValidHole = false; public static void main(String[] args) { new Test(); } public Test(){ super(); JFrame gui = new JFrame(); gui.setTitle("Game"); gui.setSize(683, 683); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setVisible(true); Board panel = new Board(); Container pane = gui.getContentPane(); pane.setLayout(new GridLayout(1,1)); pane.add(panel); statusbar = new JLabel("Nothings Happened Yet"); panel.add(statusbar); //statusbar.setVerticalAlignment(JLabel.SOUTH); Handlerclass handler = new Handlerclass(); panel.addMouseListener(handler); int finish = 0; //this is just a temporary variable to keep the following loop going while(finish == 0){ redTurn(panel); x = 0; y = 0; blackTurn(panel); x = 0; y = 0; } } public class Handlerclass implements MouseListener { @Override public void mouseClicked(MouseEvent event){ x = event.getX(); y = event.getY(); statusbar.setText(String.format("You clicked at %d, %d", event.getX(), event.getY())); } @Override public void mousePressed(MouseEvent event){ } @Override public void mouseReleased(MouseEvent event){ } @Override public void mouseEntered(MouseEvent event){ } @Override public void mouseExited(MouseEvent event){ } } public void redTurn(Board panel){ isRedTurn = true; while(x==0 && y==0){ statusbar.setText("Click the hole you would like to place a pin in, Red."); } testLocationRed(panel); } public void blackTurn(Board panel){ isBlackTurn = true; while(x==0 && y==0){ statusbar.setText("Click the hole you would like to place a pin in, Black."); } testLocationBlack(panel); } public void testLocationRed(Board panel){ int xmin = 15; int xmax = 25; int ymin = 15; int ymax = 25; for (int j = 0; j < 25; j++) { for (int i = 0; i < 25; i++) { if((x >= xmin && x <= xmax) && (y >= ymin && y <= ymax)){ clickedHole = i*j; centerOfHoleX = xmin + 5; centerOfHoleY = ymin + 5; ovalStartX = xmin; ovalStartY = ymin; panel.setRedDraw(); break; } xmin += 25; xmax += 25; } xmin = 15; xmax = 25; ymin += 25; ymax += 25; } } public void testLocationBlack(Board panel){ int xmin = 15; int xmax = 25; int ymin = 15; int ymax = 25; for (int j = 0; j < 25; j++) { for (int i = 0; i < 25; i++) { if((x >= xmin && x <= xmax) && (y >= ymin && y <= ymax)){ clickedHole = i*j; centerOfHoleX = xmin + 5; centerOfHoleY = ymin + 5; ovalStartX = xmin; ovalStartY = ymin; panel.setBlackDraw(); break; } xmin += 25; xmax += 25; } xmin = 15; xmax = 25; ymin += 25; ymax += 25; } } }
I am very new to programming Java; Ive only take one class. And my class only learned about Objects for about a week. That; why I may be improperly using code or using I poorly. I know my whole program is based on objects and their manipulation so I have little choice but to wing it. So please bear with me!
Any other suggestions are welcome.
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
Selecting a JMenu paints over the JPanel on the content pane
By Swingset in forum AWT / SwingReplies: 3Last Post: 01-05-2008, 11:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks