I am having challenges calling my graphics method in my main method so that it can paint a triangle using points from an array. When I don't use an array and just use regular numbers the painting works just fine, but the idea of the program is that the user enters values into the array to be used. Any suggestions on how to make the triangle paint? Here's the code: (I have all the proper imports)
public class Summative extends JFrame{
static int[] inpoot() {
Scanner input = new Scanner(System.in);
int[] numbers = new int[6];
System.out.println("Please enter 3 sets of coordinates:");
for (int i = 0; i < 6; i++) {
numbers[i] = input.nextInt();
}
return numbers;
}
static void outpoot(int [] numbers) {
double A = Math.sqrt (Math.pow ((numbers[4] - numbers[2]), 2) + Math.pow ((numbers[5] - numbers[3]), 2));
double B = Math.sqrt (Math.pow ((numbers[4] - numbers[0]), 2) + Math.pow ((numbers[5] - numbers[1]), 2));
double C = Math.sqrt (Math.pow ((numbers[2] - numbers[0]), 2) + Math.pow ((numbers[3] - numbers[1]), 2));
double s = (A + B + C) / 2;
double area = (Math.sqrt (s * (s - A) * (s - B) * (s - C)));
System.out.println ("The area of the triangle entered is : " + area + " units squared.");
}
public static void main(String[] args) {
int[] numbers = inpoot();
outpoot(numbers);
JFrame frame = new JFrame("Triangle");
frame.setVisible(true);
frame.setSize(new Dimension(500, 500));
JPanel panel = new myPanel();
frame.add(panel);
frame.validate();
frame.repaint();
}
}
And this is the graphics class:
public class myPanel extends JPanel{
public void paint (int [] numbers, Graphics g) {
super.paint (g);
g.setColor (Color.BLACK);
int[] xTri = {numbers[0], numbers[2], numbers[4]};
int[] yTri = {500 - numbers[1], 500 - numbers[3], 500 - numbers[5]};
g.fillPolygon (xTri, yTri, 3);
}
}
You need to supply some means to pass the information from you main method to your myPanel class. In your case, you could simply supply a custom constructor, something like...
public class myPanel extends JPanel{
private int[] xTri;
private int[] yTri;
public myPanel(int[] xTri, int[] yTri) {
this.xTri = xTri;
this.yTri = yTri;
}
public void paint (int [] numbers, Graphics g) {
super.paint (g);
g.setColor (Color.BLACK);
g.fillPolygon (xTri, yTri, xTri.length);
}
}
Observations...
Don't extend from JFrame, you're creating a new instance anyway which is just confusing the issue
Because of the way that Swing works, you need to make sure that you start you UI within the context of the Event Dispatching Thread. So, in your main method, you should use...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// Your UI code here
}
});
and build your UI within the run method
nothing is showing up at all..
I have tried moving Random rand = new Random() to outside of the loop, but it still doesnt work at all.
Nor does the frame exit on close.
public class myMain {
public static void main(String args[]) {
Frame frame = new Frame();
}
}
public class Frame extends JFrame {
public Frame(){
super("Fancy Triangle");
setSize(1024, 768);
myPanel panel = new myPanel();
add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
public class myPanel extends JPanel {
int x1 = 512;
int y1 = 109;
int x2 = 146;
int y2 = 654;
int x3 = 876;
int y3 = 654;
int x = 512;
int y = 382;
int dx, dy;
Random rand;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 50000; i++) {
g.drawLine(x, y, x, y);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
rand = new Random();
int random = 1 + rand.nextInt(3);
if (random == 1) {
dx = x - x1;
dy = y - y1;
} else if (random == 2) {
dx = x - x2;
dy = y - y2;
} else {
dx = x - x3;
dy = y - y3;
}
x = x - (dx / 2);
y = y - (dy / 2);
}
}
}
This:
Thread.sleep(300);
is not doing what you intend it to do. I think that you're trying to draw with a delay, but that's not what this does. Instead you're calling sleep on the Swing event thread puts the whole application to sleep, since the thread cannot do what it needs to do, including drawing the application and interacting with the user. Even worse, you're doing this within a painting method, a method that is required to be extremely fast since often the perceived responsiveness of a Swing application is determined by painting speed.
Instead use a Swing Timer (Swing Timer tutorial) to change the state of fields of the class, and then call repaint. Have your paintComponent use those changed fields to decide what to draw and where. Since a Sierpinski triangle is composed of dots, consider creating an ArrayList<Point>, getting rid of the for loop inside your painting method, and using the Swing Timer to replace this for loop. Within the Timer's ActionListener, place the semi-random points into the ArrayList and call repaint. Then within paintComponent, iterate through the ArrayList, drawing each point that it contains.
Alternatively, you could draw the points onto a BufferedImage in your Swing Timer and then simply have your paintComponent display the BufferedImage via g.drawImage(...) method call. This would likely be more efficient.
So I have a program that I'm trying to create for a class, and I need to create a country scene with a farm and a sun. The sun has to bounce up and down. The current problem I'm facing is that the sun keeps going down, and won't bounce up. Here is my code:
import javax.swing.*;
import java.awt.*;
/**
* Date: Oct 14, 2016
* Author:
* Description: Shows a country side with a farm and a sun bouncing up and down.
*/
public class WeAreInThePictures extends JFrame
{
ImageIcon sun, farm, bG; //assigns sun, farm and background to an image variable
static int x = 0, y = -50; //starting position of the sun
static int ySpeed = 10; //speed in y direction
static double delay = 1.0;
public WeAreInThePictures() {
super ("We Are In The Pictures!");
setSize (852, 480);
bG = new ImageIcon ("1.jpg");
sun = new ImageIcon ("sun.png");
farm = new ImageIcon ("farm.png");
setVisible (true);
}
public static void main(String[] args) {
new WeAreInThePictures ();
}
public void paint (Graphics g)
{
for (int i = 0; i < 1000; i++)
{
bG.paintIcon (this, g, 0, 0);
farm.paintIcon (this, g, 500, 50);
y = y + ySpeed;
if (ySpeed > 0)
{
sun.paintIcon (this, g, x, y);
for (int j = 0; j < 550000; j++)
{
delay = Math.pow (delay, 1);
}
}
else if (y > 50)
{
ySpeed = ySpeed - 1;
}
else if (y <= 0)
{
ySpeed = ySpeed - 1;
}
}
}
}
Can someone explain to me what is wrong, how i should fix it and why the problem is occurring?
Your problem is that you appear to have written this code without looking at any Swing Graphics tutorial or similar question on this site regarding Swing animation (why?). You're drawing directly in the JFrame, something that you shouldn't be doing, you've got object mutation code within a paint method, again something that you shouldn't be doing.
Instead do as you recommended in the tutorials, and most any other similar question on this site, in fact, search the problem before asking:
Draw in the paintComponent method of a JPanel that is displayed in a JFrame.
Use a Swing Timer for your animation loop
Change the speed's direction in the timer's ActionListener, not in a paintComponent method.
I'm making an Ultimate Tic-Tac-Toe game. If you aren't familiar with the rules, that's fine. But the board layout is just a 3x3 of Tic-Tac-Toe boards. I need an algorithm, for lack of a better term, to make it so I can make x and y wherever I want and it will draw it there correctly.
int width = 67; // Note: I've determined this variable means a lot to this "algorithm" because changing it changed the lines dramatically.
g.drawRect(0, 0, x, y); // Changing this made the boxes go to the center, so this works
g.drawLine(width, 0, width, y);
g.drawLine(width*2, 0, width*2, y);
g.drawLine(0, width, x, width);
g.drawLine(0, width*2, x, width*2);
The Class Constructor calls for the x and y to be input, so those are varying.
Specifically, I want this to work no matter what I make the x and y coordinates be.
The last 4 lines in the code make the 4 intersection bars (shaped like a very large #).
Making 1: This works fine.
Making 2: The Horizontal(--) lines work, not the Vertical(|).
Making 3: Third box doesn't even show up.
Also note that these above 3 tests are just 1 row of Tic-Tac-Toe boards.
I also understand that width can not be a single value (like it is now) but changing it makes the lines go far from where they're supposed to be so I don't know what to change it to.
Please refer to the Java Platform documentation for the Graphics class. Specifically the documentation for the drawLine() method, and also the drawRect() method (although you don't strictly need drawRect() to accomplish the task).
Based on your description, it sounds like you want to reuse the four drawLine() statements from your code sample to create each of the nine small grids as well as the one big grid.
Assuming your calling code looks something like this:
int totalWidth = 200;
// draw big grid
drawGrid(g, 0, 0, totalWidth);
// draw small grids
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
drawGrid(g, totalWidth / 3 * i, totalWidth / 3 * j, totalWidth / 3);
}
}
Here's what I would do:
private void drawGrid(Graphics g, int x, int y, int width) {
// draw box around grid (not necessary)
//g.drawRect(x, y, width, width);
// draw vertical grid lines
g.drawLine(width / 3 + x, y, width / 3 + x, y + width);
g.drawLine(width / 3 * 2 + x, y, width / 3 * 2 + x, y + width);
// draw horizontal grid lines
g.drawLine(x, width / 3 + y, x + width, width / 3 + y);
g.drawLine(x, width / 3 * 2 + y, x + width, width / 3 * 2 + y);
}
Note this doesn't put any padding around the small grids, so it looks like one huge grid. I imagine you'll want to add some padding to make it look right. You'd do that from the calling code where it loops to create the small grids.
Um. Besides the fact that it appears your asking us to do the work for you...
I would suggest not even using the Java Graphics to draw the shape of an x and to draw the shape of an o (it'll make your program all dirty, but you know.. whatever this is a tutorial). Rather, I would a series of panels JTextAreas. Give them a giant font, and make their 'isFocusable()' boolean to false. Then, I would add mouse listeners to each of them, so whenever the mouse clicks the JTextArea, it runs a method. I will write a method for you here, it's very simple. It will take one parameter, a JTextArea. This will be the area that was clicked, which can be easily be found out, because you'll be calling this method inside the mouseListener's mousePressed() method. Bare in mind, this will require a mouseListener for all 9 of your JTextAreas. This method also assumes you have created two booleans, one for the first player and one for the second. If it is Player1's turn, this method will find out, then change the turn to Player2's and vise versa. ****NOTE**** make sure you set the value of Player1 to true when you create it, or at least before this method is called.
public void makingMove(JTextArea jta) {
if (player1) {
jta.setText("x");
player1 = false;
player2 = true;
}
if (player2) {
jta.setText("o");
player1 = true;
player2 = false;
}
}
Hmm will this work? NO! what if you click a JTextArea that already has a letter in it? it'll replace it! We can't have that! so, now we need to change up our makingMove method, so we know we don't break any rules:
public void makingMove(JTextArea jta) {
if (jta.getText() == null) {
if (player1) {
jta.setText("x");
player1 = false;
player2 = true;
}
if (player2) {
jta.setText("o");
player1 = true;
player2 = false;
}
}
else {
JOptionPane.showMessageDialog(null, "You can't move there, someone already has!");
}
}
Done right? WRONG! Now we need to create a method that will check to see if anyone has won yet!
We will call this after our else statement in our makingMove() method. Like so:
.....
}
else {
JOptionPane.showMessageDialog(null, "You can't move there, someone already has!");
}
checkWinnerX(1, 2, 3, 4, 5, 6, 7, 8, 9);
checkWinnerY(1, 2, 3, 4, 5, 6, 7, 8, 9);
}
The checkWinnerX method will have to take 9 parameters, same with our checkWinnerO method. Each input object will be a JTextArea, so we can compare their input. This method assumes, your JTextAreas are named 1-9, counting like it would be a phone's dialpad. In addition, we have our checkWinnerX and checkWinnerO methods calling a method to clear everything, and reset the game, which you can assign to a button if you'd like, but I would definitely recommend having one.
*****NOTE******
I will not write the checkWinnerO method because it is the same as the checkWinnerX, but with all o's instead. and I'm sure you get the idea. AND this is an EXTREMELY inefficient way of handling this, but oh well.
public static void checkWinnerX(JTextArea 1, JTextArea 2, etc.. up to 9) {
if (1.equals("x") && 2.equals("x") && 3.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (4.equals("x") && 5.equals("x") && 6.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (7.equals("x") && 8.equals("x") && 9.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (1.equals("x") && 2.equals("x") && 3.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (1.equals("x") && 5.equals("x") && 9.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (7.equals("x") && 5.equals("x") && 3.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (1.equals("x") && 4.equals("x") && 7.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (2.equals("x") && 5.equals("x") && 8.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
if (3.equals("x") && 6.equals("x") && 9.equals("x")) {
JOptionPane.showMessageDialogue(null, "The winner is X!");
clear();
}
}
public static void clear() {
1.setText(null);
2.setText(null);
3.setText(null);
4.setText(null);
5.setText(null);
6.setText(null);
7.setText(null);
8.setText(null);
9.setText(null);
}
Now if you've got your mouseListeners set up correctly, and you use GridBagConstraints to correctly format your playing board. You pretty much have it done.
I could make this a thousand ways, using a thousand different methods and objects and blah, I chose this way because you specified you wanted an 'algorithm' for it. Now, you should be able to refer to this VERY broken down tutorial of a not-so-great way of making this game, and write your own code for it.
Hope this helps.
If I understand you correctly, you want a 3 x 3 grid of tic tac toe boards.
Here's a quick Swing application that draws a 3 x 3 grid of tic tac toe boards. 9 tic tac toe boards on 9 panels.
And here's the code. The drawing code is in the DrawingPanel class.
package com.ggl.testing;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TicTacToe implements Runnable {
private JFrame frame;
private List<DrawingPanel> drawingPanels;
public TicTacToe() {
this.drawingPanels = new ArrayList<>();
}
#Override
public void run() {
frame = new JFrame();
frame.setTitle("Ultimate Tic Tac Toe");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3, 3, 10, 10));
for (int i = 0; i < 9; i++) {
DrawingPanel drawingPanel = new DrawingPanel();
mainPanel.add(drawingPanel);
drawingPanels.add(drawingPanel);
}
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void exitProcedure() {
frame.dispose();
System.exit(0);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new TicTacToe());
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = -3774580797998095321L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(170, 170));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(5.0F));
Rectangle r = new Rectangle(0, 0, getWidth(), getHeight());
int sectionWidth = r.width / 3;
int sectionHeight = r.height / 3;
g2d.setColor(Color.BLACK);
g2d.drawLine(r.x, r.y + sectionHeight,
r.x + r.width, r.y + sectionHeight);
g2d.drawLine(r.x, r.y + sectionHeight + sectionHeight,
r.x + r.width, r.y + sectionHeight + sectionHeight);
g2d.drawLine(r.x + sectionWidth, r.y, r.x + sectionWidth,
r.y + r.height);
g2d.drawLine(r.x + sectionWidth + sectionWidth, r.y,
r.x + sectionWidth + sectionWidth, r.y + r.height);
}
}
}
Just started messing around with Swing for a class project GUI in Java. I'm trying to draw a game board, however, not a conventional one. I'm trying to draw one more like a parchessi board, so each board tile needs to have a specific location rather than a grid.
So far, I've run into this issue. In paint(), I'm trying to paint 5 rectangles, odd ones blue and empty, even ones red and filled in. However, instead of a nice checkered pattern, I get this:
Can anyone help me figure out why it's doing this?
Code:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Rectangles extends JPanel {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(800, 800);
f.add(new Rectangles());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
int x = 15;
int y = 15;
int w = 15;
int h = 15;
for(int i = 0; i < 5; i++){
if(i%2==0){
g.setColor(Color.RED);
g.fillRect (x, y, x+w, y+h);
}
else{
g.setColor(Color.BLUE);
g.drawRect (x, y, x+w, y+h);
}
x+=15;
System.out.println(Integer.toString(x) + ' ' + Integer.toString(y) + '|' + Integer.toString(w) + ' ' + Integer.toString(h));
}
}
}
Output from the Println statement(x,y,width,height):
30 15|15 15
45 15|15 15
60 15|15 15
75 15|15 15
90 15|15 15
It looked like there was overlap in the first image, so I modified the code and tried this:
for(int i = 0; i < 5; i++){
g.setColor(Color.BLUE);
g.drawRect (x, y, x+w, y+h);
x+=15;
}
Here's what happens with this code:
Why is there overlap? What causes this?
Also, does anyone know a good way to make an easily modifiable array of Rectangles? Or any good advice or tools for drawing that type of board?
Welcome to the reasons you should not break the paint chain...
Start by calling super.paint(g) as the first line of your paint method, before you do any custom painting.
A better solution would be to override paintComponent instead of paint, but still making sure you call super.paintComponent before you perform any custom painting...
Take a look at Performing Custom Painting and Painting in AWT and Swing for more details
Next, start reading the JavaDocs on Graphics#fillRect, you will see that the last two parameters represent the width and height, not the x/y position of the bottom corner
public class Rectangles extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 15;
int y = 15;
int w = 15;
int h = 15;
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
g.setColor(Color.RED);
g.fillRect(x, y, w, h);
} else {
g.setColor(Color.BLUE);
g.drawRect(x, y, w, h);
}
x += 15;
System.out.println(Integer.toString(x) + ' ' + Integer.toString(y) + '|' + Integer.toString(w) + ' ' + Integer.toString(h));
}
}
}