I'm very new to Java and programming in general and I'm having a bit of trouble with my homework assignment.
We're supposed to give a variation of 4 different shapes, 10 shape in each picture with variation of at least 20, also variation of at least 20 different colors and variation of position.
I've made classes for each of my 5 shapes. But I somehow don't know how to add them to my ArrayList randomShape so that they can be called in the paintComponent section.
Also I'm having trouble coming up with the code the positions :(
Its a bit frustrating that I've hit a road block of thoughts.
Thank you for any help/advice you give me
It's very much appreciated
Here is my code that I have so far
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Cara extends JPanel implements ActionListener {
Random random = new Random();
public static final int MAX_AMOUNT_OF_SHAPES = 50,AMOUNT_OF_DISTINCT_SHAPES=5, AMOUNT_OF_DISTINCT_COLORS = 20, SIZE_MAX_X_COORDINATE = 400, SIZE_MAX_Y_COORDINATE = 300;;
public static ArrayList<RandomShape> randomShape = new ArrayList<RandomShape>();
int x = 0;
int xMax = 400;
int y = 0;
int yMax = 300;
int width = 0;
int height = 0;
int arcWidth = 0;
int arcHeight = 0;
int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B = (int)(Math.random()*256);
Color randomColor = new Color(R,G,B);
public Cara(){
setPreferredSize( new Dimension(400,300));
}
abstract class RandomShape {
protected Color color;
protected int x,y;
abstract void draw(Graphics g);
}
public class Circle extends RandomShape{
#Override
public void draw(Graphics g) {
g.drawOval(x,y,width,width);
g.fillOval(x,y,width,width);
g.setColor(randomColor);
}
//constructor for random position
Circle (){
for ( int x; x < xMax; x++){
}
for ( int y; y < yMax; y++){
}
}
}
public class Rectangle extends RandomShape{
#Override
public void draw( Graphics g){
g.drawRect(x,y,width,height);
g.fillRect(x,y,width,height);
g.setColor(randomColor);
}
//constructor for random position
}
public class RoundRectangle extends RandomShape{
#Override
public void draw(Graphics g){
g.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
g.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
g.setColor(randomColor);
}
//constructor for random position
}
public class Oval extends RandomShape{
#Override
public void draw(Graphics g){
g.drawOval(x,y,width,height);
g.fillOval(x,y,width,height);
g.setColor(randomColor);
}
//constructor for position
}
public class Square extends RandomShape{
Square square = new Square();
#Override
public void draw(Graphics g){
g.drawRect(x,y,width,width); //because is a rectangle with sides of equal size
g.fillRect(x,y,width,width);
g.setColor(randomColor);
}
//constructor for position
}
protected void paintComponent(Graphics g) {
//clear the background
super.paintComponent(g);
//draw all shapes
for ( RandomShape rs: randomShape){
rs.draw(g);
}
}
public void actionPerformed (ActionEvent e){
regenerate();
repaint();
}
private void regenerate() {
//clear the shapes list
randomShape.clear();
// create random shapes
RandomShape shape = null;
for (int i = 0; i < 10 + random.nextInt(MAX_AMOUNT_OF_SHAPES); i++){
int randomInt = random.nextInt(AMOUNT_OF_DISTINCT_SHAPES);
switch (randomInt) {
case 0: shape = new Oval(400,300);
break;
case 1: shape = new Circle(400,300);
break;
case 2: shape = new Rectangle(400,300);
break;
case 3: shape = new Square(400,300);
break;
case 4: shape = new RoundRectangle(400,300);
break;
}
}
//random position
RandomShape position = null;
for (int i = 0; i < (MAX_SIZE_X_COORDINATE*MAX_SIZE_Y_COORDINATE) ; i++){
int randomIntpos = random.nextInt();
}
}
public static void main(String[] args) {
final Cara cara = new Cara();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
final JFrame frame = new JFrame("Computer Assisted Random Artist");
JButton button = new JButton("regenerate");
button.addActionListener(cara);
frame.add(button, BorderLayout.SOUTH);
frame.pack();
cara.regenerate();
frame.setVisible(true);
}
});
}
}
public class Cara extends JPanel implements ActionListener {
private static ArrayList<RandomShape> randomShape;
public Cara() {
randomShape = new ArrayList<RandomShape>();
}
}
private void regenerate() {
//clear the shapes list
randomShape.clear();
// create random shapes
RandomShape shape = null;
int randomInt;
for(int i = 0; i < 10 + random.nextInt(MAX_AMOUNT_OF_SHAPES); i++) {
randomInt = random.nextInt(AMOUNT_OF_DISTINCT_SHAPES);
switch (randomInt) {
case 0:
shape = new Oval(400,300);
break;
case 1:
shape = new Circle(400,300);
break;
case 2:
shape = new Rectangle(400,300);
break;
case 3:
shape = new Square(400,300);
break;
case 4:
shape = new RoundRectangle(400,300);
break;
}
randomShape.add(shape);
}
//random position
RandomShape position = null;
int randomIntpos;
for (int i = 0; i < (MAX_SIZE_X_COORDINATE*MAX_SIZE_Y_COORDINATE) ; i++){
randomIntpos = random.nextInt();
}
}
public class RoundRectangle extends RandomShape {
private int x;
private int y;
//constructor for random position
public RoundRectangle(int x, int y) {
this.x = x
this.y = y
}
#Override
public void draw(Graphics g){
g.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
g.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
g.setColor(randomColor);
}
}
Related
So my university uses the book Java programming from problem analysis to program design by DS Malik(ISBN: 978-1-111-53053-2) and in one of the examples I copied the code word for word. It runs but doesn't work the way its supposed too. What you're supposed to do is be able to click and drag circles that populate the screen in an applet. If anyone is interested in the programming example its 12-10. I have provided the code from eclipse. Any and all help is appreciated.
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
public class FreeDrawApplet extends JApplet implements MouseMotionListener
{
//instance variables
ColorCircle[] myGraph;
final int NUM_CIRCLES = 7;
final int WIDTH = 400;
final int HEIGHT = 400;
public class ColorCircle
{
private int x;
private int y;
public void setx(int iNewX)
{
x = iNewX;
}
public void sety(int iNewY)
{
x = iNewY;
}
public void paint (Graphics g)
{
g.fillOval(x-10, y-10, 20, 20);
}
public boolean selected(int iXcoord, int iYcoord)
{
if ((iXcoord >= x-10) && (iXcoord <= x+10) && (iYcoord >= y-10) && (iYcoord <= y+10))
return true;
else
return false;
}
}//end of public class circle
public void init ()
{
addMouseMotionListener(this);
myGraph = new ColorCircle[NUM_CIRCLES];
for(int i = 0; i < NUM_CIRCLES; i++)
{
ColorCircle myVertex = new ColorCircle();
myVertex.setx((int)(Math.random() * (WIDTH-50)));
myVertex.sety((int)(Math.random() * (HEIGHT-100)));
myGraph[i] = myVertex;
}
JOptionPane.showMessageDialog(null, "Try to drag any one of the colored circles ", "Information", JOptionPane.PLAIN_MESSAGE);
}//end of method
public void paint(Graphics g)
{
Color[] myColor = {Color.black, Color.red, Color.blue, Color.green, Color.cyan, Color.orange, Color.yellow};
if(NUM_CIRCLES > 0)
for(int i =0; i < NUM_CIRCLES; i++)
{
g.setColor(myColor[i]);
myGraph[i].paint(g);
}
}//end of paint method
public void mouseDragged(MouseEvent event)
{
int iX = event.getX();
int iY = event.getY();
for(int i = 0; i < NUM_CIRCLES; i++)
{
if(myGraph[i].selected(iX, iY))
{
myGraph[i].setx(iX);
myGraph[i].sety(iY);
break;
}
}
repaint();
}
public void mouseMoved(MouseEvent p1)
{
}
}
I am working on homework for class, and its late because I can't seem to understand the material despite all the research that I am doing. I am a beginner and do not know much in the way of java. Also, this is my first post so please be forgiving when you are reading this.
I am building on source code from my textbook, which I updated recently for past homework, but now I am trying to generate a class that draws multiple squares and moves those objects independently and at different speeds. They will all need to rebound off the walls as well. I followed the instructions and created two arrays that will hold the random x and y values between 1 and 10. However, I struggle with arrays and I am sure that I am not doing it correctly. So, I would love some feedback to see if I have it set up correctly.
I have a the jpanel pulling up and drawing, and as long as there is 1 square it is working fine bouncing off the walls, but things change when I draw more than one. The do not move independently and they also share the same speed. Some even disappear from time to time. This has really thrown me off. I appreciate any help!
In short, I am trying to paint new squares that all travel in different directions and at different speeds. Per the instructions we are suppose create and use a two arrays that handle the x and y values.
Here is what I have so far:
public class DotsPanel extends JPanel
{
private int delay = 15;
private final int SIZE = 7, IMAGE_SIZE = 3; // radius of each dot
private Timer timer;
private int x, y, i;
private ArrayList<Point> pointList;
static int [] xarray = new int [1000];
static int [] yarray = new int [1000];
Random rand = new Random();
//-----------------------------------------------------------------
// Constructor: Sets up this panel to listen for mouse events.
//-----------------------------------------------------------------
public DotsPanel()
{
pointList = new ArrayList<Point>();
int [] xarray = new int [1000];
int [] yarray = new int [1000];
timer = new Timer(delay, new ReboundListener());
addMouseListener (new DotsListener());
addMouseMotionListener (new DotsListener());
setBackground(Color.gray);
setPreferredSize(new Dimension(700, 500));
for(int i = 0; i < xarray.length; i++)
{
xarray[i] = rand.nextInt(7);
yarray[i] = rand.nextInt(7);
}
timer.start();
}
//-----------------------------------------------------------------
// Draws all of the dots stored in the list.
//-----------------------------------------------------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.BLUE);
for (Point spot : pointList)
{
page.fillRect(spot.x-SIZE, spot.y-SIZE, 25, 25);
page.drawString("Count: " + pointList.size(), 5, 15);
}
}
//*****************************************************************
// Represents the listener for mouse events.
//*****************************************************************
private class DotsListener implements MouseListener, MouseMotionListener
{
//--------------------------------------------------------------
// Adds the current point to the list of points and redraws
// the panel whenever the mouse button is pressed.
//--------------------------------------------------------------
public void mousePressed(MouseEvent event)
{
pointList.add(event.getPoint());
repaint();
}
public void mouseDragged(MouseEvent event)
{
// initially I had two xarray and yarray in here just like in
// mouseClicked
// but it did not change anything when removed
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void mouseClicked(MouseEvent event)
{
xarray[i] = rand.nextInt(7);
yarray[i] = rand.nextInt(7);
}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseMoved(MouseEvent e) {}
}
private class ReboundListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the position of the image and possibly the direction
// of movement whenever the timer fires an action event.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
for (Point spot : pointList)
{
spot.x += xarray[i];
spot.y += yarray[i];
if (spot.x <= 0 || spot.x >= 700)
xarray[i] = xarray[i] * -1;
if (spot.y <= 0 || spot.y >= 500)
yarray[i] = yarray[i] * -1;
repaint();
}
}
}
}
However, I struggle with arrays and I am sure that I am not doing it correctly.
I wouldn't use Arrays.
Instead, have a Ball object manage its own state. Then you can have different color, speed, size etc for each Ball. Then when the Timer fires you just calculate the new position and repaint the Ball.
Here is an example to get you started:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class BallAnimation4
{
private static void createAndShowUI()
{
BallPanel panel = new BallPanel();
JFrame frame = new JFrame("BallAnimation4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setSize(800, 600);
frame.setLocationRelativeTo( null );
//frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible( true );
panel.addBalls(5);
panel.startAnimation();
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
class BallPanel extends JPanel implements ActionListener
{
private ArrayList<Ball> balls = new ArrayList<Ball>();
public BallPanel()
{
setLayout( null );
setBackground( Color.BLACK );
}
public void addBalls(int ballCount)
{
Random random = new Random();
for (int i = 0; i < ballCount; i++)
{
Ball ball = new Ball();
ball.setRandomColor(true);
ball.setLocation(random.nextInt(getWidth()), random.nextInt(getHeight()));
ball.setMoveRate(32, 32, 1, 1, true);
// ball.setMoveRate(16, 16, 1, 1, true);
ball.setSize(32, 32);
balls.add( ball );
}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for (Ball ball: balls)
{
ball.draw(g);
}
}
public void startAnimation()
{
Timer timer = new Timer(75, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
move();
repaint();
}
private void move()
{
for (Ball ball : balls)
{
ball.move(this);
}
}
class Ball
{
public Color color = Color.BLACK;
public int x = 0;
public int y = 0;
public int width = 1;
public int height = 1;
private int moveX = 1;
private int moveY = 1;
private int directionX = 1;
private int directionY = 1;
private int xScale = moveX;
private int yScale = moveY;
private boolean randomMove = false;
private boolean randomColor = false;
private Random myRand = null;
public Ball()
{
myRand = new Random();
setRandomColor(randomColor);
}
public void move(JPanel parent)
{
int iRight = parent.getSize().width;
int iBottom = parent.getSize().height;
x += 5 + (xScale * directionX);
y += 5 + (yScale * directionY);
if (x <= 0)
{
x = 0;
directionX *= (-1);
xScale = randomMove ? myRand.nextInt(moveX) : moveX;
if (randomColor) setRandomColor(randomColor);
}
if (x >= iRight - width)
{
x = iRight - width;
directionX *= (-1);
xScale = randomMove ? myRand.nextInt(moveX) : moveX;
if (randomColor) setRandomColor(randomColor);
}
if (y <= 0)
{
y = 0;
directionY *= (-1);
yScale = randomMove ? myRand.nextInt(moveY) : moveY;
if (randomColor) setRandomColor(randomColor);
}
if (y >= iBottom - height)
{
y = iBottom - height;
directionY *= (-1);
yScale = randomMove ? myRand.nextInt(moveY) : moveY;
if (randomColor) setRandomColor(randomColor);
}
}
public void draw(Graphics g)
{
g.setColor(color);
g.fillOval(x, y, width, height);
}
public void setColor(Color c)
{
color = c;
}
public void setLocation(int x, int y)
{
this.x = x;
this.y = y;
}
public void setMoveRate(int xMove, int yMove, int xDir, int yDir, boolean randMove)
{
this.moveX = xMove;
this.moveY = yMove;
directionX = xDir;
directionY = yDir;
randomMove = randMove;
}
public void setRandomColor(boolean randomColor)
{
this.randomColor = randomColor;
switch (myRand.nextInt(3))
{
case 0: color = Color.BLUE;
break;
case 1: color = Color.GREEN;
break;
case 2: color = Color.RED;
break;
default: color = Color.BLACK;
break;
}
}
public void setSize(int width, int height)
{
this.width = width;
this.height = height;
}
}
}
Since your Arrays only contain the Point you want to paint you don't have any information about the speed each point should be moved at. The best you could do is create a random amount each point should be moved each time its location is changed. This would give erratic movement as each time you move a point the distance would be random.
If you want more constant speed then you would need to create a second Array to contain the distance each point should move every time.
This starts to get messy creating a new Array every time you want a new property to be unique for the object you want to paint. That is why the approach to create a custom Object with multiple properties is easier to manage.
I made a program to display interference patterns for light waves. I did this by using the paint method on a JPanel to draw 2 sources and then drawing concentric circles around them. This would be double slit interference, so I allowed one of the sources to move around to experiment with the slit width.
The problem is that when I run this, my computer says it is using 80% of my CPU! There's really not much to it. Circle in the middle, circles around it, and it moves. My code follows.
main class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class main {
static final int UP = -1;
static final int DOWN = 1;
static final int RIGHT = 1;
static final int LEFT = -1;
static final int NOMOVEMENT = 0;
static int verticalMovement = NOMOVEMENT;
static int horizontalMovement = NOMOVEMENT;
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point s1 = new Point((int)(screenSize.getWidth()/3), 50);
Point s2 = new Point((int)(2*screenSize.getWidth()/3), 50);
JFrame frame = new JFrame("Physics Frame");
frame.setPreferredSize(screenSize);
PhysicsPane pane = new PhysicsPane(screenSize, 15, s1, s2);
pane.setPreferredSize(screenSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
Timer time = new Timer(1000 / 20, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
switch (verticalMovement){
case UP:
s2.changeY(UP);
break;
case DOWN:
s2.changeY(DOWN);
break;
default:
break;
}
switch (horizontalMovement){
case RIGHT:
s2.changeX(RIGHT);
break;
case LEFT:
s2.changeX(LEFT);
break;
default:
break;
}
pane.repaint();
}
});
frame.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==37){
horizontalMovement = LEFT;
} else if (e.getKeyCode()==38){
verticalMovement = UP;
} else if (e.getKeyCode()==39){
horizontalMovement = RIGHT;
} else if (e.getKeyCode()==40){
verticalMovement = DOWN;
}
if(e.getKeyChar()=='a'){
pane.setWaveLength(2);
}
if(e.getKeyChar()=='s'){
pane.setWaveLength(-2);
}
}
#Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()){
case 37:
horizontalMovement = NOMOVEMENT;
break;
case 38:
verticalMovement = NOMOVEMENT;
break;
case 39:
horizontalMovement = NOMOVEMENT;
break;
case 40:
verticalMovement = NOMOVEMENT;
break;
}
}
});
frame.setVisible(true);
time.start();
}
}
Panel class. If there's an inefficiency with the drawing method, it would be here.
import javax.swing.*;
import java.awt.*;
public class PhysicsPane extends JPanel {
private Dimension size;
private Point[] pointArray = new Point[2];
private double waveLength;
public PhysicsPane(Dimension size, double wavelength, Point source1, Point source2) {
this.size = size;
pointArray[0] = source1;
pointArray[1] = source2;
setPreferredSize(size);
this.waveLength = wavelength;
}
#Override
public void paintComponent(Graphics g){
for (int i = 0; i < 2; i++) {
g.setColor(Color.black);
double x = this.pointArray[i].getX();
double y = this.pointArray[i].getY();
g.fillOval((int)x, (int)y, 2, 2);
for (int j = (int)waveLength; j < 1500; j+=waveLength) {
g.setColor(Color.red);
g.drawOval((int)x-(j/2+1), (int)y-(j/2+1), 2 + j, 2 + j);
}
}
}
public void setWaveLength(double increment){
this.waveLength+=increment;
}
}
Point Class: Really just puts x and y coordinates into one construct. Not important particularly
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void changeX(double dX){
this.x+=dX;
}
public void changeY(double dY){
this.y+=dY;
}
}
So what is wrong with my program? Why is such a simple animation consuming so much of my processor?
UPDATED CODE SECTION:
#Override
public void paintComponent(Graphics g){
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bi.getGraphics();
for (int i = 0; i < 2; i++) {
graphics.setColor(Color.black);
double x = this.pointArray[i].getX();
double y = this.pointArray[i].getY();
graphics.fillOval((int)x, (int)y, 2, 2);
for (int j = (int)waveLength; j < 1500; j+=waveLength) {
graphics.setColor(Color.red);
graphics.drawOval((int)x-(j/2+1), (int)y-(j/2+1), 2 + j, 2 + j);
}
}
g.drawImage(bi, 0, 0, new ImageObserver() {
#Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
return false;
}
});
}
The improvement that I recommend is removal of unnecessary tasks being performed, notably how your code updates the pane being drawn on, even when there aren't changes.
The following update reduced CPU usage from 12% to 0% (static frame):
Timer time = new Timer(1000 / 20, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
boolean refresh = false;
switch (verticalMovement) {
case UP:
s2.changeY(UP);
refresh = true;
break;
case DOWN:
s2.changeY(DOWN);
refresh = true;
break;
default:
break;
}
switch (horizontalMovement) {
case RIGHT:
s2.changeX(RIGHT);
refresh = true;
break;
case LEFT:
s2.changeX(LEFT);
refresh = true;
break;
default:
break;
}
if (refresh == true) {
pane.repaint();
}
}
});
I am trying to create a program that uses a JComboBox containing specific shapes (Circle, Square, Oval, Rectangle). After the user clicks on a specified shape, the Panel will display 20 of that shape in random dimensions and locations.
I am having trouble on how to make the shapes have random dimensions and locations. Here is my code so far. Any advice or sources to look at would be appreciated.
Thank you.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
public class HW1b extends JFrame
{
public HW1b()
{
super("Shapes");
final ComboPanel comboPanel = new ComboPanel();
String[] shapeItems = {"Circle", "Square", "Oval", "Rectangle"};
JComboBox shapeBox = new JComboBox<String>(shapeItems);
shapeBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
if (ie.getStateChange() == ItemEvent.SELECTED)
{
String item = (String)ie.getItem();
if(shapeBox.getSelectedItem().equals("Circle"))
comboPanel.makeCircles();
if(shapeBox.getSelectedItem().equals("Square"))
comboPanel.makeSquares();
if(shapeBox.getSelectedItem().equals("Oval"))
comboPanel.makeOvals();
if(shapeBox.getSelectedItem().equals("Rectangle"))
comboPanel.makeRectangles();
}
}
});
JPanel southPanel = new JPanel();
southPanel.add(shapeBox);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add(comboPanel, "Center");
getContentPane().add(southPanel, "South");
setSize( 400, 400 );
setLocation( 200, 200 );
setVisible( true );
}
private class ComboPanel extends JPanel
{
int w, h;
Random rand;
static final int OVAL = 0;
static final int RECTANGLE = 1;
int shapeType = -1;
public ComboPanel()
{
rand = new Random();
setBackground(Color.white);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
int x, y;
Shape s = null;
for (int i = 0; i < 20; i++)
{
x = rand.nextInt(width - w);
y = rand.nextInt(width - h);
switch(shapeType)
{
case OVAL: s = new Ellipse2D.Double(x,y,w,h);
break;
case RECTANGLE: s = new Rectangle2D.Double(x,y,w,h);
break;
}
if (shapeType > -1)
g2d.draw(s);
}
}
public void makeCircles()
{
shapeType = OVAL;
w = 75;
h = 75;
repaint();
}
public void makeSquares()
{
shapeType = RECTANGLE;
w = 50;
h = 50;
repaint();
}
public void makeOvals()
{
shapeType = OVAL;
w = 80;
h = 60;
repaint();
}
public void makeRectangles()
{
shapeType = RECTANGLE;
w = 80;
h = 40;
repaint();
}
}
public static void main(String[] args)
{
new HW1b();
}
}
You're hard-coding w and h in your code, and so there's no way for this to vary among your shapes. Instead of doing this, use your Random variable, rand, to select random w and h values that are within some desired range. Myself, I wouldn't create my shapes within the paintComponent method since painting is not fully under my control and can occur when I don't want it to. For instance, in your code, your shapes will vary tremendously if the GUI is resized. Instead I'd create a collection such as an ArrayList<Shape> and fill it with created Shape objects (i.e., Ellipse2D for my circles) when desired, and then iterate through that collection within your paintComponent method, drawing your shapes.
for example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class SomeShapes extends JPanel {
private ShapePanel shapePanel = new ShapePanel();
private JComboBox<MyShape> myShapeCombo = new JComboBox<>(MyShape.values());
public SomeShapes() {
myShapeCombo.setSelectedIndex(-1);
myShapeCombo.addItemListener(new ComboListener());
JPanel bottomPanel = new JPanel();
bottomPanel.add(myShapeCombo);
setLayout(new BorderLayout());
add(shapePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private class ComboListener implements ItemListener {
#Override
public void itemStateChanged(ItemEvent e) {
MyShape myShape = (MyShape) e.getItem();
shapePanel.drawShapes(myShape);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SomeShapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SomeShapes());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
enum MyShape {
OVAL("Oval"), RECTANGLE("Rectangle"), SQUARE("Square"), CIRCLE("Circle");
private String name;
private MyShape(String name) {
this.name = name;
}
public String getName() {
return name;
}
#Override
public String toString() {
return getName();
}
}
class ShapePanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final Color SHAPE_COLOR = Color.BLUE;
private static final int SHAPE_COUNT = 20;
private static int MIN = 5;
private static int MAX = 200;
private List<Shape> shapeList = new ArrayList<>();
private Random random = new Random();
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
public void drawShapes(MyShape myShape) {
shapeList.clear(); // empty the shapeList
switch (myShape) {
case OVAL:
drawOval();
break;
case RECTANGLE:
drawRectangle();
break;
// etc...
default:
break;
}
repaint();
}
private void drawOval() {
// for loop to do this times SHAPE_COUNT(20) times.
for (int i = 0; i < SHAPE_COUNT; i++) {
// first create random width and height
int w = random.nextInt(MAX - MIN) + MIN;
int h = random.nextInt(MAX - MIN) + MIN;
// then random location, but taking care so that it
// fully fits into our JPanel
int x = random.nextInt(getWidth() - w);
int y = random.nextInt(getHeight() - h);
// then create new Shape and place in our shapeList.
shapeList.add(new Ellipse2D.Double(x, y, w, h));
}
}
private void drawRectangle() {
// .... etc
}
//.. .. etc
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// set rendering hints for smooth ovals
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(SHAPE_COLOR);
// iterate through the shapeList ArrayList
for (Shape shape : shapeList) {
g2d.draw(shape); // and draw each Shape it holds
}
}
}
I DISCARD ALL THESE CODE AND WORK ON TOTALLY DIFFERENT ONE>>>
NO NEED TO ANSWER ANYMORE
I got a homework from my professor. He told us to make applet paint program using 4 classes.
Main; LightSourcePanel;DrawingPanel;PalettePanel;
(The basic code are not giving; only the class)
The program will take Mouse input and draw circle when I make a circular line. And the circle will have a 'glowing effect' depend on the lightSource (Use JSlider as light). When the JSlider is slided, the circle glowing effect change real time.
I am having problem referring the LightSource event Listener to the Drawing so It change the 'int light' inside the DrawingPanel. I don't know why the refering in the JColorChooser work while this one don't.
(It gave me java "non-static method cannot be referenced from a static" and I can't change it to static since I need to call repaint() method )
This is my Third class assignment and professor just taught us basic action listener in a single class. So I have no idea what Am I doing. If possible please explain my mistake in detail.
Main code:
public class HW3_to_4 extends Applet {
public void init (){
Dimension d = this.getSize();
setLayout(new BorderLayout());
JPanel Pale = new PalettePanel();
Pale.setBorder(BorderFactory.createLineBorder(Color.black, 5));
JPanel Light = new LightSourcePanel();
Light.setBorder(BorderFactory.createLineBorder(Color.black, 5));
JPanel Draw = new DrawingPanel();
Cursor c = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
Draw.setCursor(c);
Draw.setBorder(BorderFactory.createLineBorder(Color.green, 3));
add(Pale, BorderLayout.SOUTH );
add(Light,BorderLayout.NORTH);
add(Draw,BorderLayout.CENTER);
}
}
PalettePanel Code:
class PalettePanel extends JPanel implements ChangeListener {
JColorChooser j;
public PalettePanel () {
j = new JColorChooser ();
j.setPreviewPanel(new JPanel());
j.getSelectionModel().addChangeListener(this);
this.add(j);
}
public void stateChanged (ChangeEvent e){
Color a = j.getColor();
DrawingPanel.changecolor (a);
}
}
DrawingPanel Code & sub class inside (DrawingCanvas and Polyline)
I rip this off from a example page in YAIP.
:
public class DrawingPanel extends JPanel {
private List<PolyLine> lines = new ArrayList<PolyLine>();
private static PolyLine currentline;
private int maxX,maxY,difX, difY,minX,minY;
private static int lightSource = 0;
public static final int CANVAS_WIDTH = 2000;
public static final int CANVAS_HEIGHT = 800;
public static int[][] circle = new int[1000][4];
public static int check = 0;
public static Color c = Color.RED;
public DrawingPanel(){
DrawingCanvas canvas = new DrawingCanvas();
Dimension d = new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT);
canvas.setPreferredSize(d);
canvas.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
currentline = new PolyLine();
currentline.line_color = c;
lines.add(currentline);
currentline.add(e.getX(), e.getY());
}
});
canvas.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentline.add(e.getX(), e.getY());
repaint();
}
});
canvas.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
for(int i = 0; i<currentline.xList.size()-1; i++){
if(maxX<currentline.xList.get(i)){
maxX = currentline.xList.get(i);
}
}
for(int i = 0; i<currentline.yList.size()-1; i++){
if(maxY<currentline.yList.get(i)){
maxY = currentline.yList.get(i);
}
}
minX = maxX;
for(int i = 0; i<currentline.xList.size()-1; i++){
if(minX>currentline.xList.get(i)){
minX = currentline.xList.get(i);
}
}
minY = maxY;
for(int i = 0; i<currentline.yList.size()-1; i++){
if(minY>currentline.yList.get(i)){
minY = currentline.yList.get(i);
}
}
difX = maxX - minX;
difY = maxY - minY;
currentline.addcircle(minX,minY, difX, difY);
circle[check][0] = minX;
circle[check][1] = minY;
circle[check][2] = difX;
circle[check][3] = difY;
check++;
repaint();
maxX = 0; difX = 0;
maxY = 0; difY = 0;
}
});
this.add(canvas);
}
public static void changecolor(Color b){
c = b;
}
public void lightChange (int light){
lightSource = light;
RE();
}
public void RE (){
for (int x = 0; x< check ; x++) currentline.addcircle(circle[x][0],circle[x][1], circle[x][2],circle[x][3],lightSource);
repaint();
}
private class DrawingCanvas extends JPanel{
public void paint(Graphics g){
for(PolyLine line : lines){
g.setColor(line.line_color);
line.draw(g);
}
}
}
static class PolyLine{
public Color line_color = Color.RED;
private List <Integer> xList;
private List <Integer> yList;
boolean drawcircle = false;
int minx, miny, difx, dify, light;
public PolyLine() {
xList = new ArrayList<Integer>();
yList = new ArrayList<Integer>();
}
public void add(int x, int y){
xList.add(x);
yList.add(y);
}
public void addcircle(int x, int y, int difx, int dify){
this.minx = x; this.miny = y; this.difx = difx; this.dify = dify;
drawcircle = true;
}
public void addcircle(int x, int y, int difx, int dify, int light){
this.minx = x; this.miny = y; this.difx = difx; this.dify = dify; this.light = light;
drawcircle = true;
}
public void draw(Graphics g){
if(drawcircle){
g.fillOval(minx, miny, difx, dify);
g.setColor(Color.WHITE);
g.fillOval((minx+difx/4)+light, miny+dify/4, difx/4, dify/4);
g.setColor(line_color);
}else{
for(int i = 0; i<xList.size()-1; i++){
g.drawLine((int)xList.get(i), (int)yList.get(i), (int)xList.get(i + 1),
(int)yList.get(i + 1));
}
}
}}}
>
Here is the problem.
LightSourcePanel Code:
class LightSourcePanel extends JPanel implements ChangeListener {
static JSlider j;
public LightSourcePanel (){
j = new JSlider(0,180,90);
j.setMajorTickSpacing(45);
j.setMinorTickSpacing(5);
j.setPaintLabels(true);
j.setPaintTicks (true);
j.setPreferredSize(new Dimension (1500,50));
j.addChangeListener(this);
this.add(j);
}
#Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
lightChange(j.getValue()); // problem <<<<<< FAIL
}}
First off, change the changecolor(Color) method in DrawingPanel to remove the static keyword. Then you need an instance of DrawingColor to use the class. I would store the instance of it in a field just like JColorChooser is stored.