How to add resizing slider to rectangle shape in java? - java

Here is my rectangle shape code:
if(rect){
gphcs.setColor(Color.BLUE);
if(orangeshp)
gphcs.setColor(Color.ORANGE);
if(greenshp)
gphcs.setColor(Color.GREEN);
gphcs.fillRect(20,35,100,30);
}
I want to to add a resizing slider in order to change size of the rectangle.
Here is a sample pic of the slider I want to add:
I just need any simple code to create this slider.
Thanks for your time..

JSlider size = new JSlider(JSlider.HORIZONTAL, 0, 250, 100);

JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 250, 100);
slider.addChangeListener(this);
public void stateChanged (ChangeEvent event){
JSlider slider = (JSlider)event.getSource();
int value = slider.getValue();
//manipulate the value in the proption you wants to increase your coordinate of rectangle
//change the size of rectangle here
}

Related

How to Make an Image not Resize Because of Rotating it (in JavaFX)?

I have a method that rotates an image and uses the drawImage method to display it onto a canvas. However, when rotating the image, the image shrinks and grows because the width and height change (say you rotate a square, the width and height of the image changes). Here is the method:
public void rotateImage(GraphicsContext gc, double speed) {
erase(gc); // erases the previous image
imgView.setRotate(imgView.getRotate() + speed);
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
image = imgView.snapshot(params, null);
gc.drawImage(image, pos.x, pos.y, width, height);
}
Any help would be appreciated, and I can post the rest of the code if needed.
A snapshot with the provided parameters uses the dimensions of the node in the parent to determine the size of the image. Rotating an image yields dimensions different to those of the original image in most cases. In those cases the snapshot is bigger than the original image. (Consider a square image rotated by 45°; The width and height of the rotated image is the size of the diagonal of the original image, i.e. larger by a factor of sqrt(2) = 1.41...).
Since drawImage scales the drawn image to fit into a rectangle of size width x height, the snapshot that is larger than this size is scaled down.
Use the transforms of the GraphicsContext instead to avoid creating a new Image instance with each call of the method and avoid scaling the image.
Example
#Override
public void start(Stage primaryStage) {
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/240px-Smiley.svg.png");
Canvas canvas = new Canvas(500, 500);
GraphicsContext context = canvas.getGraphicsContext2D();
Slider slider = new Slider(0, 360, 0);
Button btn = new Button("draw");
VBox root = new VBox(canvas, slider, btn);
btn.setOnAction(evt -> {
context.setFill(Color.TRANSPARENT);
context.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
double posX = 200;
double posY = 150;
context.save();
// apply transformation that puts makes (posX, posY) the point
// where (0,0) is drawn and rotate
context.translate(posX, posY);
context.rotate(slider.getValue());
// draw with center at (0, 0)
context.drawImage(image, -image.getWidth()/2, -image.getHeight()/2);
// undo transformations
context.restore();
});
primaryStage.setScene(new Scene(root));
primaryStage.show();
}

Scroll Pane Zoom In Java

I have a scroll pane in java with content as an anchorpane , the anchorpane has children which are an imageview binded with the anchorpanes height and width so it can fit the anchorpane exactly and also some circles and lines representing a graph. If i enlarge the height or width of the anchorpane then the scroll pane t becomes automatically scrollable giving me the ability to view the image as if it was zoomed , but what im trying to do is zoom in on a specified area on the pic and also , to make the circles and lines change position based on the zoom (maybe using binding properties). The whole idea im trying to implement is have a picture as a map and a graph representing links between cities , but at the same time i was to zoom in on a certain path and have it take the whole pane and navigating between the scroll pane after zooming in.
AnchorPane other;
ImageView image;
ScrollPane scroll;
Button bp;
Button bm;
public void initialize(URL arg0, ResourceBundle arg1) {
System.out.println("OK");
Circle c = new Circle(250, 250, 10);
Circle c2 = new Circle(20, 20, 10);
Circle c3 = new Circle(40, 200, 10);
c3.setFill(Color.RED);
c3.setVisible(true);
c2.setFill(Color.RED);
Line line = new Line();
Line line2 = new Line();
Line line3 = new Line();
line.setStartX(c.getCenterX());
line.setStartY(c.getCenterY());
line.setEndX(c2.getCenterX());
line.setEndY(c2.getCenterY());
line2.setStartX(c.getCenterX());
line2.setStartY(c.getCenterY());
line2.setEndX(c3.getCenterX());
line2.setEndY(c3.getCenterY());
line2.setVisible(true);
line2.setStroke(Color.BLUE);
line3.setStartX(c2.getCenterX());
line3.setStartY(c2.getCenterY());
line3.setEndX(c3.getCenterX());
line3.setEndY(c3.getCenterY());
line3.setVisible(true);
line3.setStroke(Color.BLUE);
line.setVisible(true);
line.setStroke(Color.BLUE);
c.setFill(Color.RED);
c2.setVisible(true);
c.setVisible(true);
Rectangle rect = new Rectangle(100, 100, 70, 70);
other.getChildren().addAll(line, line2, line3, c, c2, c3, rect);
System.out.println(other.getChildren());
image.setVisible(true);
image.fitHeightProperty().bind(other.prefHeightProperty());
image.fitWidthProperty().bind(other.prefWidthProperty());
scroll.setPannable(true);
}
public void bpAction() {
other.setPrefHeight(other.getPrefHeight() + 30);
other.setPrefWidth(other.getPrefWidth() + 30);
}
public void bmAction() {
other.setPrefHeight(other.getPrefHeight() - 30);
other.setPrefWidth(other.getPrefWidth() - 30);
}

