Plotting Logarithmic and Exp graph in Java - java

I am doing my coursework on Java and I am stuck in doing the graphs for 3 functions. X-x^2, ln(x-1)-1 and e^x-3x.
I have already tried to set up JFrame with axis and even code for x-x^2 that plots graph for that function.
int x,y;
int ax,by;
String s="deba";
String se ="deba";
public Plotfunct1(){
setTitle("Function graph");
setSize(900,700);
setLayout(null);
setVisible(true);
setResizable(false);
x = 30;
y = 300;
}
#Override
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.drawString("Y", 310, 40);
g.drawString("Y'", 310, 600);
g.drawString("X", 30, 314);
g.drawString("X'", 600, 314);
if(x==300&&y==300){
g.drawString("Origin(0,0)", 310, 314);
}
g.drawLine(300, 30, 300, 600);
g.drawLine(30,300,600,300);
if(x>599||y<40){
x = 30;
y=300;
}
if(x<600&&y>30&&y!=600){
ax = x-300;
by = y-300;
ax++;
by = (int) (.095*(ax-Math.pow(ax,2)));
x=300+ax;
y=300-by;
}
g.fillOval(x, y, 3, 3);
repaint();
}
}
I need to plot the last 2 functions I have tried everything that I could come up with changing the Math.pow to Math.log but then I just get one line

Have you tried using Polygons?
I dont have experience with the Graphics class, but seems like fillOval() only draws circles, it does not apply for logarithmic functions.
Check this answer. You can add points to the graph using addPoint() on the Polygon, then draw the polygon on the Graphic using drawPolyline(). All you have to do is adjust the formula.

Related

How could I make my Snowmans arms wave using Graphics 2D?

So I am currently creating a drawing that creates a Snowman, as well as some other objects in the background. I got the drawing part down, it looks just as it should, but I wanted to add a little bit of movement within it. I used Graphics2D to draw all my shapes, lines, ellipses, etc., but I'm not sure how/if I can make the Snowmans arms wave back and forth with the drawLine method.
I provided the code for the entire Snowman here. It's very simple, as I said it just uses Graphics2D to draw everything. The arm1 and arm2 are near the bottom.
public class Snowman {
private int x;
private int y;
private int width;
private int height;
public Snowman(int x, int y, int width, int height) {
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public void draw(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
// the body of the snowman
g2.setColor(Color.WHITE);
g2.fillOval(x+125, y+40, 200, 200);
g2.fillOval(x+75, y+140, 300, 300);
g2.fillOval(x+25, y+290, 400, 400);
// the buttons
g2.setColor(Color.BLACK);
g2.fillOval(x+210, y+210, 40, 40);
g2.fillOval(x+210, y+270, 40, 40);
g2.fillOval(x+210, y+330, 40, 40);
// the eyes and mouth
g2.fillOval(x+190, y+98, 20, 20);
g2.fillOval(x+245, y+98, 20, 20);
g2.drawArc(x+180, y+120, 100, 50, 200, 140);
// arm1
g2.drawLine(x+75, y+270, x, y+90);
//arm2
g2.drawLine(x+375, y+270, x+450, y+90);
// the hat
g2.setColor(Color.BLACK);
g2.fillRect(x+177, y+40, 100, 30);
g2.fillRect(x+202, y, 50, 50);
}
}
Is there a way I can implement something to make them move? Or would I have to draw these another way?
you can add a method that adjusts the coordinates of the hand, and this method might contain some if statement so say if the hand reached specific x it will subtract from x to go to the other direction. And then call this method inside the draw function

paintComponent is stressing me out; Hangman programming project

Ok, so I have a programming project due in a few hours and I am close to finished. I can only paint certain parts of the hangman at a time, which is dependent on the amount of incorrect tries by the user. The teacher of my programming class requires the creation of the hangman drawing to be in a different class. The problem I'm having is sending over the number of incorrect tries to a class with the paintComponent.
if(updatedChallenge == challenge)
{
incorrectTries += 1;
}
challenge = updatedChallenge;
The challenge == updated challenge refers to the hidden word before the user's guess and the hidden word after the user's guess. If they are still equal that means the user made an incorrect choice. 1 gets added to the total number of incorrect tries.
Here is my other class:
class HangmanPicture extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(250, 300, 300, 300); //Platform
g.drawLine(275, 300, 275, 200); //Pole
g.setColor(Color.YELLOW);
g.drawLine(275, 200, 350, 200); //Rope
g.drawLine(350, 200, 350, 225); //Noose
g.setColor(Color.CYAN);
g.drawOval(350, 225, 15, 15); //Head
g.drawLine(350, 230, 350, 260); //Torso
g.drawLine(350, 240, 340, 230); //Left Arm
g.drawLine(350, 240, 360, 230); // Right Arm
g.drawLine(350, 260, 330, 280); // Left Leg
g.drawLine(350, 260, 390, 280); // Right Leg
}
}
I want to put an if statement where each body part gets added after each incorrect try, which requires me to send the number of incorrect tries to the class. Whenever I try to send the number, it always somehow interferes with paintComponent
Any help would be greatly appreciated, I'm desperate. Thanks in advanced!
Since you are extending the JPanel class. We can add extra methods and fields to the class and use them where HangmanPicture is being used.
HangmanPicture panel = new HangmanPicture();
...
if(updatedChallenge == challenge)
{
panel.addIncorrectAttempt();
}
challenge = updatedChallenge;
class HangmanPicture extends JPanel
{
private int incorrectAttempts = 0;
// Add one to counter
public void addIncorrectAttempt(){
incorrectAttempte++;
}
// Get how many times the player entered an incorrect number
public int getIncorrectAttempts(){
return incorrectAttempts;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
if( incorrectAttempts >= 1 ){
g.drawLine(250, 300, 300, 300); //Platform
}
if( incorrectAttempts >= 2 ){
g.drawLine(275, 300, 275, 200); //Pole
}
}
}