how does a swing timer work?

hello i am having trouble trying to understand swing timers. to help me could someone show me a simple flickering animation? i have looked arround on the internet but still dont fully understand how they work. it would be so helpful if someone could give me an example like this:
say if i created a circle:
g.setColor(colors.ORANGE);
g.fillOval(160, 70, 50, 50);
how could i then use a swing timer to change the color from orange to say Gray using a swing timer with a delay?
thank you so much for helping me understand :)
First of all, you wouldn't hard-code your color use like this:
g.setColor(colors.ORANGE);
g.fillOval(160, 70, 50, 50);
Since this prevents all ability to change the color's state. Instead use a class field to hold the Color used, and call it something like ovalColor:
private Color ovalColor = SOME_DEFAULT_COLOR; // some starting color
And then use that color to draw with:
g.setColor(ovalColor);
g.fillOval(160, 70, 50, 50);
I'd then give my class an array of Color or ArrayList<Color> and an int index field:
private static final Color[] COLORS = {Color.black, Color.blue, Color.red,
Color.orange, Color.cyan};
private int index = 0;
private Color ovalColor = COLORS[index]; // one way to set starting value
Then in the Swing Timer's ActionListener I'd increment the index, I'd mod it by the size of the array or of the ArrayList, I'd get the Color indicated by the index and call repaint();
index++;
index %= COLORS.length;
ovalColor = COLORS[index];
repaint();
Also here's a somewhat similar example.
Also please look at the Swing Timer Tutorial.
maybe this would help:
public class object{
Color color = Color.GREEN;
Timer timer;
public object() {
timer = null;
timer = new Timer(5000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (color.equals(Color.GREEN)) {
color = Color.RED;
timer.setDelay(2000);
} else {
color = Color.GREEN;
timer.setDelay(8000);
}
repaint();
}
});
timer.start();}}
I think a paint method would work.like this:
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.green);
g.filloval(30,40,50,50);
}

How can I duplicate my code simply?