Bouncing multiple lines with fixed coordinates off walls in java graphics [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store the fixed coordinates and then increment the coordinates until they reache the edge of the screen.
Here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Name extends JPanel
{
int x, y, w, h, xdir, ydir;
public Name ()
{
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
g.drawLine(110+x, 300+y, 95+x, 400+y);
g.drawLine(110, 300, 123, 350);
g.drawLine(135, 300, 123, 350); //M
g.drawLine(135, 300, 150, 400);
g.drawOval (150, 300, 15, 15); // i
g.fillOval (150, 300, 15, 15);
g.drawLine (155, 320, 155, 400);
g.drawLine (172, 300, 172, 400); // k
g.drawLine (172, 350, 185, 300);
g.drawLine (172, 350, 185, 400);
g.drawLine (190, 360, 265, 360); // e
g.drawArc (190, 325, 75, 75, 360, 180);
g.drawArc (190, 330, 75, 75, 168, 180);
w = getSize ().width;
h = getSize ().height;
xdir = 1;
ydir = 1;
x = x + xdir;
y = y+ ydir;
if (x > w || x < 0)
{
xdir = -1 * xdir;
}
if (y > h || y < 0)
{
ydir = -1 * ydir;
}
repaint ();
MyLib.delay (25);
} // paint method
public static void main (String[] args)
{
JFrame frame = new JFrame ("NameBounce");
frame.getContentPane ().add (new Name ());
frame.setSize (500, 500);
frame.setVisible (true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
} // main method
} // Name class
I've tried storing the coordinates into array lists but had no luck.
I've been trying to avoid hard coding the entire program.
Use something like Path2D to define the shapes you want to draw (you can draw all the letters within a single Path2D)
Use a AffineTransform to translate the location of the Graphics and Graphics2D#draw to physically paint the Path2D
Use a Swing Timer to schedule a regular call back within which you can update the position of the Path2D and call repaint on the component to trigger an update
See
Concurrency in Swing
How to use Swing Timers
2D Graphics
for more details
Don't call Thread.sleep or any other blocking process within the paintComponent method (or from within the context of the Event Dispatching Thread) as this will prevent the EDT from updating the screen.
Make sure you are creating/updating your UI only from within the context of the EDT, see Initial Threads for more details

Centering 2D Shapes in Java

I'm a novice programmer trying to overlay two 2D circles on top of each other. Each circle has different dimensions and I understand that adding or subrtracting from xCenter and yCenter will move the shape on an axis, but how do I know with certainty they are centered? Unfortunately I am a novice without many tools under my belt, so the simplest information possible would be greatly appreciated. Thank you!
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RedCross extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
g.setColor(Color.BLUE);
g.fillOval(xCenter, yCenter, 40, 40);
g.setColor(Color.RED);
g.fillOval(xCenter, yCenter, 10, 10);
}
public static void main(String[] args)
{
JFrame window = new JFrame("Target");
window.setBounds(300, 300, 200, 200);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RedCross panel = new Target();
panel.setBackground(Color.WHITE);
Container c = window.getContentPane();
c.add(panel);
window.setVisible(true);
}
}
The center of an object is window/2 - self/2
In code:
int xPanel = getWidth();
int yPanel = getHeight();
int sizeCircleOne = 40;
int sizeCircleTwo = 10;
// Drawing circle 1
g.setColor(Color.BLUE);
g.fillOval(
xPanel/2 - sizeCircleOne/2,
yPanel/2 - sizeCircleOne/2,
sizeCircleOne,
sizeCircleOne
);
// Drawing circle 2
g.setColor(Color.RED);
g.fillOval(
xPanel/2 - sizeCircleTwo/2,
yPanel/2 - sizeCircleTwo/2,
sizeCircleTwo,
sizeCircleTwo
);
Let me know if it works.
Happy coding :) -Charlie
The method you use to center your circles is how one should center objects. Just take a point on half of the width, and half of the height, and that's your center. This is because the center of a circle has all points on the circle equidistant from the center point. That's the definition of a circle.

How to alternate speed using an array of integers for javax swing timer

I am attempting to simulate rain falling using fixed line lengths with randomly generated x coordinates. I have got the random x coordinates working but I want lines to have different falling speeds as well once they are repainted onto the window. I am using the javax swing timer and the java Random to generate integers to pass into my "speed" array as indexes. The speed however does not change. It remains the same and is way too fast.
public class rain extends JPanel implements ActionListener {
int i = 0;
int[] speed = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
double[] x = {10, 202, 330, 140, 250, 160, 470, 180, 290, 510};
double y1 = 10, y2 = 20;
double down = 1;
Random random = new Random();
//Timer t = new Timer(speed[i], this);
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D l = (Graphics2D) g;
Line2D line = new Line2D.Double(x[i], y1, x[i], y2);
l.setColor(Color.blue);
l.draw(line);
Timer t = new Timer(speed[i], this);
t.start();
}
public void actionPerformed(ActionEvent e) {
if (y2 < 380) {
y1 += down;
y2 += down;
}else{
y1 = 10;
y2 = 20;
i = random.nextInt(10);
}
repaint();
}
You start a new Timer each time your panel is repainted, and all the previous ones are still running. You should change your algorithm to use a unique timer, and compute the new y based on the previous one and on the current speed.
For example, the time would repaint every 40 milliseconds. If the speed is N, the new y would be the previous one + (N * down)

Categories

Resources