I have the code below; I want it also want the code in the 'action performed' to happen when it is hovered over. Instead of copy and pasteing the code again and have twice as many times, Is there a way to set it easily?
The code:
private void btnGreenActionPerformed(java.awt.event.ActionEvent evt) {
Color btnGrn = new Color(159, 191, 143); //Sets the colour to a class
Color txtGrn = new Color(201, 255, 191); //Sets the colour to a class
Color txtGry = new Color(89, 89, 89); //Sets the colour to a class
this.getContentPane().setBackground(new java.awt.Color(127,191,95)); //Sets background color to green
btnConvert.setBackground(btnGrn); //Changes the colors to green
btnReset.setBackground(btnGrn); //Changes the colors to green
btnClose.setBackground(btnGrn); //Changes the colors to green
btnInfo.setBackground(btnGrn); //Changes the colors to green
txtIncome.setBackground(txtGrn); //Changes the colors to green
txtPayable.setBackground(txtGrn); //Changes the colors to green
txtStatus.setBackground(txtGrn); //Changes the colors to green
txtIncome.setForeground(txtGry); //Changes the colors to grey
txtPayable.setForeground(txtGry); //Changes the colors to grey
txtStatus.setForeground(txtGry); //Changes the colors to grey
}
Note: I have 7 buttons that are all the same except for the color values.
I would add an inner class to your panel class, to look after the colours, then create as many instances of it as you have colour schemes, including whatever colours you like. Something like this.
This is all inside your panel class, by the way. I'm not envisaging this as a stand-alone class in its own .java file; because it's entirely bound to the characteristics of the panel. A more re-usable version of it would look a bit different.
private class ButtonColorScheme {
final Color paneBackground;
final Color buttonBackground;
final Color textBackground;
final Color textForeground;
ButtonColorScheme(Color paneBackground, Color buttonBackground, Color textBackground, Color textForeground) {
this.paneBackground = paneBackground;
this.buttonBackground = buttonBackground;
this.textBackground = textBackground;
this.textForeground = textForeground;
}
void apply() {
getContentPane().setBackground(paneBackground);
btnConvert.setBackground(buttonBackground);
btnReset.setBackground(buttonBackground);
btnClose.setBackground(buttonBackground);
btnInfo.setBackground(buttonBackground);
txtIncome.setBackground(textBackground);
txtPayable.setBackground(textBackground);
txtStatus.setBackground(textBackground);
txtIncome.setForeground(textForeground);
txtPayable.setForeground(textForeground);
txtStatus.setForeground(textForeground);
}
}
private final ButtonColorScheme greenAndGrey = new ButtonColorScheme(
new Color(127,191,95), new Color(159, 191, 143), new Color(201, 255, 191), new Color(89, 89, 89));
private final ButtonColorScheme redAndBlack = new ButtonColorScheme(
new Color(191,120,95), new Color(202, 160, 143), new Color(255, 180, 191), Color.BLACK);
public void btnGreenActionPerformed(ActionEvent evt){
greenAndGrey.apply();
}
public void btnRedActionPerformed(ActionEvent evt){
redAndBlack.apply();
}
Just create a method,
private void setWidgetColors(Color ... colours){
int i=0;
Color btnGrn = i++ <= colours.length?colours[i-1]:new Color(159, 191, 143); //Sets the colour to a class
Color txtGrn = i++ <= colours.length?colours[i-1]:new Color(201, 255, 191); //Sets the colour to a class
Color txtGry = i++ <= colours.length?colours[i-1]:new Color(89, 89, 89);
//Sets the colour to a class
this.getContentPane().setBackground(i++ <= colours.length ? colours[3] : new Color(127,191,95)); //Sets background color to green
btnConvert.setBackground(btnGrn); //Changes the colors to green
btnReset.setBackground(btnGrn); //Changes the colors to green
btnClose.setBackground(btnGrn); //Changes the colors to green
btnInfo.setBackground(btnGrn); //Changes the colors to green
txtIncome.setBackground(txtGrn); //Changes the colors to green
txtPayable.setBackground(txtGrn); //Changes the colors to green
txtStatus.setBackground(txtGrn); //Changes the colors to green
txtIncome.setForeground(txtGry); //Changes the colors to grey
txtPayable.setForeground(txtGry); //Changes the colors to grey
txtStatus.setForeground(txtGry); //Changes the colors to grey
}
And then
private void btnGreenActionPerformed(ActionEvent evt){
Color btnGrn = new Color(159, 191, 143); //Sets the colour to a class
Color txtGrn = new Color(201, 255, 191); //Sets the colour to a class
Color txtGry = new Color(89, 89, 89);
Color backgroundColor = new Color(127,191,95);
setWidgetColors(btnGrn,txtGrn,txtGry,backgroundColor);
}
Methods are quite useful in such cases. Place this code into a method.
Add whatever parameters needed (e.g. you may decide to turn these values
159, 191, 143, etc. into parameters of the method say named r,g,b). Then
just call your method with the arguments you need (e.g. with r=166, g=202, b=192).

How to resize image Height to JFrame Height?

I have an image that is too tall for my JFrame even when it is maximized. I want to dynamically resize it so that the image will never be clipped by the top or bottom of the JFrame. I have inserted the image within a JLabel as an ImageIcon. I have tried setting the maximum size to no avail. How do I ensure that the height of the image will never be larger than the JFrame? I would ideally like to keep the ratio of height to width constant. The image is in a portrait orientation. Any ideas?
public class myClass extends JFrame {
private void initGUI(){
pane = getContentPane();
pane.setLayout(new BorderLayout());
next = new JButton("Next");
previous = new JButton("Previous");
page = new JLabel(loadImg());
page.setMaximumSize(this.getSize());
pane.add(next, BorderLayout.EAST);
pane.add(previous, BorderLayout.WEST);
pane.add(page, BorderLayout.CENTER);
}
}
I would ideally like to keep the ratio of height to width constant.
Check out Darryl's Stretch Icon. It will shrink/grow depending on the space available, while maintaining the width/height ratio.
You can try overriding the paintComponent(Graphics g) method and drawing the resized image yourself.
page = new JLabel(loadIMG()){
#Override
paintComponent(Graphics g)
{
//So we don't kill default behaviour
super.paintComponent(g);
int scaledWidth, scaledHeight;
//pseudo-code
scale and store into scaledWidth and scaledHeight;
render with g.drawImage(icon, x, y, scaledWidth, scaledHeight, null);
}
};

Categories

Resources